Added navigation on ocean

master
Martin Felis 2021-07-09 18:16:21 +02:00
parent 2652bbbfa8
commit 611e93aa47
3 changed files with 96 additions and 15 deletions

View File

@ -115,18 +115,38 @@ margin_right = 573.0
margin_bottom = 27.0 margin_bottom = 27.0
text = "(0,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_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 margin_bottom = 40.0
__meta__ = { __meta__ = {
"_edit_use_anchors_": false "_edit_use_anchors_": false
} }
[node name="FPSValue" type="Label" parent="World/UI/TopContainer"] [node name="FPSValue" type="Label" parent="World/UI/TopContainer"]
margin_left = 585.0 margin_left = 650.0
margin_top = 13.0 margin_top = 13.0
margin_right = 593.0 margin_right = 658.0
margin_bottom = 27.0 margin_bottom = 27.0
text = "0" text = "0"
align = 2 align = 2
@ -233,6 +253,7 @@ rect_pivot_offset = Vector2( -1239.87, 282.07 )
toggle_mode = true toggle_mode = true
group = SubResource( 3 ) group = SubResource( 3 )
text = "Grass" text = "Grass"
[connection signal="toggled" from="World/UI/TopContainer/EditIslandButton" to="Editor" method="_on_EditIslandButton_toggled"] [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="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"] [connection signal="value_changed" from="Editor/UI/Editor/IslandIndex" to="Editor" method="_on_IslandIndex_value_changed"]

View File

@ -108,6 +108,13 @@ func check_overlap(other):
return rect_world.intersects(rect_world_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(): func calc_rect_world():
var rect_world = Rect2(rect_local) var rect_world = Rect2(rect_local)
rect_world.position = transform.origin + rect_world.position + offset_world rect_world.position = transform.origin + rect_world.position + offset_world

View File

@ -11,6 +11,7 @@ onready var OffsetValueLabel = get_node("UI/TopContainer/OffsetValue")
onready var ZoomValueLabel = get_node("UI/TopContainer/ZoomValue") onready var ZoomValueLabel = get_node("UI/TopContainer/ZoomValue")
onready var HexCoordValueLabel = get_node("UI/TopContainer/HexCoordValue") onready var HexCoordValueLabel = get_node("UI/TopContainer/HexCoordValue")
onready var WorldCoordValueLabel = get_node("UI/TopContainer/WorldCoordValue") onready var WorldCoordValueLabel = get_node("UI/TopContainer/WorldCoordValue")
onready var TileTypeValueLabel = get_node("UI/TopContainer/TileTypeValue")
onready var PlayerChar = get_node("PlayerChar") onready var PlayerChar = get_node("PlayerChar")
onready var PlayerBoat = get_node("PlayerBoat") onready var PlayerBoat = get_node("PlayerBoat")
onready var FPSValueLabel = get_node("UI/TopContainer/FPSValue") onready var FPSValueLabel = get_node("UI/TopContainer/FPSValue")
@ -25,6 +26,7 @@ var drag_start = null
var target = Vector2() var target = Vector2()
var tile_data = {} var tile_data = {}
var current_island = null var current_island = null
var player_navigation_path = []
func _ready(): func _ready():
@ -46,10 +48,19 @@ func clear_islands():
func _process(_delta): func _process(_delta):
WorldCamera.offset = PlayerChar.position WorldCamera.offset = PlayerChar.position
PlayerBoat.transform.origin = PlayerChar.transform.origin 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 pass
func check_island_overlap(new_island): func check_island_location_valid(new_island):
var islands = Islands.get_children() var islands = Islands.get_children()
for island in islands: for island in islands:
@ -59,6 +70,15 @@ func check_island_overlap(new_island):
return false 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): func add_island_at(file_name, offset_world: Vector2):
var island = Island.new() var island = Island.new()
island.load_island(file_name) island.load_island(file_name)
@ -68,6 +88,12 @@ func add_island_at(file_name, offset_world: Vector2):
func populate_ocean_grid(): func populate_ocean_grid():
Globals.OceanGrid.remove_obstacles(Globals.OceanGrid.get_obstacles()) 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(): func generate():
@ -88,19 +114,19 @@ func generate():
var rand_coord = Vector2(rng.randi_range(-radius, radius), rng.randi_range(-radius, radius)) var rand_coord = Vector2(rng.randi_range(-radius, radius), rng.randi_range(-radius, radius))
island.offset_world = Globals.WorldToHexCenter(rand_coord) 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_num = 0
var overlap_retry_max = 10 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: if overlap_retry_num % 4 == 0:
radius = radius + 500 radius = radius + 500
overlap_retry_num = overlap_retry_num + 1 overlap_retry_num = overlap_retry_num + 1
rand_coord = Vector2(rng.randi_range(-radius, radius), rng.randi_range(-radius, radius)) rand_coord = Vector2(rng.randi_range(-radius, radius), rng.randi_range(-radius, radius))
island.offset_world = Globals.WorldToHexCenter(rand_coord) 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!") print ("Could not place island!")
else: else:
print ("Placed after " + str(overlap_retry_num) + " retries.") print ("Placed after " + str(overlap_retry_num) + " retries.")
@ -109,13 +135,26 @@ func generate():
populate_ocean_grid() 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): func handle_game_event(event):
if event is InputEventMouseButton: if event is InputEventMouseButton:
# Move main character # Move main character
if event.pressed and event.button_index == BUTTON_LEFT: if event.pressed and event.button_index == BUTTON_LEFT:
PlayerChar.target = Globals.HexGrid.get_hex_center(Globals.ScreenToHex(event.position, WorldCamera)) update_player_navigation_target (Globals.HexGrid.get_hex_center(Globals.ScreenToHex(event.position, WorldCamera)))
return true
return false return false
@ -142,11 +181,14 @@ func _unhandled_input(event):
hex_hover = Globals.ScreenToHex(event.position, WorldCamera) hex_hover = Globals.ScreenToHex(event.position, WorldCamera)
GridHighlight.pos = hex_hover GridHighlight.pos = hex_hover
HexCoordValueLabel.text = str(hex_hover) HexCoordValueLabel.text = str(hex_hover)
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"
# Islands.get_children()[1].offset_world = Globals.HexToWorld(hex_hover) WorldCoordValueLabel.text = str(world_coord)
# Islands.get_children()[1].update()
WorldCoordValueLabel.text = str(Globals.ScreenToWorld(event.position, WorldCamera))
if is_dragging: if is_dragging:
WorldCamera.offset = (drag_start - event.position) * WorldCamera.zoom.x WorldCamera.offset = (drag_start - event.position) * WorldCamera.zoom.x
@ -193,3 +235,14 @@ func load_world(path: String):
func _on_generate_button_pressed(): func _on_generate_button_pressed():
generate() 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