From f8d77b4a605dbbe4dfe9f9999603225a60945b73 Mon Sep 17 00:00:00 2001 From: Martin Felis Date: Mon, 28 Jun 2021 22:34:48 +0200 Subject: [PATCH] Added support for editing multiple islands --- Globals.gd | 4 --- scenes/Editor.gd | 41 +++++++++++++++++++++--------- scenes/Game.tscn | 55 +++++++++++++++++++++++------------------ scenes/HexTileDrawer.gd | 3 ++- scenes/Island.gd | 25 ++++++++++++++++++- scenes/World.gd | 8 +++--- 6 files changed, 90 insertions(+), 46 deletions(-) diff --git a/Globals.gd b/Globals.gd index 30bbd3e..baf6863 100644 --- a/Globals.gd +++ b/Globals.gd @@ -44,7 +44,3 @@ func ScreenToHex(pos: Vector2, camera: Camera2D): func WorldToScreen(pos: Vector2, camera: Camera2D): return pos * camera.zoom + camera.offset - - -func DrawTile(world_coords: Vector2, tile_type): - HexTileDrawer.draw_tile(world_coords, tile_type) diff --git a/scenes/Editor.gd b/scenes/Editor.gd index 882baaf..98a050d 100644 --- a/scenes/Editor.gd +++ b/scenes/Editor.gd @@ -9,11 +9,12 @@ extends Node2D onready var EditorUI = get_node("UI/Editor") onready var TileTypes = get_node("UI/TileTypes") 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 # Called when the node enters the scene tree for the first time. func _ready(): @@ -21,6 +22,8 @@ func _ready(): func handle_editor_event(event): + var island = world.current_island + 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() @@ -29,12 +32,7 @@ func handle_editor_event(event): tile_type = pressed_button.text var hex_coord = Globals.HexGrid.get_hex_center(Globals.ScreenToHex(event.position,world.WorldCamera)) - if tile_type != "None": - world.tile_data[hex_coord] = tile_type - update() - elif hex_coord in world.tile_data.keys(): - world.tile_data.erase(hex_coord) - update() + island.set_tile(hex_coord, tile_type) return true @@ -56,20 +54,39 @@ func _unhandled_input(event): 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(): - world.save_world("user://pirate_game_world.save") + var island = world.current_island + island.save_island(get_island_filename()) func _on_ClearWorldButton_pressed(): - world.tile_data = {} - world.update() + var island = world.current_island + island.clear_island() func _on_LoadWorldButton_pressed(): - world.load_world("user://pirate_game_world.save") - update() + var island = world.current_island + 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): + var island = world.current_island + if last_index >= 0: + island.save_island(get_island_filename_for_index(last_index)) + island.clear_island() + island.load_island(get_island_filename()) + diff --git a/scenes/Game.tscn b/scenes/Game.tscn index f19a13e..412214e 100644 --- a/scenes/Game.tscn +++ b/scenes/Game.tscn @@ -139,39 +139,45 @@ __meta__ = { "_edit_use_anchors_": false } -[node name="World" type="Label" parent="Editor/UI/Editor"] -margin_right = 82.0 +[node name="Island" type="Label" parent="Editor/UI/Editor"] +margin_right = 110.0 margin_bottom = 33.0 -text = "World" +text = "Island" + +[node name="IslandIndex" type="SpinBox" parent="Editor/UI/Editor"] +margin_top = 37.0 +margin_right = 110.0 +margin_bottom = 80.0 +max_value = 9.0 [node name="ClearWorldButton" type="Button" parent="Editor/UI/Editor"] -margin_top = 37.0 -margin_right = 82.0 -margin_bottom = 76.0 +margin_top = 84.0 +margin_right = 110.0 +margin_bottom = 123.0 text = "Clear" [node name="LoadWorldButton" type="Button" parent="Editor/UI/Editor"] -margin_top = 80.0 -margin_right = 82.0 -margin_bottom = 119.0 +margin_top = 127.0 +margin_right = 110.0 +margin_bottom = 166.0 text = "Load" [node name="SaveWorldButton" type="Button" parent="Editor/UI/Editor"] -margin_top = 123.0 -margin_right = 82.0 -margin_bottom = 162.0 +margin_top = 170.0 +margin_right = 110.0 +margin_bottom = 209.0 text = "Save" [node name="Label" type="Label" parent="Editor/UI/Editor"] -margin_top = 166.0 -margin_right = 82.0 -margin_bottom = 199.0 +margin_top = 213.0 +margin_right = 110.0 +margin_bottom = 246.0 text = "Tiles" [node name="NoneButton" type="Button" parent="Editor/UI/Editor"] -margin_top = 203.0 -margin_right = 82.0 -margin_bottom = 242.0 +margin_top = 250.0 +margin_right = 110.0 +margin_bottom = 289.0 rect_pivot_offset = Vector2( -1239.87, 282.07 ) toggle_mode = true group = SubResource( 3 ) @@ -181,9 +187,9 @@ __meta__ = { } [node name="SandButton" type="Button" parent="Editor/UI/Editor"] -margin_top = 246.0 -margin_right = 82.0 -margin_bottom = 285.0 +margin_top = 293.0 +margin_right = 110.0 +margin_bottom = 332.0 rect_pivot_offset = Vector2( -1239.87, 282.07 ) toggle_mode = true group = SubResource( 3 ) @@ -193,14 +199,15 @@ __meta__ = { } [node name="GrassButton" type="Button" parent="Editor/UI/Editor"] -margin_top = 289.0 -margin_right = 82.0 -margin_bottom = 328.0 +margin_top = 336.0 +margin_right = 110.0 +margin_bottom = 375.0 rect_pivot_offset = Vector2( -1239.87, 282.07 ) toggle_mode = true group = SubResource( 3 ) text = "Grass" [connection signal="toggled" from="World/UI/TopContainer/EditIslandButton" to="Editor" method="_on_EditIslandButton_toggled"] +[connection signal="value_changed" from="Editor/UI/Editor/IslandIndex" to="Editor" method="_on_IslandIndex_value_changed"] [connection signal="pressed" from="Editor/UI/Editor/ClearWorldButton" to="Editor" method="_on_ClearWorldButton_pressed"] [connection signal="pressed" from="Editor/UI/Editor/LoadWorldButton" to="Editor" method="_on_LoadWorldButton_pressed"] [connection signal="pressed" from="Editor/UI/Editor/SaveWorldButton" to="Editor" method="_on_SaveWorldButton_pressed"] diff --git a/scenes/HexTileDrawer.gd b/scenes/HexTileDrawer.gd index 34818db..e655ff0 100644 --- a/scenes/HexTileDrawer.gd +++ b/scenes/HexTileDrawer.gd @@ -6,6 +6,7 @@ enum TileType { Grass } +var TileTypeFromString = { "None": TileType.None, "Sand": TileType.Sand, "Grass": TileType.Grass } var HexPoints = null var HexColors = [] @@ -29,7 +30,7 @@ func set_hex_scale(scale): for i in range (7): var angle = (60 * i) * PI / 180 HexPoints.append(Vector2(cos(angle), sin(angle)) * hex_scale.x / 2) - NoneColors.append("#555555") + NoneColors.append("#885555") SandColors.append("#ffa106") GrassColors.append("#4b9635") diff --git a/scenes/Island.gd b/scenes/Island.gd index 66e46a9..e6fac13 100644 --- a/scenes/Island.gd +++ b/scenes/Island.gd @@ -7,6 +7,21 @@ func _ready(): pass # Replace with function body. +func set_tile(coord: Vector2, value: String): + if HexTileDrawer.TileTypeFromString[value] == HexTileDrawer.TileType.None: + if coord in tiles.keys(): + tiles.erase(coord) + else: + tiles[coord] = value + + update() + + +func clear_island(): + tiles = {} + update() + + func save_island(path: String): var island_save_data = File.new() island_save_data.open(path, File.WRITE) @@ -16,7 +31,13 @@ func save_island(path: String): func load_island(path: String): var island_save_data = File.new() + + if !island_save_data.file_exists(path): + clear_island() + return + island_save_data.open(path, File.READ) + var data = parse_json(island_save_data.get_line()) island_save_data.close() @@ -35,10 +56,12 @@ func load_island(path: String): var coords = Vector2.ZERO var regresult = coord_regex.search(k) if regresult: - print(regresult.get_string("tile_x"), " - ", regresult.get_string("tile_y")) +# print(regresult.get_string("tile_x"), " - ", regresult.get_string("tile_y")) coords = Vector2(float(regresult.get_string("tile_x")), float(regresult.get_string("tile_y"))) tiles[coords] = data[k] + update() + func _draw(): for coord in tiles.keys(): diff --git a/scenes/World.gd b/scenes/World.gd index 29ba18f..ff8f35a 100644 --- a/scenes/World.gd +++ b/scenes/World.gd @@ -23,17 +23,17 @@ var drag_start = null var target = Vector2() var tile_data = {} var islands = [] -var island = null +var current_island = null func _ready(): Grid.view_camera = WorldCamera GridHighlight.view_camera = WorldCamera - island = Island.new() - Islands.add_child(island) + current_island = Island.new() + Islands.add_child(current_island) - island.load_island("user://pirate_game_world.save") + current_island.load_island("user://pirate_game_world.save") # Set player starting position PlayerChar.position = Globals.HexGrid.get_hex_center(Vector2(0,0))