Skip to content

Commit

Permalink
Format corrections
Browse files Browse the repository at this point in the history
Co-authored-by: A Thousand Ships <[email protected]>
  • Loading branch information
m21-cerutti and AThousandShips authored Jul 13, 2024
1 parent da5562a commit 8837500
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 44 deletions.
6 changes: 3 additions & 3 deletions 2d/navigation_astar_hexagonal/astar_hex_2d.gd
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ var map: TileMap

# Final cost used for pathfinding would be weight * cost
# See https://docs.godotengine.org/fr/4.x/classes/class_astar3d.html#class-astar3d
func _compute_cost( from_id:int, to_id:int ):
func _compute_cost(from_id: int, to_id: int):
return 1
func _estimate_cost( from_id:int, to_id:int ):

func _estimate_cost(from_id: int, to_id: int):
return 1

# Euclidian distance heuristic would not work on hexagonal map with global position because
Expand Down
63 changes: 31 additions & 32 deletions 2d/navigation_astar_hexagonal/debug/debug_astar.gd
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ extends Node2D

const BASE_LINE_WIDTH = 3.0
const DRAW_COLOR:Color = Color.WHITE
const OFFSET_POSITIONS = Vector2(10,30)
const OFFSET_WEIGHT = Vector2(10,-10)
const OFFSET_POSITIONS = Vector2(10, 30)
const OFFSET_WEIGHT = Vector2(10, -10)

var _debug_connections = false
var _debug_position = false
Expand All @@ -18,27 +18,26 @@ var _debug_path = true

func _process(delta):
queue_redraw()
pass


func draw_arrow(src, dst, color, width, aa = true):
var angle = 0.6
var size_head = 20
var head : Vector2 = (dst - src).normalized() * size_head
draw_line(src, dst-head/2, color, width, aa)
draw_polygon([dst, dst - head.rotated(angle), dst - head.rotated(-angle) ], [color,color,color ])
draw_line(src, dst-head / 2, color, width, aa)
draw_polygon([ dst, dst - head.rotated(angle), dst - head.rotated(-angle) ], [ color, color, color ])


func _draw():
if _debug_connections :
if _debug_connections:
_draw_connections()
if _debug_position :
if _debug_position:
_draw_positions()
if _debug_weights :
if _debug_weights:
_draw_weights()
if _debug_costs :
if _debug_costs:
_draw_costs()
if _debug_path :
if _debug_path:
_draw_path()


Expand All @@ -61,13 +60,13 @@ func _draw_weights():
var position_weight = map.astar_node.get_point_position(id)
var cost = map.astar_node.get_point_weight_scale(id)
draw_string(
font,
position_weight + OFFSET_WEIGHT,
str(cost),
HORIZONTAL_ALIGNMENT_FILL,
-1,
16,
Color.RED
font,
position_weight + OFFSET_WEIGHT,
str(cost),
HORIZONTAL_ALIGNMENT_FILL,
-1,
16,
Color.RED
)


Expand All @@ -76,13 +75,13 @@ func _draw_positions():
var position_label = map.astar_node.get_point_position(id)
var position_map = map.local_to_map(map.to_local(map.astar_node.get_point_position(id)))
draw_string(
font,
position_label + OFFSET_POSITIONS,
str(position_map),
HORIZONTAL_ALIGNMENT_FILL,
-1,
16,
Color.RED
font,
position_label + OFFSET_POSITIONS,
str(position_map),
HORIZONTAL_ALIGNMENT_FILL,
-1,
16,
Color.RED
)


Expand All @@ -92,7 +91,7 @@ func _draw_connections():
var position_start = map.astar_node.get_point_position(id)
var position_end = map.astar_node.get_point_position(id_con)
var direction = (position_end - position_start)
draw_arrow(position_start, position_end - direction/4.0, Color(0.0, 1.0, 1.0, 1.0), BASE_LINE_WIDTH*2, true)
draw_arrow(position_start, position_end - direction / 4.0, Color(0.0, 1.0, 1.0, 1.0), BASE_LINE_WIDTH * 2, true)


func _draw_costs():
Expand All @@ -102,13 +101,13 @@ func _draw_costs():
var position_cost_end = map.astar_node.get_point_position(id_con)
var cost = map.astar_node._compute_cost(id, id_con)
draw_string(
font,
(position_cost_start+position_cost_end)/2.0,
str("%.2f"%cost),
HORIZONTAL_ALIGNMENT_CENTER,
-1,
16,
Color.PINK
font,
(position_cost_start + position_cost_end) / 2.0,
str("%.2f" % cost),
HORIZONTAL_ALIGNMENT_CENTER,
-1,
16,
Color.PINK
)


Expand Down
16 changes: 10 additions & 6 deletions 2d/navigation_astar_hexagonal/map.gd
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
class_name Map
extends TileMap

# In order to have cost function control
# In order to have cost function control.
@onready var astar_node = AStarHex2D.new()

# Private variable to not trigger on same previous paths request
var _path_start_coords: Vector2i = Vector2i()
var _path_end_coords: Vector2i = Vector2i()
var _path_start_coords: Vector2i
var _path_end_coords: Vector2i
# Used for debug
var _point_path = []

Expand All @@ -16,6 +16,7 @@ func _ready():
var walkable_cells_list = astar_add_walkable_cells()
astar_connect_walkable_cells(walkable_cells_list)


# Need to create first astar nodes, otherwise would need to handle connections on not yet created nodes
# here use tilemap as source of truth (with IsObstacle and Cost custom data)
func astar_add_walkable_cells():
Expand All @@ -31,14 +32,15 @@ func astar_add_walkable_cells():
var tile_data:TileData = get_cell_tile_data(0, coords)

# We could also disable point after having created it (for runtime modification for extent)
if(not tile_data or tile_data.get_custom_data("IsObstacle")):
if not tile_data or tile_data.get_custom_data("IsObstacle"):
continue

astar_node.add_point(id, point, tile_data.get_custom_data("Cost"))
cell_array.append(id)

return cell_array


# Create connections by using Tilemap get_surrounding_cells
# would work even when changing coordinate system
func astar_connect_walkable_cells(cell_array):
Expand All @@ -50,6 +52,7 @@ func astar_connect_walkable_cells(cell_array):
if astar_node.has_point(neighbor_id):
astar_node.connect_points(id, neighbor_id, false)


# Getter of astar result in world coordinates
func get_astar_path(world_start, world_end):
var path_world = []
Expand All @@ -60,7 +63,7 @@ func get_astar_path(world_start, world_end):
_path_start_coords = start_coords
_path_end_coords = end_coords
for point in _recalculate_path():
var point_world = point
var point_world = point
path_world.append(point_world)

return path_world
Expand All @@ -70,7 +73,8 @@ func _recalculate_path():
_point_path = []
var start_point_index = hash(_path_start_coords)
var end_point_index = hash(_path_end_coords)
if(astar_node.has_point(start_point_index) and astar_node.has_point(end_point_index)):
if astar_node.has_point(start_point_index) and astar_node.has_point(end_point_index):
_point_path = astar_node.get_point_path(start_point_index, end_point_index)

return _point_path

6 changes: 3 additions & 3 deletions 2d/navigation_astar_hexagonal/player.gd
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
extends CharacterBody2D

@export var map: Map
@export var speed = 400 # move speed in pixels/sec
@export var speed = 400 # Move speed in pixels/sec.

var _path_to_target:Array
var _target = null


func _input(event):
if event.is_action_pressed("click"):
if event.is_action_pressed(&"click"):
_path_to_target = map.get_astar_path( global_position, get_global_mouse_position())
if(not _path_to_target.is_empty()):
if not _path_to_target.is_empty():
global_position = _path_to_target.pop_front()
_target = null

Expand Down

0 comments on commit 8837500

Please sign in to comment.