-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameSystem.cs
More file actions
86 lines (61 loc) · 2.61 KB
/
Copy pathGameSystem.cs
File metadata and controls
86 lines (61 loc) · 2.61 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using UnityEngine;
using antunity.GameData;
using antunity.GameSystems.Rules;
namespace antunity.GameSystems
{
public interface IGameSystemBase
{
public IGameDataProvider Environment { get; set; }
public void Initialize(GameSystemTemplate template);
}
public interface IGameSystem<TAction> : IGameSystemBase where TAction : struct
{
public RuleResult EvaluateAction(TAction action, IGameDataProvider subject, IGameDataProvider instigator = null);
public IGameContext GetActionContext(TAction action);
public void SetActionContext(IGameContext context);
public void SetActionRule(TAction action, Rule rule);
}
public abstract class GameSystem<TAction> : MonoBehaviour, IGameSystem<TAction> where TAction : struct
{
private GameDataRegistry<IGameContext> actionContexts = new();
[SerializeField] private EnumDataValues<TAction, Rule> rules = new();
#region IGameSystemBase
public IGameDataProvider Environment { get; set; }
public abstract void Initialize(GameSystemTemplate template);
#endregion IGameSystemBase
#region IGameSystem
public RuleResult EvaluateAction(TAction action, IGameDataProvider subject, IGameDataProvider instigator = null)
{
if (!rules.ContainsIndex(action))
return RuleResult.Success();
var actionRule = rules[action];
if (!actionRule)
return RuleResult.Success();
if (!actionContexts.TryGetData(action, out IGameContext context))
{
context = new GameContext<TAction>(action);
actionContexts.Add(context);
}
// Default to the system's environment
context.Environment ??= Environment;
// Assign subject
context.Subject = subject;
// Assign the instigator if defined
if (instigator != null)
context.Instigator = instigator;
else
context.Instigator = context.Environment;
return actionRule.Evaluate(context);
}
public IGameContext GetActionContext(TAction action) => actionContexts.TryGetData(action, out var context) ? context : default;
public void SetActionContext(IGameContext context) => actionContexts.Add(context);
public void SetActionRule(TAction action, Rule rule)
{
if (rules.ContainsIndex(action))
rules[action] = rule;
else
rules.Add(action, rule);
}
#endregion IGameSystem
}
}