forked from whztt07/UnityGameplayAbilitySystem
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActiveGameplayEffectData.cs
More file actions
136 lines (108 loc) · 5.52 KB
/
Copy pathActiveGameplayEffectData.cs
File metadata and controls
136 lines (108 loc) · 5.52 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
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
using UnityEngine;
using System;
using GameplayAbilitySystem.Interfaces;
using System.Collections.Generic;
using GameplayAbilitySystem.Attributes;
namespace GameplayAbilitySystem.GameplayEffects {
/// <summary>
/// This class is used to keep track of active <see cref="GameplayEffect"/>.
/// </summary>
[Serializable]
public class ActiveGameplayEffectData {
public ActiveGameplayEffectData(GameplayEffect effect, IGameplayAbilitySystem instigator, IGameplayAbilitySystem target) {
this._gameplayEffect = effect;
this._startWorldTime = Time.time;
this.Instigator = instigator;
this.Target = target;
if (!this.Effect.Period.ExecuteOnApplication) {
this._timeOfLastPeriodicApplication = Time.time;
}
}
bool _bForceRemoveEffect = false;
public bool bForceRemoveEffect => _bForceRemoveEffect;
/// <summary>
/// The actual <see cref="GameplayEffect"/>.
/// </summary>
/// <value></value>
[SerializeField]
public GameplayEffect Effect { get => _gameplayEffect; }
/// <summary>
/// The cooldown time that has already elapsed for this gameplay effect
/// </summary>
/// <value>Cooldown time elapsed</value>
public float CooldownTimeElapsed { get => Time.time - _startWorldTime; }
/// <summary>
/// The total cooldown time for this gameplay effect
/// </summary>
/// <value>Cooldown time total</value>
public float CooldownTimeTotal { get => Effect.GameplayEffectPolicy.DurationPolicy == EDurationPolicy.HasDuration ? Effect.GameplayEffectPolicy.DurationMagnitude : 0; }
/// <summary>
/// The cooldown time that is remaining for this gameplay effect
/// </summary>
/// <value>Cooldown time remaining</value>
public float CooldownTimeRemaining { get => Effect.GameplayEffectPolicy.DurationPolicy == EDurationPolicy.HasDuration ? CooldownTimeTotal - CooldownTimeElapsed : 0; }
private float _timeOfLastPeriodicApplication = 0;
public float TimeSincePreviousPeriodicApplication { get => Time.time - _timeOfLastPeriodicApplication; }
public float TimeUntilNextPeriodicApplication { get => _timeOfLastPeriodicApplication + Effect.Period.Period - Time.time; }
private Dictionary<AttributeType, Aggregator> PeriodicEffectModificationsToDate = new Dictionary<AttributeType, Aggregator>();
public IGameplayAbilitySystem Instigator { get; private set; }
public IGameplayAbilitySystem Target { get; private set; }
[SerializeField]
private int _stacks;
[SerializeField]
private GameplayEffect _gameplayEffect;
[SerializeField]
private float _startWorldTime;
public float StartWorldTime { get => _startWorldTime; }
public void CheckOngoingTagRequirements() {
}
/// <summary>
/// Reset duration of this effect.
/// Optionally, we can provide an offset to compensate for
/// the fact that the reset did not happen at exactly 0
/// and over time this could cause time drift
/// </summary>
/// <param name="offset">Overflow time</param>
public void ResetDuration(float offset = 0) {
this._startWorldTime = Time.time;
}
public void EndEffect() {
this._startWorldTime = Time.time - CooldownTimeTotal;
}
public void ForceEndEffect() {
EndEffect();
_bForceRemoveEffect = true;
}
/// <summary>
/// Reset time at which last periodic application occured.
/// Optionally, we can provide an offset to compensate for
/// the fact that the reset did not happen at exactly 0
/// and over time this could cause time drift
/// </summary>
/// <param name="offset">Overflow time</param>
public void ResetPeriodicTime(float offset = 0) {
this._timeOfLastPeriodicApplication = Time.time - offset;
}
public void AddPeriodicEffectAttributeModifiers() {
// Check out ActiveGameplayEffectContainer.AddActiveGameplayEffect to see how to populate the ActiveEffectAttributeAggregator object
foreach (var modifier in Effect.GameplayEffectPolicy.Modifiers) {
modifier.AttemptCalculateMagnitude(out var EvaluatedMagnitude);
// If aggregator for this attribute doesn't exist, add it.
if (!PeriodicEffectModificationsToDate.TryGetValue(modifier.Attribute, out var aggregator)) {
aggregator = new Aggregator(modifier.Attribute);
// aggregator.Dirtied.AddListener(UpdateAttribute);
PeriodicEffectModificationsToDate.Add(modifier.Attribute, aggregator);
}
aggregator.AddAggregatorMod(EvaluatedMagnitude, modifier.ModifierOperation);
// Recalculate new value by recomputing all aggregators
var aggregators = Target.ActiveGameplayEffectsContainer.ActiveEffectAttributeAggregator
.GetAggregatorsForAttribute(modifier.Attribute);
Target.ActiveGameplayEffectsContainer.UpdateAttribute(aggregators, modifier.Attribute);
}
}
public Aggregator GetPeriodicAggregatorForAttribute(AttributeType Attribute) {
PeriodicEffectModificationsToDate.TryGetValue(Attribute, out var aggregator);
return aggregator;
}
}
}