Skip to content

Commit fa6061c

Browse files
authored
Add static types in 2D Navigation AStar demo (#1008)
1 parent 8ab921d commit fa6061c

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

2d/navigation_astar/character.gd

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
extends Node2D
22

3+
const PathFindAStar = preload("./pathfind_astar.gd")
4+
35
enum State {
46
IDLE,
57
FOLLOW,
68
}
79

8-
const MASS = 10.0
9-
const ARRIVE_DISTANCE = 10.0
10+
const MASS: float = 10.0
11+
const ARRIVE_DISTANCE: float = 10.0
1012

11-
@export_range(10, 500, 0.1, "or_greater") var speed := 200.0
13+
@export_range(10, 500, 0.1, "or_greater") var speed: float = 200.0
1214

1315
var _state := State.IDLE
1416
var _velocity := Vector2()
@@ -17,7 +19,7 @@ var _click_position := Vector2()
1719
var _path := PackedVector2Array()
1820
var _next_point := Vector2()
1921

20-
@onready var _tile_map: TileMap = $"../TileMap"
22+
@onready var _tile_map: PathFindAStar = $"../TileMap"
2123

2224
func _ready() -> void:
2325
_change_state(State.IDLE)
@@ -27,7 +29,7 @@ func _process(_delta: float) -> void:
2729
if _state != State.FOLLOW:
2830
return
2931

30-
var arrived_to_next_point := _move_to(_next_point)
32+
var arrived_to_next_point: bool = _move_to(_next_point)
3133
if arrived_to_next_point:
3234
_path.remove_at(0)
3335
if _path.is_empty():
@@ -46,9 +48,9 @@ func _unhandled_input(event: InputEvent) -> void:
4648
_change_state(State.FOLLOW)
4749

4850

49-
func _move_to(local_position: Vector2) -> float:
50-
var desired_velocity := (local_position - position).normalized() * speed
51-
var steering := desired_velocity - _velocity
51+
func _move_to(local_position: Vector2) -> bool:
52+
var desired_velocity: Vector2 = (local_position - position).normalized() * speed
53+
var steering: Vector2 = desired_velocity - _velocity
5254
_velocity += steering / MASS
5355
position += _velocity * get_process_delta_time()
5456
rotation = _velocity.angle()

2d/navigation_astar/pathfind_astar.gd

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ enum Tile {
77
}
88

99
const CELL_SIZE = Vector2i(64, 64)
10-
const BASE_LINE_WIDTH = 3.0
10+
const BASE_LINE_WIDTH: float = 3.0
1111
const DRAW_COLOR = Color.WHITE * Color(1, 1, 1, 0.5)
1212

1313
# The object for pathfinding on 2D grids.
@@ -39,9 +39,9 @@ func _draw() -> void:
3939
if _path.is_empty():
4040
return
4141

42-
var last_point := _path[0]
42+
var last_point: Vector2 = _path[0]
4343
for index in range(1, len(_path)):
44-
var current_point := _path[index]
44+
var current_point: Vector2 = _path[index]
4545
draw_line(last_point, current_point, DRAW_COLOR, BASE_LINE_WIDTH, true)
4646
draw_circle(current_point, BASE_LINE_WIDTH * 2.0, DRAW_COLOR)
4747
last_point = current_point
@@ -51,8 +51,8 @@ func round_local_position(local_position: Vector2i) -> Vector2i:
5151
return map_to_local(local_to_map(local_position))
5252

5353

54-
func is_point_walkable(local_position: Vector2i) -> bool:
55-
var map_position := local_to_map(local_position)
54+
func is_point_walkable(local_position: Vector2) -> bool:
55+
var map_position: Vector2i = local_to_map(local_position)
5656
if _astar.is_in_boundsv(map_position):
5757
return not _astar.is_point_solid(map_position)
5858
return false

0 commit comments

Comments
 (0)