player character has move and dodge
This commit is contained in:
42
Scripts/player_state_machine.gd
Normal file
42
Scripts/player_state_machine.gd
Normal file
@@ -0,0 +1,42 @@
|
||||
extends Node
|
||||
class_name PlayerStateMachine
|
||||
|
||||
@export var character : Player
|
||||
@export var initial_state : State
|
||||
|
||||
var current_state : State
|
||||
var states : Dictionary = {}
|
||||
|
||||
func _ready() -> void:
|
||||
for child in get_children():
|
||||
if child is State:
|
||||
states[child.name.to_lower()] = child
|
||||
child.Transitioned.connect(on_child_transition)
|
||||
|
||||
if initial_state:
|
||||
initial_state.Enter()
|
||||
current_state = initial_state
|
||||
|
||||
func _process(delta: float) -> void:
|
||||
if current_state:
|
||||
current_state.Update(delta)
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
if current_state:
|
||||
current_state.Physics_Update(delta)
|
||||
|
||||
func on_child_transition(state,new_state_name):
|
||||
if state != current_state:
|
||||
return
|
||||
|
||||
var new_state = states.get(new_state_name.to_lower())
|
||||
if !new_state:
|
||||
return
|
||||
|
||||
if current_state:
|
||||
current_state.Exit()
|
||||
|
||||
new_state.Enter()
|
||||
|
||||
current_state = new_state
|
||||
print("PLAYER STATE CHANGED TO : ",current_state)
|
||||
Reference in New Issue
Block a user