player_state_machine.gd 861 B

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