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:
@@ -3,8 +3,11 @@ extends Node
|
||||
|
||||
@export var state_machine: PlayerStateMachine
|
||||
|
||||
var _subscribed_events: Array[String] = []
|
||||
var _subscribed_events_callables: Array[Callable] = []
|
||||
|
||||
func GetStateEnum() -> PlayerStateMachine.States:
|
||||
push_error("Unimplemented Method: BaseState.GetName")
|
||||
push_error("Unimplemented Method: BaseState.GetStateEnum")
|
||||
return PlayerStateMachine.States.IDLE
|
||||
|
||||
|
||||
@@ -31,3 +34,23 @@ func IsActive() -> bool:
|
||||
|
||||
func IsStateActionable() -> bool:
|
||||
return true
|
||||
|
||||
|
||||
func SubscribeToEvent(event_name: String, callback: Callable) -> void:
|
||||
if _subscribed_events.has(event_name):
|
||||
return
|
||||
_subscribed_events.append(event_name)
|
||||
_subscribed_events_callables.append(callback)
|
||||
|
||||
|
||||
# State Machine Visibility Only
|
||||
func _StateMachine_OnEventSent(event_name: String, parameters: Variant) -> void:
|
||||
var idx := _subscribed_events.find(event_name)
|
||||
if idx == -1:
|
||||
push_error("THIS SHOULD NEVER HAPPEN LOL")
|
||||
|
||||
var callable := _subscribed_events_callables[idx]
|
||||
if callable.get_argument_count() == 0:
|
||||
callable.call()
|
||||
else:
|
||||
callable.call(parameters)
|
||||
|
||||
Reference in New Issue
Block a user