]> git.r.bdr.sh - rbdr/super-polarity/blame_incremental - SuperPolarity/ParticleEngine.cs
Update README
[rbdr/super-polarity] / SuperPolarity / ParticleEngine.cs
... / ...
CommitLineData
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; }
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;
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();
29 Color = Color.Red;
30 TTL = 20;
31 TTLRandomFactor = 40;
32 StretchFactor = 1;
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));
42
43 float angle = 0;
44 float angularVelocity = 0.1f * (float)(random.NextDouble() * 2 - 1);
45 Color color = Color;
46 float size = (float)random.NextDouble() * StretchFactor;
47
48 position.X += random.Next(-ScatterFactor, ScatterFactor);
49 position.Y += random.Next(-ScatterFactor, ScatterFactor);
50
51 int ttl = TTL + random.Next(TTLRandomFactor);
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}