Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extends Node
- class_name StateMachine
- var state = null
- var previous_state = null
- var states = {}
- onready var parent = get_parent()
- func _physics_process(delta):
- if state != null:
- _state_logic(delta)
- var transition = _get_transition(delta)
- if transition != null:
- set_state(transition)
- #Follow 4 functions need to be pasted into new script and applied accordingly; See EnemySM.gd for examples
- #State Logic is where you apply logic when parent enters a certain state
- func _state_logic(delta):
- pass
- #Get Transition applies when transitions should occur and applies new states
- func _get_transition(delta):
- return null
- # Enter State is called when a state is entered, mainly used for animations, sounds, or attacks
- func _enter_state(new_state, old_state):
- pass
- # Exit State is called when a state is exited, good for the same usage as enter state
- func _exit_state(old_state, new_state):
- pass
- func set_state(new_state):
- previous_state = state
- state = new_state
- if previous_state != null:
- _exit_state(previous_state, new_state)
- if new_state != null:
- _enter_state(new_state, previous_state)
- func add_state(state_name):
- states[state_name] = states.size()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement