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