1 | using System; |
---|
2 | using System.Collections.Generic; |
---|
3 | using System.Linq; |
---|
4 | using Microsoft.Xna.Framework; |
---|
5 | using Microsoft.Xna.Framework.Audio; |
---|
6 | using Microsoft.Xna.Framework.Content; |
---|
7 | using Microsoft.Xna.Framework.GamerServices; |
---|
8 | using Microsoft.Xna.Framework.Graphics; |
---|
9 | using Microsoft.Xna.Framework.Input; |
---|
10 | using Microsoft.Xna.Framework.Media; |
---|
11 | using Microsoft.Xna.Framework.Net; |
---|
12 | using Microsoft.Xna.Framework.Storage; |
---|
13 | |
---|
14 | namespace Effects |
---|
15 | { |
---|
16 | public abstract class ParticleSystem : DrawableGameComponent |
---|
17 | { |
---|
18 | private ParticleGame game; |
---|
19 | |
---|
20 | private Particle[] particles; |
---|
21 | |
---|
22 | /// <summary> |
---|
23 | /// There are variables that need to be set by subclasses |
---|
24 | /// </summary> |
---|
25 | #region Subclass variables |
---|
26 | protected Vector2 position; |
---|
27 | |
---|
28 | protected String textureName; |
---|
29 | |
---|
30 | protected String innerTextureName; |
---|
31 | protected String outerTextureName; |
---|
32 | |
---|
33 | protected float minScale; |
---|
34 | protected float maxScale; |
---|
35 | |
---|
36 | protected float rotation; |
---|
37 | |
---|
38 | protected Vector2 origin; |
---|
39 | |
---|
40 | protected Vector2 minVelocity; |
---|
41 | protected Vector2 maxVelocity; |
---|
42 | |
---|
43 | protected float minLifetime; |
---|
44 | protected float maxLifetime; |
---|
45 | |
---|
46 | #endregion |
---|
47 | |
---|
48 | private enum TextureType |
---|
49 | { |
---|
50 | Single, |
---|
51 | Dual |
---|
52 | }; |
---|
53 | |
---|
54 | private TextureType textureType; |
---|
55 | |
---|
56 | private Texture2D texture, innerTexture, outerTexture; |
---|
57 | private Vector2 textureOrigin; |
---|
58 | |
---|
59 | private int maxAmountOfParticles; |
---|
60 | public int MaxAmountOfParticles |
---|
61 | { |
---|
62 | get { return maxAmountOfParticles; } |
---|
63 | set { maxAmountOfParticles = value; } |
---|
64 | } |
---|
65 | |
---|
66 | public ParticleSystem(ParticleGame game, Vector2 position, String textureName) |
---|
67 | : base(game) |
---|
68 | { |
---|
69 | textureType = TextureType.Single; |
---|
70 | this.game = game; |
---|
71 | } |
---|
72 | |
---|
73 | public ParticleSystem(ParticleGame game, Vector2 position, String innerTextureName, String outerTextureName) |
---|
74 | : base(game) |
---|
75 | { |
---|
76 | textureType = TextureType.Dual; |
---|
77 | this.game = game; |
---|
78 | } |
---|
79 | |
---|
80 | protected abstract void InitializeParticles(); |
---|
81 | |
---|
82 | public override void Initialize() |
---|
83 | { |
---|
84 | for (int i = 0; i < maxAmountOfParticles; i++) |
---|
85 | { |
---|
86 | particles[i] = new Particle(); |
---|
87 | } |
---|
88 | base.Initialize(); |
---|
89 | } |
---|
90 | |
---|
91 | protected override void LoadContent() |
---|
92 | { |
---|
93 | particles = new Particle[maxAmountOfParticles]; |
---|
94 | if (textureType == TextureType.Single) |
---|
95 | { |
---|
96 | texture = game.Content.Load<Texture2D>(textureName.ToString()); |
---|
97 | textureOrigin.X = texture.Width / 2; |
---|
98 | textureOrigin.Y = texture.Height / 2; |
---|
99 | } |
---|
100 | if (textureType == TextureType.Dual) |
---|
101 | { |
---|
102 | innerTexture = game.Content.Load<Texture2D>(innerTextureName.ToString()); |
---|
103 | outerTexture = game.Content.Load<Texture2D>(outerTextureName.ToString()); |
---|
104 | textureOrigin.X = outerTexture.Width / 2; |
---|
105 | textureOrigin.Y = outerTexture.Height / 2; |
---|
106 | } |
---|
107 | |
---|
108 | base.LoadContent(); |
---|
109 | } |
---|
110 | |
---|
111 | public override void Update(GameTime gameTime) |
---|
112 | { |
---|
113 | |
---|
114 | base.Update(gameTime); |
---|
115 | } |
---|
116 | |
---|
117 | public override void Draw(GameTime gameTime) |
---|
118 | { |
---|
119 | game.SpriteBatch.Begin(); |
---|
120 | foreach (Particle p in particles) |
---|
121 | { |
---|
122 | if (!p.Alive) |
---|
123 | { |
---|
124 | continue; |
---|
125 | } |
---|
126 | if (textureType == TextureType.Single) |
---|
127 | { |
---|
128 | game.SpriteBatch.Draw(texture, p.Position, null, Color.White, p.Rotation, textureOrigin, p.Scale, SpriteEffects.None, 1.0f); |
---|
129 | } |
---|
130 | if (textureType == TextureType.Dual) |
---|
131 | { |
---|
132 | game.SpriteBatch.Draw(outerTexture, p.Position, null, Color.White, p.Rotation, textureOrigin, p.Scale, SpriteEffects.None, 1.0f); |
---|
133 | game.SpriteBatch.Draw(innerTexture, p.Position, null, Color.White, p.Rotation, textureOrigin, p.Scale, SpriteEffects.None, 1.0f); |
---|
134 | } |
---|
135 | } |
---|
136 | base.Draw(gameTime); |
---|
137 | } |
---|
138 | } |
---|
139 | } |
---|