]> git.r.bdr.sh - rbdr/super-polarity/blob - Super Polarity/ParticleEngine.cs
5cf6bdb2dc7c6215cad1dc8a3d9e30bc6ef0aa34
[rbdr/super-polarity] / Super Polarity / ParticleEngine.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using Microsoft.Xna.Framework;
6 using Microsoft.Xna.Framework.Graphics;
7
8 namespace SuperPolarity
9 {
10 class ParticleEngine
11 {
12 private Random random;
13 public Vector2 EmitterLocation { get; set; }
14 private List<Particle> particles;
15 private List<Texture2D> textures;
16
17 public ParticleEngine(List<Texture2D> textures, Vector2 location)
18 {
19 EmitterLocation = location;
20 this.textures = textures;
21 this.particles = new List<Particle>();
22 random = new Random();
23 }
24
25 private Particle GenerateNewParticle()
26 {
27 Texture2D texture = textures[random.Next(textures.Count)];
28 Vector2 position = EmitterLocation;
29 Vector2 velocity = new Vector2(
30 1f * (float)(random.NextDouble() * 2 - 1),
31 1f * (float)(random.NextDouble() * 2 - 1));
32 float angle = 0;
33 float angularVelocity = 0.1f * (float)(random.NextDouble() * 2 - 1);
34 Color color = new Color(
35 (float)random.NextDouble(),
36 (float)random.NextDouble(),
37 (float)random.NextDouble());
38 float size = (float)random.NextDouble();
39
40 int ttl = 20 + random.Next(40);
41
42 return new Particle(texture, position, velocity, angle, angularVelocity, color, size, ttl);
43 }
44
45 public void Update()
46 {
47 int total = 10;
48
49 for (int i = 0; i < total; i++)
50 {
51 particles.Add(GenerateNewParticle());
52 }
53
54 for (int particle = 0; particle < particles.Count; particle++)
55 {
56 particles[particle].Update();
57 if (particles[particle].TTL <= 0)
58 {
59 particles.RemoveAt(particle);
60 particle--;
61 }
62 }
63 }
64
65 public void Draw(SpriteBatch spriteBatch)
66 {
67 //spriteBatch.Begin();
68 for (int index = 0; index < particles.Count; index++)
69 {
70 particles[index].Draw(spriteBatch);
71 }
72 //spriteBatch.End();
73 }
74 }
75 }