-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathEventQueue.cs
190 lines (156 loc) · 3.55 KB
/
EventQueue.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
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Xml.Serialization;
using UnityEngine;
using UnityEngine.SceneManagement;
public abstract class Event
{
public bool IsFinished = false;
public bool IsStarted = false;
public Event NextEvent;
public virtual void EventStart()
{
IsFinished = false;
IsStarted = true;
NextEvent = null;
}
public virtual void Update()
{
}
public virtual void EventEnd()
{
IsFinished = true;
}
}
public class ActionEvent : Event
{
public Action MethodToFire;
public ActionEvent()
{
}
public ActionEvent(Action action)
{
this.MethodToFire = action;
}
public override void EventStart()
{
base.EventStart();
MethodToFire.Invoke();
EventEnd();
}
}
public class ChangeSceneEvent : Event
{
public string SceneToLoad;
public ChangeSceneEvent(string sceneName)
{
this.SceneToLoad = sceneName;
}
public override void EventStart()
{
base.EventStart();
SceneManager.LoadScene(SceneToLoad);
EventEnd();
}
}
public class WaitEvent : Event
{
public float WaitTime;
public WaitEvent()
{
WaitTime = 0f;
}
public WaitEvent(float waitDuration)
{
WaitTime = waitDuration;
}
public override void Update()
{
WaitTime -= Time.deltaTime;
Debug.Log("Wait event running");
if (WaitTime <= 0)
EventEnd();
}
}
public class EventQueue : MonoBehaviour {
public Queue<Event> gameEvents;
public Event activeEvent;
private static EventQueue instance;
public static EventQueue Instance
{
get
{
if (instance == null)
{
instance = new EventQueue();
}
return instance;
}
}
public bool IsBusy
{
get
{
if (activeEvent != null)
return true;
else
return false;
}
}
void Awake()
{
// First we check if there are any other instances conflicting
if (instance != null && instance != this)
{
Debug.Log("There's already a Event Queue in the scene, destroying this one.");
Destroy(gameObject);
}
else
{
instance = this;
Debug.Log("Event Queue woke");
}
}
// Use this for initialization
void Start () {
gameEvents = new Queue<Event>();
}
void Update () {
//check if we need to do something
if (activeEvent != null)
{
if (activeEvent.IsFinished)
{
activeEvent.EventEnd();
activeEvent = null;
gameEvents.Dequeue(); //remove last event
}
else
{
if (!activeEvent.IsStarted)
activeEvent.EventStart();
activeEvent.Update();
}
}
if (gameEvents.Count > 0)
{
activeEvent = gameEvents.Peek();
}
}
public void ChangeScene(string levelName)
{
gameEvents.Enqueue(new ChangeSceneEvent(levelName));
}
public void AddAction(Action method)
{
ActionEvent ev = new ActionEvent(method);
gameEvents.Enqueue(ev);
}
public void WaitABit(float duration)
{
gameEvents.Enqueue(new WaitEvent(duration));
}
}