Path planning on ocean good
parent
cf7a62d89b
commit
a5035f81fa
16
Globals.gd
16
Globals.gd
|
@ -8,6 +8,7 @@ const SHOVEL_DURATION=1
|
|||
|
||||
var DebugLabel = null
|
||||
var HexGrid = null
|
||||
var HexCell = null
|
||||
var OceanGrid = null
|
||||
|
||||
var hex_size = 128
|
||||
|
@ -20,6 +21,8 @@ func _ready():
|
|||
OceanGrid = preload("../thirdparty/gdhexgrid/HexGrid.gd").new()
|
||||
OceanGrid.hex_scale = Vector2(hex_size, hex_size)
|
||||
|
||||
HexCell = preload("../thirdparty/gdhexgrid/HexCell.gd").new()
|
||||
|
||||
HexTileDrawer.hex_scale = Vector2(hex_size, hex_size)
|
||||
|
||||
|
||||
|
@ -60,3 +63,16 @@ func WorldToHexCenter(pos: Vector2):
|
|||
|
||||
func WorldToScreen(pos: Vector2, camera: Camera2D):
|
||||
return pos * camera.zoom + camera.offset
|
||||
|
||||
|
||||
func WorldLineToHexTiles (start: Vector2, target: Vector2):
|
||||
var start_cell = HexCell.new_hex(WorldToHex(start))
|
||||
var target_coord = WorldToHex(target)
|
||||
var path_cells = start_cell.line_to(target_coord)
|
||||
|
||||
var path = []
|
||||
for cell in path_cells:
|
||||
path.append(HexToWorld(cell.axial_coords))
|
||||
|
||||
return path
|
||||
|
||||
|
|
|
@ -127,8 +127,8 @@ func draw_bsphere():
|
|||
draw_circle(center_world_coord, radius_world, Color("#80000033"))
|
||||
draw_circle(center_world_coord, 10, Color.red)
|
||||
|
||||
|
||||
func _draw():
|
||||
print (name, offset_world)
|
||||
for coord in tiles.keys():
|
||||
draw_set_transform (coord + offset_world, 0, Vector2.ONE)
|
||||
draw_polygon(HexTileDrawer.HexPoints, HexTileDrawer.get_tile_color(tiles[coord]))
|
||||
|
|
|
@ -27,6 +27,7 @@ var target = Vector2()
|
|||
var tile_data = {}
|
||||
var current_island = null
|
||||
var player_navigation_path = []
|
||||
var hex_line_path = []
|
||||
|
||||
|
||||
#
|
||||
|
@ -55,17 +56,21 @@ 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")
|
||||
func draw_hex_path (path: Array, color: Color):
|
||||
var path_len = len(path)
|
||||
if path_len > 0:
|
||||
var last_point = path[0]
|
||||
draw_circle(last_point, 5, color)
|
||||
for i in range (1, path_len):
|
||||
var cur_point = path[i]
|
||||
draw_line (last_point, cur_point, color)
|
||||
draw_circle(cur_point, 5, color)
|
||||
last_point = cur_point
|
||||
|
||||
func _draw():
|
||||
draw_hex_path (player_navigation_path, "#f200f2")
|
||||
draw_hex_path (hex_line_path, "#00f2f2")
|
||||
|
||||
|
||||
#
|
||||
# World Modification/Query
|
||||
|
@ -146,6 +151,13 @@ func generate():
|
|||
|
||||
populate_ocean_grid()
|
||||
|
||||
# var island = Islands.get_children()[0]
|
||||
# var player_pos = island.center_world_coord + island.offset_world
|
||||
# PlayerChar.position = player_pos
|
||||
# PlayerChar.target = player_pos
|
||||
# PlayerChar.update()
|
||||
# print (player_pos)
|
||||
|
||||
#
|
||||
# Navigation
|
||||
#
|
||||
|
@ -157,15 +169,29 @@ func populate_ocean_grid():
|
|||
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)
|
||||
Globals.OceanGrid.add_obstacles(grid_coords, 0)
|
||||
|
||||
|
||||
func update_player_navigation_target(target_world: Vector2):
|
||||
var start_coord = Globals.WorldToHex(PlayerChar.transform.origin)
|
||||
player_navigation_path = []
|
||||
|
||||
var start_world = PlayerChar.transform.origin
|
||||
var start_coord = Globals.WorldToHex(start_world)
|
||||
var goal_coord = Globals.WorldToHex(target_world)
|
||||
|
||||
if get_tile_type(target_world) != null:
|
||||
var direct_path = Globals.WorldLineToHexTiles(target_world, start_world)
|
||||
while len(direct_path) > 0 and get_tile_type(direct_path[0]) != null:
|
||||
direct_path.remove(0)
|
||||
|
||||
if len(direct_path) == 0:
|
||||
print ("Could not find path!")
|
||||
return
|
||||
|
||||
print ("Using ", direct_path[0], " instead of ", goal_coord, " as goal.")
|
||||
goal_coord = Globals.WorldToHex(direct_path[0])
|
||||
|
||||
var path = Globals.OceanGrid.find_path(start_coord, goal_coord)
|
||||
player_navigation_path = []
|
||||
for target in path:
|
||||
var target_world_coord = Globals.HexToWorld(target.axial_coords)
|
||||
var target_type = get_tile_type(target_world_coord)
|
||||
|
@ -196,6 +222,12 @@ func handle_game_event(event):
|
|||
return false
|
||||
|
||||
|
||||
func update_hex_line_path(target: Vector2):
|
||||
var start = PlayerChar.position
|
||||
|
||||
hex_line_path = Globals.WorldLineToHexTiles(start, target)
|
||||
|
||||
|
||||
func _unhandled_input(event):
|
||||
if event is InputEventMouseButton:
|
||||
if handle_game_event(event):
|
||||
|
@ -227,6 +259,8 @@ func _unhandled_input(event):
|
|||
|
||||
WorldCoordValueLabel.text = str(world_coord)
|
||||
|
||||
update_hex_line_path(world_coord)
|
||||
|
||||
if is_dragging:
|
||||
WorldCamera.offset = (drag_start - event.position) * WorldCamera.zoom.x
|
||||
|
||||
|
|
Loading…
Reference in New Issue