Deozaan

StateMachine.gd

Dec 31st, 2020 (edited)
376
213 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extends Node
  2. class_name StateMachine
  3.  
  4.  
  5. var state = null setget set_state
  6. var previous_state = null
  7. var states = {}
  8.  
  9.  
  10. onready var parent = get_parent()
  11.  
  12.  
  13. func _process(delta):
  14.     if state != null:
  15.         _state_logic(delta)
  16.         _set_transition(delta)
  17.  
  18.  
  19. func _physics_process(delta):
  20.     if state != null:
  21.         _state_physics_logic(delta)
  22.         _set_transition(delta)
  23.  
  24.  
  25. func _state_logic(_delta):
  26.     pass
  27.    
  28.  
  29. func _state_physics_logic(_delta):
  30.     pass
  31.  
  32.  
  33. func _set_transition(delta):
  34.     var transition = _get_transition(delta)
  35.     if transition != null:
  36.         set_state(transition)
  37.  
  38.  
  39. func _get_transition(_delta):
  40.     return null
  41.  
  42.  
  43. func _enter_state(new_state, old_state):
  44.     pass
  45.  
  46.  
  47. func _exit_state(old_state, new_state):
  48.     pass
  49.  
  50.  
  51. func set_state(new_state):
  52.     previous_state = state
  53.     state = new_state
  54.    
  55.     if previous_state != null:
  56.         _exit_state(previous_state, new_state)
  57.    
  58.     if new_state != null:
  59.         _enter_state(new_state, previous_state)
  60.  
  61.  
  62. func add_state(state_name):
  63.     states[state_name] = states.size()
  64.  
RAW Paste Data