PirateTreasureHunt/scenes/Editor.gd

105 lines
2.4 KiB
GDScript

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")
# World objects
onready var world = get_node("../World")
onready var world_camera = get_node("../World/Camera")
var last_index = -1
var Island = preload("Island.gd")
var island = null
# Called when the node enters the scene tree for the first time.
func _ready():
island = Island.new()
island.load_island("res://islands/pirate_game_island_0.island")
add_child(island)
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
var hex_coord = Globals.HexGrid.get_hex_center(Globals.ScreenToHex(event.position,world.WorldCamera))
island.set_tile(hex_coord, tile_type)
return true
return false
func _process(_delta):
world.Islands.visible = !EditorUI.visible
island.visible = EditorUI.visible
func is_active():
return EditorUI.visible
func _unhandled_input(event):
if not EditorUI.visible:
return
if event is InputEventMouseButton:
if handle_editor_event (event):
get_tree().set_input_as_handled()
update()
if 'position' in event:
if handle_editor_event (event):
get_tree().set_input_as_handled()
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)
func _on_SaveWorldButton_pressed():
island.save_island(get_island_filename())
func _on_ClearWorldButton_pressed():
island.clear_island()
func _on_LoadWorldButton_pressed():
island.load_island(get_island_filename())
last_index = IslandIndex.value
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())
func _on_GenerateButton_pressed():
island.clear_island()
island.generate()