-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDelayedActionsDaemon.cs
More file actions
113 lines (98 loc) · 3.53 KB
/
DelayedActionsDaemon.cs
File metadata and controls
113 lines (98 loc) · 3.53 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
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace com.github.lhervier.ksp
{
// <summary>
// Allows to launch an action in a set of frames. If the same action is triggered
// a second time, the launch of the action will be delayed again.
// </summary>
public class DelayedActionDaemon : MonoBehaviour
{
// <summary>
// Logger
// </summary>
private static SteamControllerLogger LOGGER = new SteamControllerLogger("DelayedActionDaemon");
// ===============================================
// <summary>
// Frame count at which the delayed action will occur
// except if another operation
// ask for another update, which will increase this value.
// </summary>
private IDictionary<Action, int> actionThreshold = new Dictionary<Action, int>();
// <summary>
// Co-routines used to delay the actions
// </summary>
private IDictionary<Action, Coroutine> coroutines = new Dictionary<Action, Coroutine>();
// =======================================================================
// Unity Lifecycle
// =======================================================================
// <summary>
// Component awaked
// </summary>
public void Awake()
{
LOGGER.Log("Awaked");
DontDestroyOnLoad(this);
}
// <summary>
// Component destroyed
// </summary>
public void OnDestroy()
{
LOGGER.Log("Destroyed");
}
// <summary>
// Startup of the component
// </summary>
public void Start()
{
LOGGER.Log("Started");
}
// ===============================================================
// <summary>
// Trigger an action in the future
// </summary>
public void TriggerDelayedAction(Action action, int inFrames)
{
int threshold;
if( this.actionThreshold.ContainsKey(action) )
{
threshold = Math.Max(Time.frameCount + inFrames, this.actionThreshold[action]);
} else {
threshold = Time.frameCount + inFrames;
}
this.actionThreshold[action] = threshold;
if( !this.coroutines.ContainsKey(action) )
{
this.coroutines[action] = this.StartCoroutine(_TriggerDelayedAction(action));
}
}
private IEnumerator _TriggerDelayedAction(Action action)
{
while( this.actionThreshold.ContainsKey(action) && Time.frameCount < this.actionThreshold[action] )
{
yield return null;
}
if( this.actionThreshold.ContainsKey(action) )
{
action();
}
this.coroutines.Remove(action);
this.actionThreshold.Remove(action);
}
// <summary>
// Cancel any action set change request
// </summary>
public void CancelDelayedAction(Action action)
{
if( this.coroutines.ContainsKey(action) )
{
this.StopCoroutine(this.coroutines[action]);
}
this.coroutines.Remove(action);
this.actionThreshold.Remove(action);
}
}
}