Added state machine
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
class_name AbstractState
|
||||
extends Node
|
||||
|
||||
|
||||
@warning_ignore("unused_signal")
|
||||
signal switch_state(state: AbstractState)
|
||||
|
||||
|
||||
func init() -> void:
|
||||
pass
|
||||
|
||||
|
||||
func enter() -> void:
|
||||
pass
|
||||
|
||||
|
||||
func exit() -> void:
|
||||
pass
|
||||
|
||||
|
||||
@warning_ignore("unused_parameter")
|
||||
func process(delta: float) -> void:
|
||||
pass
|
||||
|
||||
|
||||
@warning_ignore("unused_parameter")
|
||||
func physics_process(delta: float) -> void:
|
||||
pass
|
||||
@@ -0,0 +1 @@
|
||||
uid://ofqdyeqbeg4i
|
||||
@@ -0,0 +1,6 @@
|
||||
[gd_scene format=3 uid="uid://cxdirjlkyccs4"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://ofqdyeqbeg4i" path="res://game/state_machine/abstract_state.gd" id="1_80326"]
|
||||
|
||||
[node name="AbstractState" type="Node" unique_id=836520368]
|
||||
script = ExtResource("1_80326")
|
||||
@@ -0,0 +1,43 @@
|
||||
class_name StateMachine
|
||||
extends Node
|
||||
|
||||
|
||||
@export var initial_state: AbstractState
|
||||
|
||||
|
||||
var current_state : AbstractState
|
||||
|
||||
|
||||
func init() -> void:
|
||||
_init_states()
|
||||
if initial_state:
|
||||
_change_state(initial_state)
|
||||
|
||||
|
||||
func process(delta: float) -> void:
|
||||
if current_state:
|
||||
current_state.process(delta)
|
||||
|
||||
|
||||
func physics_process(delta: float) -> void:
|
||||
if current_state:
|
||||
current_state.physics_process(delta)
|
||||
|
||||
|
||||
func _change_state(new_state: AbstractState) -> void:
|
||||
if new_state == null: return
|
||||
if new_state == current_state: return
|
||||
|
||||
if current_state:
|
||||
current_state.exit()
|
||||
current_state = new_state
|
||||
current_state.enter()
|
||||
|
||||
|
||||
func _init_states() -> void:
|
||||
for child in get_children():
|
||||
if not child is AbstractState: continue
|
||||
var state := child as AbstractState
|
||||
state.init()
|
||||
state.switch_state.connect(_change_state)
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
uid://cwd731cnbgann
|
||||
@@ -0,0 +1,6 @@
|
||||
[gd_scene format=3 uid="uid://blxgfix54dxq3"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://cwd731cnbgann" path="res://game/state_machine/state_machine.gd" id="1_ncw1q"]
|
||||
|
||||
[node name="StateMachine" type="Node" unique_id=560273456]
|
||||
script = ExtResource("1_ncw1q")
|
||||
Reference in New Issue
Block a user