From cf7a62d89b3b48da7327ffb5c7ff0825c4b5bef7 Mon Sep 17 00:00:00 2001 From: Martin Felis Date: Sat, 10 Jul 2021 11:24:04 +0200 Subject: [PATCH] Minor cleanup World.gd --- scenes/Game.tscn | 1 - scenes/World.gd | 115 +++++++++++++++++++---------------------------- 2 files changed, 46 insertions(+), 70 deletions(-) diff --git a/scenes/Game.tscn b/scenes/Game.tscn index 9954530..122c454 100644 --- a/scenes/Game.tscn +++ b/scenes/Game.tscn @@ -253,7 +253,6 @@ 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="pressed" from="World/UI/TopContainer/Button" to="World" method="_on_generate_button_pressed"] [connection signal="value_changed" from="Editor/UI/Editor/IslandIndex" to="Editor" method="_on_IslandIndex_value_changed"] diff --git a/scenes/World.gd b/scenes/World.gd index c65ef34..42f41c4 100644 --- a/scenes/World.gd +++ b/scenes/World.gd @@ -29,6 +29,9 @@ var current_island = null var player_navigation_path = [] +# +# Godot Functions +# func _ready(): Grid.view_camera = WorldCamera GridHighlight.view_camera = WorldCamera @@ -39,12 +42,6 @@ func _ready(): PlayerChar.position = Globals.HexGrid.get_hex_center(Vector2(0,0)) -func clear_islands(): - for island in Islands.get_children(): - Islands.remove_child(island) - island.queue_free() - - func _process(_delta): WorldCamera.offset = PlayerChar.position PlayerBoat.transform.origin = PlayerChar.transform.origin @@ -58,6 +55,27 @@ func _process(_delta): PlayerChar.target = player_navigation_path[0] +func _draw(): + var nav_path_len = len(player_navigation_path) + if nav_path_len > 0: + var last_point = player_navigation_path[0] + draw_circle(last_point, 5, "#f200f2") + for i in range (1, nav_path_len): + var cur_point = player_navigation_path[i] + draw_line (last_point, cur_point, "#f200f2") + draw_circle(cur_point, 5, "#f200f2") + last_point = cur_point + + +# +# World Modification/Query +# +func clear_islands(): + for island in Islands.get_children(): + Islands.remove_child(island) + island.queue_free() + + func get_tile_type(world_coord: Vector2): for island in Islands.get_children(): var tile = island.get_tile_by_world_coord(world_coord) @@ -74,17 +92,6 @@ func add_island_at(file_name, offset_world: Vector2): Islands.add_child(island) -func populate_ocean_grid(): - var obstacles = Globals.OceanGrid.get_obstacles() - Globals.OceanGrid.remove_obstacles(obstacles.keys()) - Globals.OceanGrid.set_bounds(Vector2.ONE * -500, Vector2.ONE * 500) - - for island in Islands.get_children(): - for tile in island.tiles.keys(): - var grid_coords = Globals.WorldToHex(tile + island.offset_world) - Globals.OceanGrid.add_obstacles(grid_coords, 5) - - func check_island_location_valid(new_island): var grid_origin_world_coord = Globals.HexToWorld(Vector2(0,0)) if new_island.get_tile_by_world_coord(grid_origin_world_coord) != null: @@ -96,7 +103,7 @@ func check_island_location_valid(new_island): return false return true - + func generate(): PlayerChar.transform.origin = Vector2.ZERO @@ -112,7 +119,7 @@ func generate(): for i in range (num_islands): var island = Island.new() - var island_index = i % 2 + var island_index = i % 4 var file_name = "user://pirate_game_island_" + str(island_index) + ".island" island.load_island(file_name) @@ -139,6 +146,19 @@ func generate(): populate_ocean_grid() +# +# Navigation +# +func populate_ocean_grid(): + var obstacles = Globals.OceanGrid.get_obstacles() + Globals.OceanGrid.remove_obstacles(obstacles.keys()) + Globals.OceanGrid.set_bounds(Vector2.ONE * -500, Vector2.ONE * 500) + + for island in Islands.get_children(): + for tile in island.tiles.keys(): + var grid_coords = Globals.WorldToHex(tile + island.offset_world) + Globals.OceanGrid.add_obstacles(grid_coords, 15) + func update_player_navigation_target(target_world: Vector2): var start_coord = Globals.WorldToHex(PlayerChar.transform.origin) @@ -160,6 +180,13 @@ func update_player_navigation_target(target_world: Vector2): update() +# +# Input & Events +# +func _on_generate_button_pressed(): + generate() + + func handle_game_event(event): if event is InputEventMouseButton: # Move main character @@ -206,53 +233,3 @@ func _unhandled_input(event): OffsetValueLabel.text = str(WorldCamera.offset) ZoomValueLabel.text = str(WorldCamera.zoom) - - -func save_world(path: String): - var world_save_data = File.new() - world_save_data.open(path, File.WRITE) - world_save_data.store_line (to_json(tile_data)) - world_save_data.close() - - -func load_world(path: String): - var world_save_data = File.new() - world_save_data.open(path, File.READ) - var data = parse_json(world_save_data.get_line()) - world_save_data.close() - - var data_keys = data.keys() - tile_data = {} - var coord_regex = RegEx.new() - coord_regex.compile("\\((?-?\\d+(.?\\d+)?), (?-?\\d+(.?\\d+)?)\\)") - - var reg_test_result = coord_regex.search("(0, 0)") - assert(reg_test_result) - - reg_test_result = coord_regex.search("(123.124, 552.0)") - assert(reg_test_result) - - for k in data_keys: - var coords = Vector2.ZERO - var regresult = coord_regex.search(k) - if regresult: - 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"))) - tile_data[coords] = data[k] - - update() - - -func _on_generate_button_pressed(): - generate() - -func _draw(): - var nav_path_len = len(player_navigation_path) - if nav_path_len > 0: - var last_point = player_navigation_path[0] - draw_circle(last_point, 5, "#f200f2") - for i in range (1, nav_path_len): - var cur_point = player_navigation_path[i] - draw_line (last_point, cur_point, "#f200f2") - draw_circle(cur_point, 5, "#f200f2") - last_point = cur_point