Skip to content

Commit

Permalink
Add static types in 2D Navigation AStar demo (#1008)
Browse files Browse the repository at this point in the history
  • Loading branch information
MatrixFrog authored Aug 31, 2024
1 parent 8ab921d commit fa6061c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
18 changes: 10 additions & 8 deletions 2d/navigation_astar/character.gd
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
extends Node2D

const PathFindAStar = preload("./pathfind_astar.gd")

enum State {
IDLE,
FOLLOW,
}

const MASS = 10.0
const ARRIVE_DISTANCE = 10.0
const MASS: float = 10.0
const ARRIVE_DISTANCE: float = 10.0

@export_range(10, 500, 0.1, "or_greater") var speed := 200.0
@export_range(10, 500, 0.1, "or_greater") var speed: float = 200.0

var _state := State.IDLE
var _velocity := Vector2()
Expand All @@ -17,7 +19,7 @@ var _click_position := Vector2()
var _path := PackedVector2Array()
var _next_point := Vector2()

@onready var _tile_map: TileMap = $"../TileMap"
@onready var _tile_map: PathFindAStar = $"../TileMap"

func _ready() -> void:
_change_state(State.IDLE)
Expand All @@ -27,7 +29,7 @@ func _process(_delta: float) -> void:
if _state != State.FOLLOW:
return

var arrived_to_next_point := _move_to(_next_point)
var arrived_to_next_point: bool = _move_to(_next_point)
if arrived_to_next_point:
_path.remove_at(0)
if _path.is_empty():
Expand All @@ -46,9 +48,9 @@ func _unhandled_input(event: InputEvent) -> void:
_change_state(State.FOLLOW)


func _move_to(local_position: Vector2) -> float:
var desired_velocity := (local_position - position).normalized() * speed
var steering := desired_velocity - _velocity
func _move_to(local_position: Vector2) -> bool:
var desired_velocity: Vector2 = (local_position - position).normalized() * speed
var steering: Vector2 = desired_velocity - _velocity
_velocity += steering / MASS
position += _velocity * get_process_delta_time()
rotation = _velocity.angle()
Expand Down
10 changes: 5 additions & 5 deletions 2d/navigation_astar/pathfind_astar.gd
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ enum Tile {
}

const CELL_SIZE = Vector2i(64, 64)
const BASE_LINE_WIDTH = 3.0
const BASE_LINE_WIDTH: float = 3.0
const DRAW_COLOR = Color.WHITE * Color(1, 1, 1, 0.5)

# The object for pathfinding on 2D grids.
Expand Down Expand Up @@ -39,9 +39,9 @@ func _draw() -> void:
if _path.is_empty():
return

var last_point := _path[0]
var last_point: Vector2 = _path[0]
for index in range(1, len(_path)):
var current_point := _path[index]
var current_point: Vector2 = _path[index]
draw_line(last_point, current_point, DRAW_COLOR, BASE_LINE_WIDTH, true)
draw_circle(current_point, BASE_LINE_WIDTH * 2.0, DRAW_COLOR)
last_point = current_point
Expand All @@ -51,8 +51,8 @@ func round_local_position(local_position: Vector2i) -> Vector2i:
return map_to_local(local_to_map(local_position))


func is_point_walkable(local_position: Vector2i) -> bool:
var map_position := local_to_map(local_position)
func is_point_walkable(local_position: Vector2) -> bool:
var map_position: Vector2i = local_to_map(local_position)
if _astar.is_in_boundsv(map_position):
return not _astar.is_point_solid(map_position)
return false
Expand Down

0 comments on commit fa6061c

Please sign in to comment.