-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathStateMachine.cs
51 lines (43 loc) · 1.26 KB
/
StateMachine.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using Godot;
using System;
using System.Collections.Generic;
public partial class StateMachine : Node
{
[Export] public NodePath initialState;
private Dictionary<string, State> _states;
private State _currentState;
public override void _Ready()
{
_states = new Dictionary<string, State>();
foreach (Node node in GetChildren()) {
if (node is State s) {
_states[node.Name] = s;
s.fsm = this;
s.Ready();
s.Exit(); // reset
}
}
_currentState = GetNode<State>(initialState);
_currentState.Enter();
}
public override void _Process(double delta)
{
_currentState.Update((float)delta);
}
public override void _PhysicsProcess(double delta)
{
_currentState.PhysicsUpdate((float)delta);
}
public override void _UnhandledInput(InputEvent @event)
{
_currentState.HandleInput(@event);
@event.Dispose();
}
public void TransitionTo(string key) {
if (!_states.ContainsKey(key) || _states[key] == _currentState)
return;
_currentState.Exit();
_currentState = _states[key];
_currentState.Enter();
}
}