PirateTreasureHunt/scenes/Editor.gd

100 lines
2.3 KiB
GDScript
Raw Normal View History

2021-06-27 12:26:44 +02:00
extends Node2D
# Declare member variables here. Examples:
# var a = 2
# var b = "text"
# UI elements
onready var EditorUI = get_node("UI/Editor")
onready var NoneButton = get_node("UI/Editor/NoneButton")
onready var IslandIndex = get_node("UI/Editor/IslandIndex")
2021-06-27 12:26:44 +02:00
# World objects
onready var world = get_node("../World")
onready var world_camera = get_node("../World/Camera")
var last_index = -1
2021-06-27 12:26:44 +02:00
2021-07-14 20:11:20 +02:00
var Island = preload("Island.gd")
var island = null
2021-06-27 12:26:44 +02:00
# Called when the node enters the scene tree for the first time.
func _ready():
2021-07-14 20:11:20 +02:00
island = Island.new()
island.load_island("res://islands/pirate_game_island_0.island")
add_child(island)
2021-06-27 12:26:44 +02:00
func handle_editor_event(event):
if (Input.get_mouse_button_mask() & BUTTON_LEFT) == BUTTON_LEFT:
# if event.pressed and event.button_index == BUTTON_LEFT:
var pressed_button = NoneButton.group.get_pressed_button()
var tile_type = "None"
if pressed_button:
tile_type = pressed_button.text
2021-06-28 21:51:09 +02:00
var hex_coord = Globals.HexGrid.get_hex_center(Globals.ScreenToHex(event.position,world.WorldCamera))
island.set_tile(hex_coord, tile_type)
2021-06-27 12:26:44 +02:00
return true
return false
2021-07-14 20:11:20 +02:00
func _process(_delta):
world.Islands.visible = !EditorUI.visible
island.visible = EditorUI.visible
func is_active():
return EditorUI.visible
2021-06-27 12:26:44 +02:00
func _unhandled_input(event):
if not EditorUI.visible:
return
if event is InputEventMouseButton:
if handle_editor_event (event):
2021-07-14 20:11:20 +02:00
get_tree().set_input_as_handled()
update()
2021-06-27 12:26:44 +02:00
if 'position' in event:
if handle_editor_event (event):
2021-07-14 20:11:20 +02:00
get_tree().set_input_as_handled()
2021-06-27 12:26:44 +02:00
update()
func get_island_filename_for_index(index):
return "user://pirate_game_island_" + str(index) + ".island"
func get_island_filename():
return get_island_filename_for_index(IslandIndex.value)
2021-06-27 12:26:44 +02:00
func _on_SaveWorldButton_pressed():
island.save_island(get_island_filename())
2021-06-27 12:26:44 +02:00
func _on_ClearWorldButton_pressed():
island.clear_island()
2021-06-27 12:26:44 +02:00
func _on_LoadWorldButton_pressed():
island.load_island(get_island_filename())
last_index = IslandIndex.value
2021-06-27 12:26:44 +02:00
func _on_EditIslandButton_toggled(button_pressed):
EditorUI.visible = button_pressed
print (button_pressed)
func _on_IslandIndex_value_changed(_value):
if last_index >= 0:
island.save_island(get_island_filename_for_index(last_index))
island.clear_island()
island.load_island(get_island_filename())