player_state_machine.gd 997 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. extends "res://state_machine/state_machine.gd"
  2. @onready var idle = $Idle
  3. @onready var move = $Move
  4. @onready var jump = $Jump
  5. @onready var stagger = $Stagger
  6. @onready var attack = $Attack
  7. func _ready():
  8. states_map = {
  9. "idle": idle,
  10. "move": move,
  11. "jump": jump,
  12. "stagger": stagger,
  13. "attack": attack,
  14. }
  15. func _change_state(state_name):
  16. # The base state_machine interface this node extends does most of the work.
  17. if not _active:
  18. return
  19. if state_name in ["stagger", "jump", "attack"]:
  20. states_stack.push_front(states_map[state_name])
  21. if state_name == "jump" and current_state == move:
  22. jump.initialize(move.speed, move.velocity)
  23. super._change_state(state_name)
  24. func _unhandled_input(event):
  25. # Here we only handle input that can interrupt states, attacking in this case,
  26. # otherwise we let the state node handle it.
  27. if event.is_action_pressed("attack"):
  28. if current_state in [attack, stagger]:
  29. return
  30. _change_state("attack")
  31. return
  32. current_state.handle_input(event)