-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnityCondition.cs
206 lines (163 loc) · 5.25 KB
/
UnityCondition.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
using System;
using System.Collections.Generic;
using System.Linq;
using Sirenix.OdinInspector;
using UnityEngine;
using UnityEngine.Events;
[BoxGroup]
[Serializable]
public class UnityCondition
{
[SerializeField]
[HideLabel]
[TabGroup("Root", "Parameters", SdfIconType.GearFill, TextColor = "Orange")]
[EnumToggleButtons]
private BooleanOperation _operation;
[Space(5)] [SerializeField] [TabGroup("Root", "Parameters", SdfIconType.GearFill, TextColor = "Orange")]
private List<Statement> _statements = new();
[TabGroup("Root", "Events", SdfIconType.ArrowUpShort, TextColor = "Yellow")]
public UnityEvent OnTrue;
[TabGroup("Root", "Events", SdfIconType.ArrowUpShort, TextColor = "Yellow")]
public UnityEvent OnFalse;
public bool Evaluate()
{
var result = _operation switch
{
BooleanOperation.AND => _statements.All(t => t.Evaluate()),
BooleanOperation.OR => _statements.Any(t => t.Evaluate()),
_ => false
};
if (result is true) OnTrue?.Invoke();
if (result is false) OnFalse?.Invoke();
return result;
}
}
[BoxGroup]
[Serializable]
public class Statement
{
#region Inspector
public enum Type
{
Boolean,
Numeric,
Progression
}
[SerializeField] private Type _type;
#region Boolean
[HorizontalGroup("Main", Width = 0.47f)]
[HideLabel]
[ShowIf("@_type == Type.Boolean")]
[SerializeField]
[SuffixLabel("is")]
private BooleanGameData _booleanValueA;
[HorizontalGroup("Main", Width = 0.47f)]
[ShowIf("@_type == Type.Boolean && _useGameDataAsValueB")]
[SerializeField]
[HideLabel]
private BooleanGameData _booleanGameDataValueB;
[HorizontalGroup("Main", Width = 0.47f)]
[ShowIf("@_type == Type.Boolean && !_useGameDataAsValueB")]
[SerializeField]
[HideLabel]
private bool _booleanLocalValueB;
#endregion
#region Numeric
[HorizontalGroup("Main", Width = 0.422f)]
[HideLabel]
[ShowIf("@_type == Type.Numeric")]
[SerializeField]
private NumericGameData _numericValueA;
[SerializeField]
[HorizontalGroup("Main", Width = 0.09f)]
[HideLabel]
[ShowIf("@_type == Type.Numeric")]
private NumericOperation _numericOperation;
[HorizontalGroup("Main", Width = 0.422f)]
[ShowIf("@_type == Type.Numeric && _useGameDataAsValueB")]
[SerializeField]
[HideLabel]
private NumericGameData _numericGameDataValueB;
[HorizontalGroup("Main", Width = 0.422f)]
[ShowIf("@_type == Type.Numeric && !_useGameDataAsValueB")]
[SerializeField]
[HideLabel]
private float _numericLocalValueB;
#endregion
#region Progression
[HorizontalGroup("Main")]
[HideLabel]
[ShowIf("@_type == Type.Progression")]
[SerializeField]
[SuffixLabel("is")]
private ProgressionGameData _progressionValueA;
[HorizontalGroup("Main")] [ShowIf("@_type == Type.Progression")] [SerializeField] [HideLabel]
private ProgressionState _progressionValueB;
#endregion
[HorizontalGroup("Main", Width = 0.045f)]
[Button(Icon = SdfIconType.ArrowLeftRight, IconAlignment = IconAlignment.LeftOfText)]
[HideLabel]
[Tooltip("Toggle to game Data or to local value")]
[ShowIf("@_type != Type.Progression")]
private void ToggleBooleanValue()
{
_useGameDataAsValueB = !_useGameDataAsValueB;
}
private bool _useGameDataAsValueB;
#endregion
#region API
public bool Evaluate()
{
return _type switch
{
Type.Boolean => EvaluateBoolean(),
Type.Numeric => EvaluateNumeric(),
Type.Progression => EvaluateProgression(),
_ => false
};
}
private bool EvaluateProgression()
{
return GameData.Current.GetProgressionGameData(_progressionValueA) == _progressionValueB;
}
private bool EvaluateNumeric()
{
var valueA = GameData.Current.GetNumericGameData(_numericValueA);
var valueB = _useGameDataAsValueB
? GameData.Current.GetNumericGameData(_numericGameDataValueB)
: _numericLocalValueB;
return _numericOperation switch
{
NumericOperation.Equals => Math.Abs(valueA - valueB) < 0.1f,
NumericOperation.BiggerThan => valueA > valueB,
NumericOperation.SmallerThan => valueA < valueB,
NumericOperation.BiggerThanOrEqual => valueA >= valueB,
NumericOperation.SmallerThanOrEqual => valueA <= valueB,
_ => false
};
}
private bool EvaluateBoolean()
{
var valueA = GameData.Current.GetBooleanGameData(_booleanValueA);
var valueB = _useGameDataAsValueB
? GameData.Current.GetBooleanGameData(_booleanGameDataValueB)
: _booleanLocalValueB;
return valueA == valueB;
}
#endregion
}
public enum NumericOperation
{
[InspectorName("==")] Equals,
[InspectorName(">")] BiggerThan,
[InspectorName(">=")] BiggerThanOrEqual,
[InspectorName("<")] SmallerThan,
[InspectorName("<=")] SmallerThanOrEqual
}
public enum BooleanOperation
{
[InspectorName("All Statements Must Be True")]
AND,
[InspectorName("Only One Statement Must Be True")]
OR
}