Add static types in 2D Navigation AStar demo (#1008)

This commit is contained in:
Tyler Breisacher
2024-08-30 18:48:17 -07:00
committed by GitHub
parent 8ab921d5b4
commit fa6061c623
2 changed files with 15 additions and 13 deletions

View File

@@ -1,14 +1,16 @@
extends Node2D extends Node2D
const PathFindAStar = preload("./pathfind_astar.gd")
enum State { enum State {
IDLE, IDLE,
FOLLOW, FOLLOW,
} }
const MASS = 10.0 const MASS: float = 10.0
const ARRIVE_DISTANCE = 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 _state := State.IDLE
var _velocity := Vector2() var _velocity := Vector2()
@@ -17,7 +19,7 @@ var _click_position := Vector2()
var _path := PackedVector2Array() var _path := PackedVector2Array()
var _next_point := Vector2() var _next_point := Vector2()
@onready var _tile_map: TileMap = $"../TileMap" @onready var _tile_map: PathFindAStar = $"../TileMap"
func _ready() -> void: func _ready() -> void:
_change_state(State.IDLE) _change_state(State.IDLE)
@@ -27,7 +29,7 @@ func _process(_delta: float) -> void:
if _state != State.FOLLOW: if _state != State.FOLLOW:
return 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: if arrived_to_next_point:
_path.remove_at(0) _path.remove_at(0)
if _path.is_empty(): if _path.is_empty():
@@ -46,9 +48,9 @@ func _unhandled_input(event: InputEvent) -> void:
_change_state(State.FOLLOW) _change_state(State.FOLLOW)
func _move_to(local_position: Vector2) -> float: func _move_to(local_position: Vector2) -> bool:
var desired_velocity := (local_position - position).normalized() * speed var desired_velocity: Vector2 = (local_position - position).normalized() * speed
var steering := desired_velocity - _velocity var steering: Vector2 = desired_velocity - _velocity
_velocity += steering / MASS _velocity += steering / MASS
position += _velocity * get_process_delta_time() position += _velocity * get_process_delta_time()
rotation = _velocity.angle() rotation = _velocity.angle()

View File

@@ -7,7 +7,7 @@ enum Tile {
} }
const CELL_SIZE = Vector2i(64, 64) 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) const DRAW_COLOR = Color.WHITE * Color(1, 1, 1, 0.5)
# The object for pathfinding on 2D grids. # The object for pathfinding on 2D grids.
@@ -39,9 +39,9 @@ func _draw() -> void:
if _path.is_empty(): if _path.is_empty():
return return
var last_point := _path[0] var last_point: Vector2 = _path[0]
for index in range(1, len(_path)): 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_line(last_point, current_point, DRAW_COLOR, BASE_LINE_WIDTH, true)
draw_circle(current_point, BASE_LINE_WIDTH * 2.0, DRAW_COLOR) draw_circle(current_point, BASE_LINE_WIDTH * 2.0, DRAW_COLOR)
last_point = current_point last_point = current_point
@@ -51,8 +51,8 @@ func round_local_position(local_position: Vector2i) -> Vector2i:
return map_to_local(local_to_map(local_position)) return map_to_local(local_to_map(local_position))
func is_point_walkable(local_position: Vector2i) -> bool: func is_point_walkable(local_position: Vector2) -> bool:
var map_position := local_to_map(local_position) var map_position: Vector2i = local_to_map(local_position)
if _astar.is_in_boundsv(map_position): if _astar.is_in_boundsv(map_position):
return not _astar.is_point_solid(map_position) return not _astar.is_point_solid(map_position)
return false return false