-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCooldownAction.cs
More file actions
61 lines (52 loc) · 1.7 KB
/
CooldownAction.cs
File metadata and controls
61 lines (52 loc) · 1.7 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
using System;
public class CooldownAction {
/// <summary>
/// No cooldown at 0, full cooldown at 1.
/// </summary>
public float percentRemaining => cooldownLeft / cooldownDuration;
private Action action;
public float cooldownDuration;
public float cooldownLeft { get; private set; }
private float bufferTime;
private float bufferLeft;
/// <param name="action">The action you want called.</param>
/// <param name="cooldownDuration">How long to cooldown between action calls.</param>
/// <param name="bufferTime">How long to hold onto an Invoke before losing it to the void.</param>
public CooldownAction(float cooldownDuration, Action action = null, float bufferTime = 0) {
if (action != null)
this.action += action;
this.cooldownDuration = cooldownDuration;
this.bufferTime = bufferTime;
}
public void AddAction(Action action) => this.action += action;
public void RemoveAction(Action action) => this.action -= action;
/// <summary>
/// Updates the timers with deltaTime.
/// Might also call the action if its been buffered.
/// </summary>
public void UpdateTime(float deltaTime) {
if (cooldownLeft > 0)
cooldownLeft -= Math.Min(deltaTime, cooldownLeft);
if (bufferLeft > 0) {
bufferLeft -= Math.Min(deltaTime, bufferLeft);
if (bufferLeft > 0 && cooldownLeft == 0)
Invoke();
}
}
/// <summary>
/// Calls the action, returns true if the action can be called taking the buffer time into account.
/// </summary>
public bool Invoke() {
if (action == null)
return false;
if (cooldownLeft <= 0) {
action.Invoke();
bufferLeft = 0;
cooldownLeft = cooldownDuration;
return true;
} else {
bufferLeft = bufferTime;
return cooldownLeft <= bufferTime;
}
}
}