Rework player state system to use an event subscription system to avoid directly calling methods on individual state and having to worry about validity

This commit is contained in:
2026-04-01 11:11:02 -05:00
parent 7cd34cb07e
commit eabfeab91a
35 changed files with 882 additions and 2288 deletions

View File

@@ -1,30 +1,41 @@
extends BaseState
# walking_state.gd
signal StartedWalking
@export var walking_speed := 100
@export var direction_component: FacingDirectionComponent
@export var body: CharacterBody2D
var _movement_component: MovementComponent
var _queued_dv: Vector2
func GetStateEnum() -> PlayerStateMachine.States:
return PlayerStateMachine.States.WALKING
func _ready() -> void:
SubscribeToEvent("MovementQueued", OnMovementQueued)
SubscribeToEvent("NoMovementQueued", OnNoMovementQueued)
func Enter(_extra_parameters: Dictionary) -> void:
_movement_component = ComponentUtils.GetMovementComponent(body)
print("Walking State Entered")
_queued_dv = _extra_parameters["dv"]
StartedWalking.emit()
func Update(_delta: float) -> void:
var movement_vector := _movement_component.movement_vector
if movement_vector == Vector2.ZERO:
if _queued_dv == Vector2.ZERO:
state_machine.QueueStateChange(PlayerStateMachine.States.IDLE)
return
body.velocity = movement_vector * walking_speed
body.velocity = _queued_dv * walking_speed
body.move_and_slide()
direction_component.ChangeDirectionUsingMovementVector(movement_vector)
func OnMovementQueued(dv: Vector2) -> void:
_queued_dv = dv
func OnNoMovementQueued() -> void:
_queued_dv = Vector2.ZERO
func GetStateEnum() -> PlayerStateMachine.States:
return PlayerStateMachine.States.WALKING