-
Notifications
You must be signed in to change notification settings - Fork 0
/
Projectile.cs
330 lines (270 loc) · 11.4 KB
/
Projectile.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
using XNA = Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Numerics;
using System.Collections.Generic;
namespace Collab11
{
public static class Ext
{
public static XNA.Color ToColor(this Random rand)
{
return new XNA.Color()
{
R = (byte)rand.Next(150, 250),
G = (byte)rand.Next(20, 50),
B = (byte)35,
A = 250
};
}
public static Vector2 ToVector2(this Random rand)
{
float angle = rand.Next(0, 360) * (float)Math.PI;
return new Vector2
{
X = (float)Math.Cos(angle),
Y = (float)Math.Sin(angle)
};
}
public static Vector2 ToVector2Clamped(this Random rand, Vector2 Angle)
{
float angle = rand.Next((int)Angle.X, (int)Angle.Y + 1) * (float)Math.PI;
return new Vector2
{
X = (float)Math.Cos(angle),
Y = (float)Math.Sin(angle)
};
}
}
public readonly struct PositionColor
{
public readonly Vector2 Position;
public readonly XNA.Color Color;
public PositionColor(Vector2 position, XNA.Color color)
{
Position = position;
Color = color;
}
public static readonly VertexDeclaration VertexDeclaration = new VertexDeclaration(
new VertexElement[]
{
new VertexElement(0, VertexElementFormat.Vector2, VertexElementUsage.Position, 0),
new VertexElement(8, VertexElementFormat.Color, VertexElementUsage.Color, 0)
});
}
public struct Particle
{
public Vector2 Position;
public Vector2 Size;
public Vector2 Direction;
public Vector2 Destination;
public Vector2 Speed;
public int Sides;
public float Rotation;
public float LifeSpan;
public XNA.Color Stroke;
public XNA.Color Fill;
public bool IsActive;
private static Vector2[][] _vertices;
public const int MinSides = 8;
public const int MaxSides = 16;
public const int MaxVertices = ((MaxSides - 1) * 3) + 3;
static Particle()
{
_vertices = new Vector2[(MaxSides - MinSides) + 1][];
for (int i = 0, sides = Particle.MinSides; i < _vertices.GetLength(0); i++, sides++)
{
_vertices[i] = new Vector2[sides];
float angle = (float)Math.PI * 2f / (float)sides;
SetVertices(_vertices[i], angle, sides);
}
}
#region properties - methods
public readonly int Vertices => ((Sides - 1) * 3) + 3;
public readonly Matrix3x2 Transform => Matrix3x2.Identity
* Matrix3x2.CreateScale(Size)
* Matrix3x2.CreateRotation(Rotation)
* Matrix3x2.CreateTranslation(Position);
public readonly void CopyTo(PositionColor[] buffer, ref int stride)
{
Vector2[] vertices = _vertices[Sides - MinSides];
Vector2 vec = Vector2.Zero;
Matrix3x2 transform = this.Transform;
for (int i = 0; i < Sides - 1; i++)
{
vec = Vector2.Transform(vertices[i], transform);
buffer[stride + i * 3] = new PositionColor(vec, Stroke);
vec = Vector2.Transform(vertices[i + 1], transform);
buffer[stride + i * 3 + 1] = new PositionColor(vec, Stroke);
buffer[stride + i * 3 + 2] = new PositionColor(Position, Fill);
}
vec = Vector2.Transform(vertices[Sides - 1], transform);
buffer[stride + (Sides - 1) * 3] = new PositionColor(vec, Stroke);
vec = Vector2.Transform(vertices[0], transform);
buffer[stride + (Sides - 1) * 3 + 1] = new PositionColor(vec, Stroke);
buffer[stride + (Sides - 1) * 3 + 2] = new PositionColor(Position, Fill);
stride += this.Vertices; // ((Sides - 1) * 3) + 3;
}
private static void SetVertices(Vector2[] points, float angle, int sides)
{
for (int i = 0; i < sides; i++)
{
points[i].X = (float)Math.Cos(angle * i);
points[i].Y = (float)Math.Sin(angle * i);
}
}
#endregion
}
public class Particles
{
private int count;
private Particle[] particles;
private PositionColor[] vertexData;
private static Stack<Particle[]> particlesPool = new Stack<Particle[]>();
private static Stack<PositionColor[]> vertexDataPool = new Stack<PositionColor[]>();
private static Random rand = new Random();
public BasicEffect Effect { get; set; }
public bool IsActive => count > 0;
const int minTime = 1500;
const int maxTime = 2500;
public const int Capacity = 100; // max particle count = Capacity
#region fields
public Vector2 Center = new Vector2(400, 300);
Vector2 angle;
Vector2 initialDestination;
//Vector2 initialSpeed;
//Vector2 initialScale;
Vector2 scale;
Vector2 direction;
Vector2 speed;
Vector2 particlesDirection;
float dspeed;
float dscale;
float rotation = 1f;
XNA.Color fill, stroke;
XNA.Color fillVariance, strokeVariance;
#endregion
private Particles(Vector2 position, int count)
{
this.count = Math.Clamp(1, count, Capacity);
this.Center = position;
ResetColor();
}
public static Particles CreateSource(Vector2 position, int count)
{
Particles source = new Particles(position, count);
source.particles = particlesPool.Count < 1 ? new Particle[Capacity] : particlesPool.Pop();
source.vertexData = vertexDataPool.Count < 1 ? new PositionColor[Capacity * Particle.MaxVertices] : vertexDataPool.Pop();
source.EmitParticles();
return source;
}
public static void ReturnSource(ref Particles source)
{
particlesPool.Push(source.particles);
vertexDataPool.Push(source.vertexData);
source.Effect = null;
source.particles = null;
source.vertexData = null;
source = null;
}
#region methods
public void ResetColor()
{
fill.R = (byte)rand.Next(150, 256);
fill.G = (byte)rand.Next(1, 150);
fill.B = (byte)rand.Next(1, 150);
fill.A = (byte)rand.Next(100, 200);
fillVariance.R = (byte)rand.Next(1, 51);
fillVariance.G = (byte)rand.Next(1, 51);
fillVariance.B = (byte)rand.Next(1, 51);
particlesDirection = new Vector2(0f, -1f);
initialDestination = Center + particlesDirection;
}
private void EmitParticles()
{
float lifeSpan = rand.Next(minTime, maxTime);
for (int i = 0; i < count; i++)
{
particles[i] = ExplosionEmit(Center + new Vector2(rand.Next(-100, 100), rand.Next(100, 100)), lifeSpan);
}
}
public void Update(double dt)
{
//if(count == 0) EmitParticles();
for (int i = 0; i < count; i++)
{
ref Particle particle = ref particles[i];
particle.LifeSpan -= (float)dt;
particle.IsActive = particle.LifeSpan > 0 ? true : false;
if (!particle.IsActive
|| particle.Size.X <= 0f
|| particle.Size.Y <= 0f)
{
particles[i--] = particles[--count];
}
}
for (int i = 0; i < count; i++)
{
ExplosionBehaviour(ref particles[i], (float)dt);
}
}
public void Draw(GraphicsDevice graphics)
{
if (count > 0)
{
graphics.BlendState = BlendState.AlphaBlend;
Effect.Alpha = 1.0f;
Effect.VertexColorEnabled = true;
Effect.Projection = XNA.Matrix.CreateOrthographicOffCenter(0, graphics.Viewport.Width, graphics.Viewport.Height, 0, 0, 1);
Effect.CurrentTechnique.Passes[0].Apply();
int verticesCount = 0;
for (int i = 0; i < count; i++)
{
particles[i].CopyTo(vertexData, ref verticesCount);
}
graphics.DrawUserPrimitives<PositionColor>(PrimitiveType.TriangleList, vertexData, 0, verticesCount / 3, PositionColor.VertexDeclaration);
}
}
#endregion
#region behaviours
private void ExplosionBehaviour(ref Particle particle, float dt)
{
if (MathF.Abs(particle.Position.Y - particle.Destination.Y) > 2f)
{
particle.Destination.Y = particle.Position.Y + particlesDirection.Y * (Center - particle.Position).Length();
}
particle.Direction += Vector2.Normalize(particle.Destination - particle.Position) * (1 / particle.Speed.Length());
particle.Direction += direction;
particle.Direction = Vector2.Normalize(particle.Direction);
particle.Speed += speed;
particle.Rotation += rotation * dt;
particle.Size += scale;
particle.Position += particle.Direction * particle.Speed * dt / 5f;
}
public Particle ExplosionEmit(Vector2 destination, float lifeSpan)
{
Vector2 angle = this.angle;
float scaleVariance = this.dscale;
float speedVariance = this.dspeed;
Vector2 direction = angle == Vector2.Zero ? rand.ToVector2() : rand.ToVector2Clamped(angle);
float rotation = (float)Math.Atan2(direction.Y, direction.X);
scaleVariance = rand.Next(-(int)(scaleVariance * 100f), (int)((scaleVariance + 1) * 100f)) * 0.01f;
speedVariance = rand.Next(-(int)(speedVariance * 100f), (int)((speedVariance + 1) * 100f)) * 0.01f;
return new Particle
{
IsActive = true,
Sides = rand.Next(Particle.MinSides, Particle.MaxSides),
Rotation = rotation,
Stroke = stroke,
Fill = fill,
Position = Center,
LifeSpan = lifeSpan,
Size = new Vector2(rand.Next(20, 30)) + new Vector2(scaleVariance),
Direction = direction,
Speed = (destination - Center) / 100f + new Vector2(speedVariance),
Destination = destination
};
}
#endregion
}
}