add match state helper function to player

This commit is contained in:
yuki 2025-11-22 11:38:03 -03:00
parent 66039b70d9
commit c1de69a1c0
2 changed files with 19 additions and 1 deletions

View file

@ -12,7 +12,7 @@ func _ready() -> void:
assert(player != null, "The PlayerState state type must be used only in the player scene. It needs the owner to be a Player node.")
func _enter(_previous_state_path: String, _data: Dictionary = {}) -> void:
func _enter(_previous_state_path: String, _data: Dictionary[StringName, Variant] = {}) -> void:
pass

View file

@ -1,10 +1,28 @@
class_name Player extends CharacterBody2D
const DEADZONE = 0.1
@export var max_speed: float = 300.0
@onready var sprite: AnimatedSprite2D = $AnimatedSprite2D
@onready var state_machine: StateMachine = $StateMachine
func get_movement_vector() -> Vector2:
var x_mov: float = Input.get_action_strength('move_right') - Input.get_action_strength('move_left')
var y_mov: float = Input.get_action_strength('move_down') - Input.get_action_strength('move_up')
return Vector2(x_mov, y_mov)
func match_state(exclude: Array[StringName] = []) -> StringName:
var movement_vector: Vector2 = get_movement_vector()
if movement_vector.length() > DEADZONE:
if PlayerState.WALKING in exclude: return ""
return PlayerState.WALKING
elif movement_vector.length() < DEADZONE:
if PlayerState.IDLE in exclude: return ""
return PlayerState.IDLE
return ""