Advertisement
Guest User

Untitled

a guest
Feb 16th, 2020
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. extends Node
  2. class_name StateMachine
  3.  
  4. signal state_changed(new_state,old_state)
  5.  
  6. var state = null setget set_state
  7. var previous_state = null
  8. var states = {}
  9.  
  10. onready var parent = get_parent()
  11.  
  12. func _physics_process(delta: float) -> void:
  13. if state != null:
  14. _state_logic(delta)
  15. var transition = _get_transition(delta)
  16. if transition != null:
  17. set_state(transition)
  18.  
  19. func _state_logic(delta):
  20. pass
  21.  
  22. func _get_transition(delta):
  23. return null
  24.  
  25. func _enter_state(new_state,old_state):
  26. pass
  27.  
  28. func _exit_state(old_state,new_state):
  29. pass
  30.  
  31. func set_state(new_state):
  32. previous_state = state
  33. state = new_state
  34.  
  35. if previous_state != null:
  36. _exit_state(previous_state,new_state)
  37. if new_state != null:
  38. _enter_state(new_state,previous_state)
  39.  
  40. emit_signal("state_changed",new_state,previous_state)
  41.  
  42. func add_state(state_name):
  43. states[state_name] = states.size()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement