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,4 +1,4 @@
extends Node2D
extends AnimatedSprite2D
class_name PlayerSprite
# Signals
@@ -6,37 +6,11 @@ signal DrawingBowAnimationFinished
signal FiringArrowAnimationFinished
signal AnimationFinished(animation_name: String)
# Exports
@export var state_machine: PlayerStateMachine
@export var direction_component: FacingDirectionComponent
# OnReady Variables
@onready var full: AnimatedSprite2D = $Full
# Private Variables
var _all_parts: Array[AnimatedSprite2D]
var _current_animation := "idle-down"
var _is_flipped := false
# Public Methods
func UpdateSprite() -> void:
var current_state := state_machine.current_state
var current_direction := direction_component.GetCurrentDirection()
var animation := _build_animation_name(current_state, current_direction)
var should_flip := _should_flip_horizontal()
if animation == _current_animation and should_flip == _is_flipped:
return
_current_animation = animation
_is_flipped = should_flip
for part in _all_parts:
part.animation = animation
part.play()
part.flip_h = should_flip
func OnAnimationFinished() -> void:
if _current_animation.begins_with("drawing-bow-"):
DrawingBowAnimationFinished.emit()
@@ -47,18 +21,8 @@ func OnAnimationFinished() -> void:
func PlaySpecifiedAnimation(animation_name: String) -> void:
_current_animation = animation_name
for part in _all_parts:
part.animation = animation_name
part.play()
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
_all_parts = [
full
]
UpdateSprite()
animation = animation_name
play()
func _build_animation_name(state: BaseState, direction: Enums.Directions) -> String:
@@ -72,7 +36,3 @@ func _direction_to_animation_state(direction: Enums.Directions) -> String:
if direction == Enums.Directions.LEFT or direction == Enums.Directions.RIGHT:
return "side"
return Enums.Directions.keys()[direction].to_lower()
func _should_flip_horizontal() -> bool:
return direction_component.GetCurrentDirection() == Enums.Directions.LEFT