-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStateFactory.cs
51 lines (47 loc) · 1.58 KB
/
StateFactory.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 System.Collections.Generic;
using UnityEngine;
namespace Buffer.HFSM
{
public class StateFactory
{
public StateMachine machine { get; }
private readonly Dictionary<string, StateBase> states = new();
/// <summary>
/// Creates a new StateFactory for the given StateMachine.
/// </summary>
/// <param name="machine"></param>
public StateFactory(StateMachine machine)
{
this.machine = machine;
}
/// <summary>
/// Returns a state of the given type. If it doesn't exist, it will be created.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public T GetState<T>() where T : StateBase, new()
{
string key = typeof(T).ToString();
if (states.ContainsKey(key)) return (T)states[key];
T state = new T();
states.Add(key, state);
state.Setup(machine);
Debug.Log("Added new state: " + key);
return state;
}
/// <summary>
/// Returns a state of the given type. If it doesn't exist, it will be created.
/// </summary>
/// <param name="state"></param>
/// <returns></returns>
public StateBase GetState(StateBase state)
{
string key = state.GetType().ToString();
if (states.ContainsKey(key)) return states[key];
states.Add(key, state);
state.Setup(machine);
Debug.Log("Added new state: " + key);
return state;
}
}
}