Advertisement
mrtapas

state machine

Nov 24th, 2020
734
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extends Node
  2.  
  3. class_name StateMachine
  4.  
  5. var states: Dictionary={}
  6. var current_state: State
  7.  
  8. func _ready():
  9.     yield(get_parent(), "ready")
  10.    
  11.     for state in get_children():
  12.         states[state.name] = state
  13.     for state in get_children():
  14.         state.states = self.states
  15.         state.player = get_parent()
  16.  
  17.     current_state = states["Idle"]
  18.     enter_state()
  19.  
  20. func enter_state() -> void:
  21.     current_state.enter()
  22.    
  23. func _physics_process(_delta):
  24.     current_state.logic()
  25.     if current_state.get_transition() != null:
  26.         change_state(current_state.get_transition())
  27.        
  28. func change_state(nextState: State) -> void:
  29.     current_state = nextState
  30.     enter_state()
  31.  
  32.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement