123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- #region File Description
- //-----------------------------------------------------------------------------
- // Particle.cs
- //
- // Microsoft XNA Community Game Platform
- // Copyright (C) Microsoft Corporation. All rights reserved.
- //-----------------------------------------------------------------------------
- #endregion
- #region Using Statements
- using System;
- using System.Collections.Generic;
- using System.Text;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- #endregion
- namespace ParticleSample
- {
- /// <summary>
- /// particles are the little bits that will make up an effect. each effect will
- /// be comprised of many of these particles. They have basic physical properties,
- /// such as position, velocity, acceleration, and rotation. They'll be drawn as
- /// sprites, all layered on top of one another, and will be very pretty.
- /// </summary>
- public class Particle
- {
- // Position, Velocity, and Acceleration represent exactly what their names
- // indicate. They are public fields rather than properties so that users
- // can directly access their .X and .Y properties.
- public Vector2 Position;
- public Vector2 Velocity;
- public Vector2 Acceleration;
- // how long this particle will "live"
- private float lifetime;
- public float Lifetime
- {
- get { return lifetime; }
- set { lifetime = value; }
- }
- // how long it has been since initialize was called
- private float timeSinceStart;
- public float TimeSinceStart
- {
- get { return timeSinceStart; }
- set { timeSinceStart = value; }
- }
- // the scale of this particle
- private float scale;
- public float Scale
- {
- get { return scale; }
- set { scale = value; }
- }
- // its rotation, in radians
- private float rotation;
- public float Rotation
- {
- get { return rotation; }
- set { rotation = value; }
- }
- // how fast does it rotate?
- private float rotationSpeed;
- public float RotationSpeed
- {
- get { return rotationSpeed; }
- set { rotationSpeed = value; }
- }
- // is this particle still alive? once TimeSinceStart becomes greater than
- // Lifetime, the particle should no longer be drawn or updated.
- public bool Active
- {
- get { return TimeSinceStart < Lifetime; }
- }
-
- // initialize is called by ParticleSystem to set up the particle, and prepares
- // the particle for use.
- public void Initialize(Vector2 position, Vector2 velocity, Vector2 acceleration,
- float lifetime, float scale, float rotationSpeed)
- {
- // set the values to the requested values
- this.Position = position;
- this.Velocity = velocity;
- this.Acceleration = acceleration;
- this.Lifetime = lifetime;
- this.Scale = scale;
- this.RotationSpeed = rotationSpeed;
-
- // reset TimeSinceStart - we have to do this because particles will be
- // reused.
- this.TimeSinceStart = 0.0f;
- // set rotation to some random value between 0 and 360 degrees.
- this.Rotation = ParticleSampleGame.RandomBetween(0, MathHelper.TwoPi);
- }
- // update is called by the ParticleSystem on every frame. This is where the
- // particle's position and that kind of thing get updated.
- public void Update(float dt)
- {
- Velocity += Acceleration * dt;
- Position += Velocity * dt;
- Rotation += RotationSpeed * dt;
- TimeSinceStart += dt;
- }
- }
- }
|