-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathHealEffect.cs
227 lines (183 loc) · 6.61 KB
/
HealEffect.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
using System;
namespace ModiBuff.Core.Units
{
public sealed class HealEffect : IMutableStateEffect, IStackEffect, IRevertEffect, IEffect,
ICallbackEffect, IConditionEffect, IStackRevertEffect, IMetaEffectOwner<HealEffect, float, float>,
IPostEffectOwner<HealEffect, float>, IEffectStateInfo<HealEffect.Data>, ISavableEffect<HealEffect.SaveData>
{
public bool IsRevertible => _effectState != 0;
public bool UsesMutableState => IsRevertible || _stackEffect.UsesMutableState();
public bool UsesMutableStackEffect => _stackEffect.UsesMutableState();
public Condition[] Conditions { get; set; }
private readonly float _heal;
private readonly EffectState _effectState;
private readonly StackEffectType _stackEffect;
private readonly float _stackValue;
private readonly Targeting _targeting;
private IMetaEffect<float, float>[] _metaEffects;
private IPostEffect<float>[] _postEffects;
private float _extraHeal;
private float _totalHeal;
public HealEffect(float heal, EffectState effectState = EffectState.None,
StackEffectType stack = StackEffectType.Effect, float stackValue = -1,
Targeting targeting = Targeting.TargetSource)
: this(heal, effectState, stack, stackValue, targeting, null, null, null)
{
}
/// <summary>
/// Manual modifier generation constructor
/// </summary>
public static HealEffect Create(float heal, EffectState effectState = EffectState.None,
StackEffectType stack = StackEffectType.Effect, float stackValue = -1,
Targeting targeting = Targeting.TargetSource, IMetaEffect<float, float>[] metaEffects = null,
IPostEffect<float>[] postEffects = null, Condition[] conditions = null) =>
new HealEffect(heal, effectState, stack, stackValue, targeting, metaEffects, postEffects, conditions);
private HealEffect(float heal, EffectState effectState, StackEffectType stack, float stackValue,
Targeting targeting, IMetaEffect<float, float>[] metaEffects, IPostEffect<float>[] postEffects,
Condition[] conditions)
{
_heal = heal;
_effectState = effectState;
_stackEffect = stack;
_stackValue = stackValue;
_targeting = targeting;
_metaEffects = metaEffects;
_postEffects = postEffects;
Conditions = conditions ?? Array.Empty<Condition>();
}
public HealEffect SetMetaEffects(params IMetaEffect<float, float>[] metaEffects)
{
_metaEffects = metaEffects;
return this;
}
public HealEffect SetPostEffects(params IPostEffect<float>[] postEffects)
{
_postEffects = postEffects;
return this;
}
public void Effect(IUnit target, IUnit source)
{
if (!this.Check(_heal, target, source))
return;
float returnHeal = 0;
_targeting.UpdateTargetSource(target, source, out var effectTarget, out var effectSource);
if (effectTarget is IHealable<float, float> healableTarget)
{
float heal = _heal;
if (_metaEffects != null)
foreach (var metaEffect in _metaEffects)
if (metaEffect is not IConditionEffect conditionEffect ||
conditionEffect.Check(heal, target, source))
heal = metaEffect.Effect(heal, target, source);
heal += _extraHeal;
//TODO Design choice, do we want to revert overheal? Or only applied heals?
returnHeal = healableTarget.Heal(heal, effectSource);
if (IsRevertible)
_totalHeal += returnHeal;
}
#if MODIBUFF_EFFECT_CHECK
else
EffectHelper.LogImplError(effectTarget, nameof(IHealable<float, float>));
#endif
if (_postEffects != null)
foreach (var postEffect in _postEffects)
if (postEffect is not IConditionEffect conditionEffect ||
conditionEffect.Check(returnHeal, target, source))
postEffect.Effect(returnHeal, target, source);
}
public void RevertEffect(IUnit target, IUnit source)
{
_targeting.UpdateTargetSource(ref target, ref source);
if (target is not IHealable<float, float> healableTarget)
return;
healableTarget.Heal(-_totalHeal, source);
_totalHeal = 0;
}
public void StackEffect(int stacks, IUnit target, IUnit source)
{
if ((_stackEffect & StackEffectType.Set) != 0)
_extraHeal = _stackValue;
if ((_stackEffect & StackEffectType.SetStacksBased) != 0)
_extraHeal = _stackValue * stacks;
if ((_stackEffect & StackEffectType.Add) != 0)
_extraHeal += _stackValue;
if ((_stackEffect & StackEffectType.AddStacksBased) != 0)
_extraHeal += _stackValue * stacks;
if ((_stackEffect & StackEffectType.Effect) != 0)
Effect(target, source);
}
//TODO Should callback effects use stack logic?
public void CallbackEffect(IUnit target, IUnit source)
{
if ((_stackEffect & StackEffectType.Set) != 0)
_extraHeal = _stackValue;
if ((_stackEffect & StackEffectType.Add) != 0)
_extraHeal += _stackValue;
if ((_stackEffect & StackEffectType.Effect) != 0)
Effect(target, source);
}
public void RevertStack(int stacks, IUnit target, IUnit source)
{
if ((_stackEffect & StackEffectType.Effect) != 0 && _effectState != EffectState.ValueIsRevertible)
{
_targeting.UpdateTargetSource(ref target, ref source);
//TODO Do we want a custom negative heal method?
if (target is IHealable<float, float> healableTarget)
{
_totalHeal -= _heal + _extraHeal;
healableTarget.Heal(-_heal - _extraHeal, source);
}
}
if ((_stackEffect & StackEffectType.AddStacksBased) != 0)
_extraHeal -= _stackValue * stacks;
if ((_stackEffect & StackEffectType.Add) != 0)
_extraHeal -= _stackValue;
if ((_stackEffect & StackEffectType.SetStacksBased) != 0)
_extraHeal = 0;
if ((_stackEffect & StackEffectType.Set) != 0)
_extraHeal = 0;
}
public Data GetEffectData() => new Data(_heal, _extraHeal);
public void ResetState()
{
_extraHeal = 0;
_totalHeal = 0;
}
public IEffect ShallowClone() => new HealEffect(_heal, _effectState, _stackEffect, _stackValue, _targeting,
_metaEffects, _postEffects, Conditions);
object IShallowClone.ShallowClone() => ShallowClone();
public object SaveState() => new SaveData(_extraHeal, _totalHeal);
public void LoadState(object data)
{
var saveData = (SaveData)data;
_extraHeal = saveData.ExtraHeal;
_totalHeal = saveData.TotalHeal;
}
public readonly struct Data
{
public readonly float BaseHeal;
public readonly float ExtraHeal;
public Data(float baseHeal, float extraHeal)
{
BaseHeal = baseHeal;
ExtraHeal = extraHeal;
}
}
public readonly struct SaveData
{
public readonly float ExtraHeal;
public readonly float TotalHeal;
public SaveData(float extraHeal, float totalHeal)
{
ExtraHeal = extraHeal;
TotalHeal = totalHeal;
}
}
public enum EffectState
{
None = 0,
IsRevertible = 1,
ValueIsRevertible = 2,
}
}
}