-
Notifications
You must be signed in to change notification settings - Fork 1
/
Chest.cs
64 lines (53 loc) · 1.62 KB
/
Chest.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
52
53
54
55
56
57
58
59
60
61
62
63
64
using Stateless;
using System;
using UnityEngine;
namespace Gbros.StatelessSamples.Example3
{
[AddComponentMenu(Constants.ComponentPath + nameof(Chest))]
public class Chest : MonoBehaviour
{
public enum State { Opened, Closed }
public enum Trigger { Open, Close }
public State CurrentState => stateMachine.State;
public event Action Opened;
public event Action Closed;
private StateMachine<State, Trigger> stateMachine;
private void Awake()
{
stateMachine = new StateMachine<State, Trigger>(State.Closed);
stateMachine
.Configure(State.Opened)
.OnEntry(OnChestOpened)
.Permit(Trigger.Close, State.Closed);
stateMachine
.Configure(State.Closed)
.OnEntry(OnChestClosed)
.Permit(Trigger.Open, State.Opened);
}
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
Open();
return;
}
if (Input.GetMouseButtonDown(1))
{
Close();
return;
}
}
public void Open()
{
if (!stateMachine.CanFire(Trigger.Open)) return;
stateMachine.Fire(Trigger.Open);
}
public void Close()
{
if (!stateMachine.CanFire(Trigger.Close)) return;
stateMachine.Fire(Trigger.Close);
}
private void OnChestClosed() => Closed?.Invoke();
private void OnChestOpened() => Opened?.Invoke();
}
}