Skip to content

Commit

Permalink
A few more fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
MatrixFrog committed Jan 8, 2024
1 parent 91012d9 commit 5d5f542
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
14 changes: 8 additions & 6 deletions 2d/navigation_astar/character.gd
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
extends Node2D

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

enum State { IDLE, FOLLOW }

const MASS = 10.0
Expand All @@ -16,14 +18,14 @@ var _click_position := Vector2()
var _path := PackedVector2Array()
var _next_point := Vector2()

func _ready():
func _ready() -> void:
_change_state(State.IDLE)


func _process(_delta):
func _process(_delta: float) -> void:
if _state != State.FOLLOW:
return
var arrived_to_next_point = _move_to(_next_point)
var arrived_to_next_point := _move_to(_next_point)
if arrived_to_next_point:
_path.remove_at(0)
if _path.is_empty():
Expand All @@ -32,7 +34,7 @@ func _process(_delta):
_next_point = _path[0]


func _unhandled_input(event: InputEvent):
func _unhandled_input(event: InputEvent) -> void:
_click_position = get_global_mouse_position()
if _tile_map.is_point_walkable(_click_position):
if event.is_action_pressed(&"teleport_to", false, true):
Expand All @@ -42,7 +44,7 @@ func _unhandled_input(event: InputEvent):
_change_state(State.FOLLOW)


func _move_to(local_position: Vector2):
func _move_to(local_position: Vector2) -> bool:
var desired_velocity := (local_position - position).normalized() * speed
var steering := desired_velocity - _velocity
_velocity += steering / MASS
Expand All @@ -51,7 +53,7 @@ func _move_to(local_position: Vector2):
return position.distance_to(local_position) < ARRIVE_DISTANCE


func _change_state(new_state: State):
func _change_state(new_state: State) -> void:
if new_state == State.IDLE:
_tile_map.clear_path()
elif new_state == State.FOLLOW:
Expand Down
9 changes: 4 additions & 5 deletions 2d/navigation_astar/pathfind_astar.gd
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
extends TileMap
class_name PathFindAStar

enum Tile { OBSTACLE, START_POINT, END_POINT }

Expand All @@ -14,7 +13,7 @@ var _start_point := Vector2i()
var _end_point := Vector2i()
var _path := PackedVector2Array()

func _ready():
func _ready() -> void:
_astar.region = get_used_rect()
_astar.cell_size = CELL_SIZE
_astar.offset = CELL_SIZE * 0.5
Expand All @@ -30,13 +29,13 @@ func _ready():
_astar.set_point_solid(pos)


func _draw():
func _draw() -> void:
if _path.is_empty():
return

var last_point = _path[0]
var last_point := _path[0]
for index in range(1, len(_path)):
var current_point = _path[index]
var current_point := _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 Down

0 comments on commit 5d5f542

Please sign in to comment.