feat: Enhance player movement and turn management system

This commit is contained in:
2026-03-20 20:14:21 +11:00
parent 5ab233b00c
commit 00af585cf4
3 changed files with 40 additions and 11 deletions
+1 -1
View File
@@ -1,3 +1,3 @@
{ {
"godotTools.editorPath.godot4": "/home/stefwill/Applications/Godot/Godot_v4.6.1-stable_linux.x86_64" "godotTools.editorPath.godot4": "/home/stefwill/.local/share/godot/godot"
} }
+38 -10
View File
@@ -1,5 +1,5 @@
## player.gd ## player.gd
# Player character script. Handles input, pathfinding, and movement. # Player character script. Handles input, pathfinding, and turn-based movement.
class_name Player class_name Player
extends Character extends Character
@@ -8,13 +8,28 @@ const SPEED: float = 250.0
# Distance threshold for snapping to a tile (prevents jitter). # Distance threshold for snapping to a tile (prevents jitter).
const ARRIVAL_THRESHOLD: float = 4.0 const ARRIVAL_THRESHOLD: float = 4.0
# The queued path (tile coordinates) the player will walk along.
# The current path (as a list of tile coordinates) the player is following.
var _path: Array[Vector2i] = [] var _path: Array[Vector2i] = []
# True when the TurnManager is waiting for the player to act.
var _awaiting_input: bool = false
# True while the player is animating toward _move_target.
var _is_moving: bool = false
# World position of the tile the player is currently stepping toward.
var _move_target: Vector2 = Vector2.ZERO
# Handles mouse input for movement. Calculates a path to the clicked tile. # Called by TurnManager at the start of the player's turn.
func take_turn() -> void:
if _path.is_empty():
_awaiting_input = true
else:
_start_step()
# Handles mouse input for movement. Only active while awaiting input.
func _unhandled_input(event: InputEvent) -> void: func _unhandled_input(event: InputEvent) -> void:
if not _awaiting_input:
return
if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT and event.pressed: if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT and event.pressed:
var clicked_tile: Vector2i = LevelManager.world_to_tile(get_global_mouse_position()) var clicked_tile: Vector2i = LevelManager.world_to_tile(get_global_mouse_position())
var current_tile: Vector2i = LevelManager.world_to_tile(position) var current_tile: Vector2i = LevelManager.world_to_tile(position)
@@ -22,20 +37,33 @@ func _unhandled_input(event: InputEvent) -> void:
# Drop the first entry — it's the tile the player is already on. # Drop the first entry — it's the tile the player is already on.
if not _path.is_empty(): if not _path.is_empty():
_path.remove_at(0) _path.remove_at(0)
if not _path.is_empty():
_start_step()
# Moves the player along the path, one tile at a time, using velocity.
# Begins movement toward the next tile in the queued path.
func _start_step() -> void:
_awaiting_input = false
_is_moving = true
var next_tile: Vector2i = _path.pop_front()
_move_target = LevelManager.tile_to_world(next_tile)
# Moves the player toward _move_target each frame. Emits turn_finished on arrival.
func _physics_process(_delta: float) -> void: func _physics_process(_delta: float) -> void:
if _path.is_empty(): if not _is_moving:
velocity = Vector2.ZERO velocity = Vector2.ZERO
move_and_slide() move_and_slide()
return return
var target_world: Vector2 = LevelManager.tile_to_world(_path[0]) var to_target: Vector2 = _move_target - position
var to_target: Vector2 = target_world - position
if to_target.length() < ARRIVAL_THRESHOLD: if to_target.length() < ARRIVAL_THRESHOLD:
position = target_world position = _move_target
_path.remove_at(0) velocity = Vector2.ZERO
move_and_slide()
_is_moving = false
turn_finished.emit()
else: else:
velocity = to_target.normalized() * SPEED velocity = to_target.normalized() * SPEED
move_and_slide() move_and_slide()
+1
View File
@@ -28,3 +28,4 @@ func _initialize_map() -> void:
player = player_scene.instantiate() as Player player = player_scene.instantiate() as Player
add_child(player) add_child(player)
LevelManager.initialize(settings, map_layer, player) LevelManager.initialize(settings, map_layer, player)
TurnManager.start_turns()