-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEmitter.cs
283 lines (229 loc) · 7.41 KB
/
Emitter.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.IO;
public struct Vector2i {
public int X;
public int Y;
public Vector2i(Vector2i other )
{
X = other.X;
Y = other.Y;
}
public static Vector2i operator *(Vector2i lhs, float scale)
{
return new Vector2i() { X = (int)(lhs.X * scale), Y = (int)(lhs.Y * scale) };
}
public static Vector2i operator +(Vector2i lhs, Vector2i rhs)
{
return new Vector2i() { X = lhs.X + rhs.X, Y = lhs.Y + rhs.Y };
}
public static explicit operator Vector2f(Vector2i rhs)
{
return new Vector2f() { X = rhs.X, Y = rhs.Y };
}
}
public struct Vector2f {
public float X;
public float Y;
public Vector2f(Vector2f other)
{
X = other.X;
Y = other.Y;
}
public static Vector2f operator *(Vector2f lhs, float scale)
{
return new Vector2f() { X = lhs.X * scale, Y = lhs.Y * scale };
}
public static Vector2f operator +(Vector2f lhs, Vector2f rhs)
{
return new Vector2f() { X = lhs.X + rhs.X, Y = lhs.Y + rhs.Y };
}
public static explicit operator Vector2i(Vector2f rhs)
{
return new Vector2i() { X = (int)rhs.X, Y = (int)rhs.Y };
}
public static Vector2f Random(float minAngle, float maxAngle)
{
float angle = minAngle + (maxAngle - minAngle) * (float)MathExtensions.RNG.NextDouble();
double radians = (Math.PI / 180f) * angle;
float radius = 1.0f;
var vec = new Vector2f() { X = radius * (float)Math.Cos(radians), Y = radius * (float)Math.Sin(radians) };
return vec;
}
}
public class Particle : IDisposable {
public Vector2f PrevOrigin;
public Vector2f Origin;
public Vector2f Velocity;
public float Lifetime;
public void Dispose()
{
Console.CursorLeft = (int)PrevOrigin.X;
Console.CursorTop = Console.WindowHeight - (int)PrevOrigin.Y;
Console.Write(' ');
}
}
public class EmitterLoader {
public static Emitter Create(string emitterPath, Vector2i origin)
{
var data = File.ReadAllText(emitterPath);
var particleDefs = JsonConvert.DeserializeObject<List<Emitter.ParticleDef>>(data, DataLoader.Settings);
Emitter emitter = new Emitter(particleDefs);
emitter.Origin = origin;
return emitter;
}
}
public class Emitter {
private static LinkedList<Emitter> m_Emitters = new LinkedList<Emitter>();
public static void Register( Emitter emitter )
{
m_Emitters.AddLast(emitter);
}
public static void Unregister(Emitter emitter)
{
m_Emitters.Remove(emitter);
}
public static async Task ProcessEmitters()
{
float deltaTime = 1f / 10f;
Vector2i prevCursor;
while (true)
{
prevCursor = new Vector2i() { X = Console.CursorLeft, Y = Console.CursorTop };
foreach (var emitter in m_Emitters)
{
emitter.UpdateFrame(deltaTime);
}
foreach (var emitter in m_Emitters)
{
emitter.RenderFrame();
}
Console.CursorLeft = prevCursor.X;
Console.CursorTop = prevCursor.Y;
Console.ResetColor();
await Task.Delay((int)(deltaTime * 1000f));
}
}
public readonly Vector2f Gravity = new Vector2f() { X = 0.0f, Y = -9.8f };
public class ParticleDef {
[JsonProperty("count")]
public int Count;
[JsonProperty("launchDirection")]
public Tuple<int, int> LaunchDirection;
[JsonProperty("image")]
public char Image;
[JsonProperty("color")]
public ConsoleColor Color;
[JsonProperty("lifetime")]
public float Lifetime;
[JsonProperty("gravityScale")]
public float GravityScale;
[JsonProperty("startSpeed")]
public float StartSpeed;
[JsonProperty("endSpeed")]
public float EndSpeed;
}
protected Dictionary<ParticleDef, LinkedList<Particle>> m_Particles = new Dictionary<ParticleDef, LinkedList<Particle>>();
public Vector2i Origin {
get;
set;
}
public bool DestroyWhenDone;
public Emitter( List<Emitter.ParticleDef> particleDefs )
{
foreach( var def in particleDefs )
{
m_Particles.Add(def, new LinkedList<Particle>());
}
}
public void Start()
{
foreach( var def in m_Particles.Keys )
{
var particleList = m_Particles[def];
for( int ix = 0; ix < def.Count; ++ix )
{
Particle p = new Particle();
p.Origin = (Vector2f)Origin;
p.PrevOrigin = p.Origin;
p.Lifetime = def.Lifetime;
p.Velocity = Vector2f.Random(def.LaunchDirection.Item1, def.LaunchDirection.Item2) * def.StartSpeed;
particleList.AddLast(p);
}
}
Register(this);
}
public void Stop()
{
Unregister(this);
m_Particles.Clear();
}
protected bool IsParticleOutofBounds(Particle particle)
{
if((int)particle.Origin.Y >= Console.WindowHeight )
{
return true;
}
if ((int)particle.Origin.Y <= 0)
{
return true;
}
if ((int)particle.Origin.X <= 0)
{
return true;
}
if ((int)particle.Origin.X >= Console.WindowWidth)
{
return true;
}
return false;
}
public void UpdateFrame(float deltaTime)
{
Vector2f deltaGravity = Gravity * deltaTime;
foreach (var def in m_Particles.Keys)
{
var particleList = m_Particles[def];
LinkedListNode<Particle> node = particleList.First;
while (node != null)
{
node.Value.Velocity += deltaGravity * def.GravityScale;
node.Value.PrevOrigin = node.Value.Origin;
node.Value.Origin += (node.Value.Velocity * deltaTime);
node.Value.Lifetime -= deltaTime;
LinkedListNode<Particle> next = node.Next;
if (IsParticleOutofBounds(node.Value) || node.Value.Lifetime <= 0)
{
node.Value.Dispose();
particleList.Remove(node);
}
node = next;
}
}
//if( m_Particles.Count <= 0 && DestroyWhenDone )
//{
// Unregister(this);//TODO: Find a real way to do this. Some one could have a reference. Wrapper struct??
//}
}
public void RenderFrame()
{
foreach (var def in m_Particles.Keys)
{
var particleList = m_Particles[def];
LinkedListNode<Particle> node = particleList.First;
while (node != null)
{
Console.CursorLeft = (int)node.Value.PrevOrigin.X;
Console.CursorTop = Console.WindowHeight - (int)node.Value.PrevOrigin.Y;
Console.Write(' ');
Console.ForegroundColor = def.Color;
Console.CursorLeft = (int)node.Value.Origin.X;
Console.CursorTop = Console.WindowHeight - (int)node.Value.Origin.Y;
Console.Write(def.Image);
node = node.Next;
}
}
}
}