Skip to content

Commit 8837500

Browse files
Format corrections
Co-authored-by: A Thousand Ships <[email protected]>
1 parent da5562a commit 8837500

File tree

4 files changed

+47
-44
lines changed

4 files changed

+47
-44
lines changed

2d/navigation_astar_hexagonal/astar_hex_2d.gd

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ var map: TileMap
55

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

1414
# Euclidian distance heuristic would not work on hexagonal map with global position because

2d/navigation_astar_hexagonal/debug/debug_astar.gd

+31-32
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ extends Node2D
66

77
const BASE_LINE_WIDTH = 3.0
88
const DRAW_COLOR:Color = Color.WHITE
9-
const OFFSET_POSITIONS = Vector2(10,30)
10-
const OFFSET_WEIGHT = Vector2(10,-10)
9+
const OFFSET_POSITIONS = Vector2(10, 30)
10+
const OFFSET_WEIGHT = Vector2(10, -10)
1111

1212
var _debug_connections = false
1313
var _debug_position = false
@@ -18,27 +18,26 @@ var _debug_path = true
1818

1919
func _process(delta):
2020
queue_redraw()
21-
pass
2221

2322

2423
func draw_arrow(src, dst, color, width, aa = true):
2524
var angle = 0.6
2625
var size_head = 20
2726
var head : Vector2 = (dst - src).normalized() * size_head
28-
draw_line(src, dst-head/2, color, width, aa)
29-
draw_polygon([dst, dst - head.rotated(angle), dst - head.rotated(-angle) ], [color,color,color ])
27+
draw_line(src, dst-head / 2, color, width, aa)
28+
draw_polygon([ dst, dst - head.rotated(angle), dst - head.rotated(-angle) ], [ color, color, color ])
3029

3130

3231
func _draw():
33-
if _debug_connections :
32+
if _debug_connections:
3433
_draw_connections()
35-
if _debug_position :
34+
if _debug_position:
3635
_draw_positions()
37-
if _debug_weights :
36+
if _debug_weights:
3837
_draw_weights()
39-
if _debug_costs :
38+
if _debug_costs:
4039
_draw_costs()
41-
if _debug_path :
40+
if _debug_path:
4241
_draw_path()
4342

4443

@@ -61,13 +60,13 @@ func _draw_weights():
6160
var position_weight = map.astar_node.get_point_position(id)
6261
var cost = map.astar_node.get_point_weight_scale(id)
6362
draw_string(
64-
font,
65-
position_weight + OFFSET_WEIGHT,
66-
str(cost),
67-
HORIZONTAL_ALIGNMENT_FILL,
68-
-1,
69-
16,
70-
Color.RED
63+
font,
64+
position_weight + OFFSET_WEIGHT,
65+
str(cost),
66+
HORIZONTAL_ALIGNMENT_FILL,
67+
-1,
68+
16,
69+
Color.RED
7170
)
7271

7372

@@ -76,13 +75,13 @@ func _draw_positions():
7675
var position_label = map.astar_node.get_point_position(id)
7776
var position_map = map.local_to_map(map.to_local(map.astar_node.get_point_position(id)))
7877
draw_string(
79-
font,
80-
position_label + OFFSET_POSITIONS,
81-
str(position_map),
82-
HORIZONTAL_ALIGNMENT_FILL,
83-
-1,
84-
16,
85-
Color.RED
78+
font,
79+
position_label + OFFSET_POSITIONS,
80+
str(position_map),
81+
HORIZONTAL_ALIGNMENT_FILL,
82+
-1,
83+
16,
84+
Color.RED
8685
)
8786

8887

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

9796

9897
func _draw_costs():
@@ -102,13 +101,13 @@ func _draw_costs():
102101
var position_cost_end = map.astar_node.get_point_position(id_con)
103102
var cost = map.astar_node._compute_cost(id, id_con)
104103
draw_string(
105-
font,
106-
(position_cost_start+position_cost_end)/2.0,
107-
str("%.2f"%cost),
108-
HORIZONTAL_ALIGNMENT_CENTER,
109-
-1,
110-
16,
111-
Color.PINK
104+
font,
105+
(position_cost_start + position_cost_end) / 2.0,
106+
str("%.2f" % cost),
107+
HORIZONTAL_ALIGNMENT_CENTER,
108+
-1,
109+
16,
110+
Color.PINK
112111
)
113112

114113

2d/navigation_astar_hexagonal/map.gd

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
class_name Map
22
extends TileMap
33

4-
# In order to have cost function control
4+
# In order to have cost function control.
55
@onready var astar_node = AStarHex2D.new()
66

77
# Private variable to not trigger on same previous paths request
8-
var _path_start_coords: Vector2i = Vector2i()
9-
var _path_end_coords: Vector2i = Vector2i()
8+
var _path_start_coords: Vector2i
9+
var _path_end_coords: Vector2i
1010
# Used for debug
1111
var _point_path = []
1212

@@ -16,6 +16,7 @@ func _ready():
1616
var walkable_cells_list = astar_add_walkable_cells()
1717
astar_connect_walkable_cells(walkable_cells_list)
1818

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

3334
# We could also disable point after having created it (for runtime modification for extent)
34-
if(not tile_data or tile_data.get_custom_data("IsObstacle")):
35+
if not tile_data or tile_data.get_custom_data("IsObstacle"):
3536
continue
3637

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

4041
return cell_array
4142

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

55+
5356
# Getter of astar result in world coordinates
5457
func get_astar_path(world_start, world_end):
5558
var path_world = []
@@ -60,7 +63,7 @@ func get_astar_path(world_start, world_end):
6063
_path_start_coords = start_coords
6164
_path_end_coords = end_coords
6265
for point in _recalculate_path():
63-
var point_world = point
66+
var point_world = point
6467
path_world.append(point_world)
6568

6669
return path_world
@@ -70,7 +73,8 @@ func _recalculate_path():
7073
_point_path = []
7174
var start_point_index = hash(_path_start_coords)
7275
var end_point_index = hash(_path_end_coords)
73-
if(astar_node.has_point(start_point_index) and astar_node.has_point(end_point_index)):
76+
if astar_node.has_point(start_point_index) and astar_node.has_point(end_point_index):
7477
_point_path = astar_node.get_point_path(start_point_index, end_point_index)
78+
7579
return _point_path
7680

2d/navigation_astar_hexagonal/player.gd

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
extends CharacterBody2D
22

33
@export var map: Map
4-
@export var speed = 400 # move speed in pixels/sec
4+
@export var speed = 400 # Move speed in pixels/sec.
55

66
var _path_to_target:Array
77
var _target = null
88

99

1010
func _input(event):
11-
if event.is_action_pressed("click"):
11+
if event.is_action_pressed(&"click"):
1212
_path_to_target = map.get_astar_path( global_position, get_global_mouse_position())
13-
if(not _path_to_target.is_empty()):
13+
if not _path_to_target.is_empty():
1414
global_position = _path_to_target.pop_front()
1515
_target = null
1616

0 commit comments

Comments
 (0)