2 using System.Collections.Generic;
3 using Microsoft.Xna.Framework;
4 using Microsoft.Xna.Framework.Graphics;
20 private Random random =
new Random();
23 private LinkedList<Particle> particles;
26 private Queue<Particle> freeParticles;
31 public Image ParticleImage {
get;
set; }
36 public Image OuterParticleImage {
get;
set; }
41 #region Subclass variables 46 public double MinScale {
get;
set; }
50 public double MaxScale {
get;
set; }
55 public double ScaleAmount {
get;
set; }
60 public double MinVelocity {
get;
set; }
64 public double MaxVelocity {
get;
set; }
69 public double MinLifetime {
get;
set; }
74 public double MaxLifetime {
get;
set; }
79 public double MinAcceleration {
get;
set; }
84 public double MaxAcceleration {
get;
set; }
89 public double MinRotationSpeed {
get;
set; }
94 public double MaxRotationSpeed {
get;
set; }
99 public double MinRotation {
get;
set; }
104 public double MaxRotation {
get;
set; }
109 public double AlphaAmount {
get;
set; }
118 public Boolean IgnoreWind {
get;
set; }
120 private Boolean visible =
true;
121 private Boolean fadeIn =
false;
122 private Boolean fadeOut =
false;
123 private double originalAlpha = 1.0;
124 private double fadeTime = 0.0;
125 private double fadeTimePassed = 0.0;
135 fadeTimePassed = 0.0;
137 fadeTime = timeInSeconds;
149 originalAlpha = AlphaAmount;
150 fadeTimePassed = 0.0;
152 fadeTime = TimeInSeconds;
163 this.ParticleImage = particleImage;
169 InitializeParticles();
171 particles =
new LinkedList<Particle>();
172 freeParticles =
new Queue<Particle>(maxAmountOfParticles);
173 for (
int i = 0; i < maxAmountOfParticles; i++)
175 freeParticles.Enqueue(
new Particle());
193 public void AddEffect(
double x,
double y,
int numberOfParticles)
195 AddEffect(
new Vector(x, y), numberOfParticles);
200 for (
int i = 0; i < numberOfParticles && freeParticles.Count > 0; i++)
202 Particle p = freeParticles.Dequeue();
203 particles.AddLast(p);
204 InitializeParticle(p, position, angle);
215 for (
int i = 0; i < numberOfParticles && freeParticles.Count > 0; i++)
217 Particle p = freeParticles.Dequeue();
218 particles.AddLast(p);
219 InitializeParticle(p, position);
229 double angle = random.NextDouble() * MathHelper.TwoPi;
230 return new Vector(Math.Cos(angle), Math.Sin(angle));
240 Vector direction = GiveRandomDirection();
249 p.
Initialize(position, scale, rotation, rotationSpeed, velocity * direction, acceleration * direction, lifetime);
254 Vector direction = GiveRandomDirection();
256 double rotation = angle.
Degrees;
263 p.
Initialize(position, scale, rotation, rotationSpeed, velocity * direction, acceleration * direction, lifetime);
274 LinkedListNode<Particle> node = particles.First;
285 freeParticles.Enqueue(p);
286 LinkedListNode<Particle> previous = node;
288 particles.Remove(previous);
293 AlphaAmount = Math.Min(1 - fadeTimePassed / fadeTime, originalAlpha);
295 if (fadeTime <= fadeTimePassed)
304 AlphaAmount = Math.Min(fadeTimePassed / fadeTime, originalAlpha);
306 if (fadeTime <= fadeTimePassed)
308 AlphaAmount = originalAlpha;
318 static readonly Vector2 textureTopLeft =
new Vector2(0.0f, 0.0f);
319 static readonly Vector2 textureBottomLeft =
new Vector2(0.0f, 1.0f);
320 static readonly Vector2 textureTopRight =
new Vector2(1.0f, 0.0f);
321 static readonly Vector2 textureBottomRight =
new Vector2(1.0f, 1.0f);
323 static readonly Vector3 topLeft =
new Vector3(-0.5f, 0.5f, 0);
324 static readonly Vector3 bottomLeft =
new Vector3(-0.5f, -0.5f, 0);
325 static readonly Vector3 topRight =
new Vector3(0.5f, 0.5f, 0);
326 static readonly Vector3 bottomRight =
new Vector3(0.5f, -0.5f, 0);
328 static readonly VertexPositionTexture[] vertices =
331 new VertexPositionTexture(topLeft, textureTopLeft),
332 new VertexPositionTexture(bottomLeft, textureBottomLeft),
333 new VertexPositionTexture(topRight, textureTopRight),
336 new VertexPositionTexture(bottomLeft, textureBottomLeft),
337 new VertexPositionTexture(bottomRight, textureBottomRight),
338 new VertexPositionTexture(topRight, textureTopRight),
341 private static BlendState ToXnaBlendState(
BlendMode mode)
346 return BlendState.AlphaBlend;
348 return BlendState.Additive;
350 return BlendState.AlphaBlend;
354 internal void Draw(Matrix worldMatrix)
357 var effect = Graphics.BasicTextureEffect;
359 Texture2D texture = ParticleImage.XNATexture;
361 device.RasterizerState = RasterizerState.CullClockwise;
362 device.SamplerStates[0] = SamplerState.LinearClamp;
363 device.BlendState = ToXnaBlendState(
BlendMode);
365 effect.Texture = texture;
366 effect.CurrentTechnique.Passes[0].Apply();
368 if (OuterParticleImage == null)
373 double alpha = 4 * nTime * (1 - nTime) * AlphaAmount;
374 float scale = (float)(p.
Scale * (.75f + .25f * nTime * ScaleAmount));
377 Matrix.CreateScale(scale * texture.Width / texture.Height, scale, 1f)
378 * Matrix.CreateRotationZ((
float)p.
Rotation)
383 effect.Alpha = (float)alpha;
384 effect.World = matrix;
385 effect.CurrentTechnique.Passes[0].Apply();
387 device.DrawUserPrimitives<VertexPositionTexture>(PrimitiveType.TriangleList, vertices, 0, 2);
392 Texture2D outerTexture = OuterParticleImage.XNATexture;
397 double alpha = 4 * nTime * (1 - nTime) * AlphaAmount;
398 float scale = (float)(p.
Scale * (.75f + .25f * nTime * ScaleAmount));
401 Matrix.CreateScale(scale, scale, 1f)
402 * Matrix.CreateRotationZ((
float)p.
Rotation)
407 Matrix.CreateScale(0.65f, 0.65f, 1f)
410 effect.Texture = texture;
411 effect.Alpha = (float)alpha;
412 effect.World = matrix;
413 effect.CurrentTechnique.Passes[0].Apply();
415 device.DrawUserPrimitives<VertexPositionTexture>(PrimitiveType.TriangleList, vertices, 0, 2);
417 effect.Texture = outerTexture;
418 effect.World = matrix2;
419 effect.CurrentTechnique.Passes[0].Apply();
421 device.DrawUserPrimitives<VertexPositionTexture>(PrimitiveType.TriangleList, vertices, 0, 2);
double Rotation
Partikkelin kierto
Vector Position
Partikkelin sijainti
Järjestelmä partikkelien käsittelyyn
TimeSpan MaxLifetime
Partikkelin elinikä
Suuntakulma (rajoitettu -180 ja 180 asteen välille) asteina ja radiaaneina. Tietoja kulmasta: http://...
virtual void InitializeParticle(Particle p, Vector position)
Alustaa yhden partikkelin
Satunnaisgeneraattori. Luo satunnaisia arvoja, mm. lukuja, vektoreita sekä kulmia.
static double NextDouble(double min, double max)
Palauttaa satunnaisen liukuluvun parametrien
void Update(double time)
Päivittää partikkelin sijannin, nopeuden ja kierron
virtual void InitializeParticle(Particle p, Vector position, Angle angle)
TimeSpan SinceLastUpdate
Aika joka on kulunut viime päivityksestä.
void AddEffect(double x, double y, int numberOfParticles)
Lisää efektin kentälle
Sisältää tiedon ajasta, joka on kulunut pelin alusta ja viime päivityksestä.
void FadeOut(double TimeInSeconds)
Efekti hiipuu näkyvistä tietyn sekuntimäärän aikana
virtual void InitializeParticles()
Metodi joka asettaa partikkeleille attribuutit Täytyy kutsua perityistä luokista
Peliluokka reaaliaikaisille peleille.
double Degrees
Palauttaa tai asettaa kulman asteina.
override void Update(Time time)
Kutsutaan kun luokka päivitetään
virtual Vector GiveRandomDirection()
Antaa satunnaisen suunnan
void AddEffect(Vector position, int numberOfParticles)
Lisää efektin kentälle
ParticleSystem(Image particleImage, int maxAmountOfParticles)
Muodostaja
static new GraphicsDevice GraphicsDevice
bool Alive
Onko partikkelin "elossa", eli päivitetäänkö ja piirretäänkö se
double Scale
Partikkelin skaalaus
void AddEffect(Vector position, Angle angle, int numberOfParticles)
void Initialize(Vector position, double scale, double rotation, double rotationSpeed, Vector velocity, Vector acceleration, double lifetime)
Alusta partikkeli
TimeSpan Lifetime
Partikkelin tämän hetkinen ikä
Pelialueella liikkuva olio. Käytä fysiikkapeleissä PhysicsObject-olioita.
void FadeIn(double timeInSeconds)
Efekti tulee näkyviin tietyn sekuntimäärän aikana