-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSingleInstanceStatusEffectEffect.cs
114 lines (92 loc) · 3.29 KB
/
SingleInstanceStatusEffectEffect.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
namespace ModiBuff.Core.Units
{
public sealed class SingleInstanceStatusEffectEffect : IMutableStateEffect, IStackEffect, IRevertEffect,
IEffect, IEffectStateInfo<SingleInstanceStatusEffectEffect.Data>,
ISavableEffect<SingleInstanceStatusEffectEffect.SaveData>
{
public bool IsRevertible { get; }
public bool UsesMutableState => IsRevertible || _stackEffect.UsesMutableState();
public bool UsesMutableStackEffect => _stackEffect.UsesMutableState();
private readonly StatusEffectType _statusEffectType;
private readonly float _duration;
private readonly StackEffectType _stackEffect;
private readonly float _stackValue;
private float _extraDuration;
private float _totalDuration;
public SingleInstanceStatusEffectEffect(StatusEffectType statusEffectType, float duration,
bool revertible = false, StackEffectType stackEffect = StackEffectType.Effect, float stackValue = -1)
{
_statusEffectType = statusEffectType;
_duration = duration;
IsRevertible = revertible;
_stackEffect = stackEffect;
_stackValue = stackValue;
}
public void Effect(IUnit target, IUnit source)
{
if (target is not ISingleInstanceStatusEffectOwner<LegalAction, StatusEffectType> statusEffectTarget)
{
#if MODIBUFF_EFFECT_CHECK
EffectHelper.LogImplError(target,
nameof(ISingleInstanceStatusEffectOwner<LegalAction, StatusEffectType>));
#endif
return;
}
if (IsRevertible)
_totalDuration = _duration + _extraDuration;
statusEffectTarget.StatusEffectController.ChangeStatusEffect(_statusEffectType, _duration + _extraDuration);
}
public void RevertEffect(IUnit target, IUnit source)
{
if (target is not ISingleInstanceStatusEffectOwner<LegalAction, StatusEffectType> statusEffectTarget)
return;
statusEffectTarget.StatusEffectController.DecreaseStatusEffect(_statusEffectType, _totalDuration);
_totalDuration = 0;
}
public void StackEffect(int stacks, IUnit target, IUnit source)
{
if ((_stackEffect & StackEffectType.Add) != 0)
_extraDuration += _stackValue;
if ((_stackEffect & StackEffectType.AddStacksBased) != 0)
_extraDuration += _stackValue * stacks;
if ((_stackEffect & StackEffectType.Effect) != 0)
Effect(target, source);
}
public Data GetEffectData() => new Data(_duration, _extraDuration);
public void ResetState()
{
_extraDuration = 0;
_totalDuration = 0;
}
public IEffect ShallowClone() =>
new SingleInstanceStatusEffectEffect(_statusEffectType, _duration, IsRevertible, _stackEffect, _stackValue);
object IShallowClone.ShallowClone() => ShallowClone();
public object SaveState() => new SaveData(_extraDuration, _totalDuration);
public void LoadState(object saveData)
{
var data = (SaveData)saveData;
_extraDuration = data.ExtraDuration;
_totalDuration = data.TotalDuration;
}
public readonly struct Data
{
public readonly float BaseDuration;
public readonly float ExtraDuration;
public Data(float baseDuration, float extraDuration)
{
BaseDuration = baseDuration;
ExtraDuration = extraDuration;
}
}
public readonly struct SaveData
{
public readonly float ExtraDuration;
public readonly float TotalDuration;
public SaveData(float extraDuration, float totalDuration)
{
ExtraDuration = extraDuration;
TotalDuration = totalDuration;
}
}
}
}