From 611e93aa479fa37373d36634591eb8e253182010 Mon Sep 17 00:00:00 2001 From: Martin Felis Date: Fri, 9 Jul 2021 18:16:21 +0200 Subject: [PATCH] Added navigation on ocean --- scenes/Game.tscn | 29 ++++++++++++++++--- scenes/Island.gd | 7 +++++ scenes/World.gd | 75 +++++++++++++++++++++++++++++++++++++++++------- 3 files changed, 96 insertions(+), 15 deletions(-) diff --git a/scenes/Game.tscn b/scenes/Game.tscn index b9619fa..9954530 100644 --- a/scenes/Game.tscn +++ b/scenes/Game.tscn @@ -115,18 +115,38 @@ margin_right = 573.0 margin_bottom = 27.0 text = "(0,0)" -[node name="VSeparator" type="VSeparator" parent="World/UI/TopContainer"] +[node name="TileType" type="Label" parent="World/UI/TopContainer"] margin_left = 577.0 -margin_right = 581.0 +margin_top = 13.0 +margin_right = 600.0 +margin_bottom = 27.0 +text = "Tile" +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="TileTypeValue" type="Label" parent="World/UI/TopContainer"] +margin_left = 604.0 +margin_top = 13.0 +margin_right = 638.0 +margin_bottom = 27.0 +text = "None" +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="VSeparator" type="VSeparator" parent="World/UI/TopContainer"] +margin_left = 642.0 +margin_right = 646.0 margin_bottom = 40.0 __meta__ = { "_edit_use_anchors_": false } [node name="FPSValue" type="Label" parent="World/UI/TopContainer"] -margin_left = 585.0 +margin_left = 650.0 margin_top = 13.0 -margin_right = 593.0 +margin_right = 658.0 margin_bottom = 27.0 text = "0" align = 2 @@ -233,6 +253,7 @@ 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/Island.gd b/scenes/Island.gd index 131f1f2..862b1de 100644 --- a/scenes/Island.gd +++ b/scenes/Island.gd @@ -108,6 +108,13 @@ func check_overlap(other): return rect_world.intersects(rect_world_other) +func get_tile_by_world_coord(world_coord: Vector2): + var center_world = Globals.WorldToHexCenter(world_coord - offset_world) + if center_world in tiles.keys(): + return tiles[center_world] + return null + + func calc_rect_world(): var rect_world = Rect2(rect_local) rect_world.position = transform.origin + rect_world.position + offset_world diff --git a/scenes/World.gd b/scenes/World.gd index 213302a..130c659 100644 --- a/scenes/World.gd +++ b/scenes/World.gd @@ -11,6 +11,7 @@ onready var OffsetValueLabel = get_node("UI/TopContainer/OffsetValue") onready var ZoomValueLabel = get_node("UI/TopContainer/ZoomValue") onready var HexCoordValueLabel = get_node("UI/TopContainer/HexCoordValue") onready var WorldCoordValueLabel = get_node("UI/TopContainer/WorldCoordValue") +onready var TileTypeValueLabel = get_node("UI/TopContainer/TileTypeValue") onready var PlayerChar = get_node("PlayerChar") onready var PlayerBoat = get_node("PlayerBoat") onready var FPSValueLabel = get_node("UI/TopContainer/FPSValue") @@ -25,6 +26,7 @@ var drag_start = null var target = Vector2() var tile_data = {} var current_island = null +var player_navigation_path = [] func _ready(): @@ -46,10 +48,19 @@ func clear_islands(): func _process(_delta): WorldCamera.offset = PlayerChar.position PlayerBoat.transform.origin = PlayerChar.transform.origin + + if len(player_navigation_path) > 1: + var player_coord = Globals.WorldToHexCenter(PlayerChar.transform.origin) + print (player_coord, " nav_path_0 ", player_navigation_path[0]) + if (player_coord - player_navigation_path[0]).length_squared() < 0.1: + player_navigation_path.remove(0) + + if len(player_navigation_path) > 0: + PlayerChar.target = player_navigation_path[0] pass -func check_island_overlap(new_island): +func check_island_location_valid(new_island): var islands = Islands.get_children() for island in islands: @@ -59,6 +70,15 @@ func check_island_overlap(new_island): return false +func get_tile_type(world_coord: Vector2): + for island in Islands.get_children(): + var tile = island.get_tile_by_world_coord(world_coord) + if tile != null: + return tile + + return null + + func add_island_at(file_name, offset_world: Vector2): var island = Island.new() island.load_island(file_name) @@ -68,6 +88,12 @@ func add_island_at(file_name, offset_world: Vector2): func populate_ocean_grid(): Globals.OceanGrid.remove_obstacles(Globals.OceanGrid.get_obstacles()) + 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) func generate(): @@ -88,19 +114,19 @@ func generate(): var rand_coord = Vector2(rng.randi_range(-radius, radius), rng.randi_range(-radius, radius)) island.offset_world = Globals.WorldToHexCenter(rand_coord) - var overlapping = check_island_overlap(island) + var location_valid = check_island_location_valid(island) var overlap_retry_num = 0 var overlap_retry_max = 10 - while overlapping and overlap_retry_num < overlap_retry_max: + while location_valid and overlap_retry_num < overlap_retry_max: if overlap_retry_num % 4 == 0: radius = radius + 500 overlap_retry_num = overlap_retry_num + 1 rand_coord = Vector2(rng.randi_range(-radius, radius), rng.randi_range(-radius, radius)) island.offset_world = Globals.WorldToHexCenter(rand_coord) - overlapping = check_island_overlap(island) + location_valid = check_island_location_valid(island) - if overlapping: + if location_valid: print ("Could not place island!") else: print ("Placed after " + str(overlap_retry_num) + " retries.") @@ -109,13 +135,26 @@ func generate(): populate_ocean_grid() +func update_player_navigation_target(target_world: Vector2): + var start_coord = Globals.WorldToHex(PlayerChar.transform.origin) + var goal_coord = Globals.WorldToHex(target_world) + + var path = Globals.OceanGrid.find_path(start_coord, goal_coord) + player_navigation_path = [] + for p in path: + player_navigation_path.append(Globals.HexToWorld(p.axial_coords)) + + if len(player_navigation_path) > 1: + PlayerChar.target = player_navigation_path[1] + + update() + func handle_game_event(event): if event is InputEventMouseButton: # Move main character if event.pressed and event.button_index == BUTTON_LEFT: - PlayerChar.target = Globals.HexGrid.get_hex_center(Globals.ScreenToHex(event.position, WorldCamera)) - return true + update_player_navigation_target (Globals.HexGrid.get_hex_center(Globals.ScreenToHex(event.position, WorldCamera))) return false @@ -142,11 +181,14 @@ func _unhandled_input(event): hex_hover = Globals.ScreenToHex(event.position, WorldCamera) GridHighlight.pos = hex_hover HexCoordValueLabel.text = str(hex_hover) - -# Islands.get_children()[1].offset_world = Globals.HexToWorld(hex_hover) -# Islands.get_children()[1].update() + var world_coord = Globals.ScreenToWorld(event.position, WorldCamera) + var tile_type = get_tile_type(world_coord) + if tile_type != null: + TileTypeValueLabel.text = tile_type + else: + TileTypeValueLabel.text = "None" - WorldCoordValueLabel.text = str(Globals.ScreenToWorld(event.position, WorldCamera)) + WorldCoordValueLabel.text = str(world_coord) if is_dragging: WorldCamera.offset = (drag_start - event.position) * WorldCamera.zoom.x @@ -193,3 +235,14 @@ func load_world(path: String): 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