123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297 |
- #region File Description
- //-----------------------------------------------------------------------------
- // ParticleEffect.cs
- //
- // Microsoft XNA Community Game Platform
- // Copyright (C) Microsoft Corporation. All rights reserved.
- //-----------------------------------------------------------------------------
- #endregion
- #region Using Statements
- using System;
- using System.Collections.ObjectModel;
- using System.IO;
- using System.Xml.Serialization;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Content;
- #endregion
- namespace NetRumble
- {
- /// <summary>
- /// A multi-emitter particle effect, comprised of multiple particle systems.
- /// </summary>
- public class ParticleEffect
- {
- #region Description Data
- /// <summary>
- /// The name of the particle effect.
- /// </summary>
- private string name;
- /// <summary>
- /// The particle systems in this effect.
- /// </summary>
- private Collection<ParticleSystem> particleSystems =
- new Collection<ParticleSystem>();
- #endregion
- #region Graphics Data
- /// <summary>
- /// The position of the particle effect in the world.
- /// </summary>
- private Vector2 position;
- /// <summary>
- /// The gameplay object that the system is following, if any.
- /// </summary>
- private GameplayObject followObject;
- [XmlIgnore()]
- public GameplayObject GameplayObject
- {
- get { return followObject; }
- set { followObject = value; }
- }
- #endregion
- #region Status Data
- /// <summary>
- /// If true, the particle effect is currently active.
- /// </summary>
- private bool active = false;
- [XmlIgnore()]
- public bool Active
- {
- get { return active; }
- }
- #endregion
- #region Initialization Methods
- /// <summary>
- /// Create a new particle effect.
- /// </summary>
- public ParticleEffect() { }
- /// <summary>
- /// Create a new particle effect that is a clone of another one.
- /// </summary>
- /// <returns></returns>
- public ParticleEffect Clone()
- {
- ParticleEffect clone = new ParticleEffect();
- // copy the data
- clone.name = this.name;
- clone.position = this.position;
- // copy each system
- for (int i = 0; i < this.particleSystems.Count; ++i)
- {
- clone.ParticleSystems.Add(this.particleSystems[i].Clone());
- }
- return clone;
- }
-
- /// <summary>
- /// Initialize the particle effect.
- /// </summary>
- /// <param name="content">The contenet manager that owns the texture.</param>
- public virtual void Initialize(ContentManager content)
- {
- // initialize each system
- for (int i = 0; i < particleSystems.Count; ++i)
- {
- particleSystems[i].Initialize(content);
- }
- // allow us to start updating and drawing
- active = true;
- }
- /// <summary>
- /// Reset the particle effect.
- /// </summary>
- public virtual void Reset()
- {
- // reset each system
- for (int i = 0; i < particleSystems.Count; ++i)
- {
- particleSystems[i].Reset();
- }
- // allow us to start updating and drawing
- active = true;
- }
- #endregion
- #region Updating Methods
- /// <summary>
- /// Update the particle effect.
- /// </summary>
- /// <param name="elapsedTime">The amount of elapsed time, in seconds.</param>
- public virtual void Update(float elapsedTime)
- {
- // update the position based on the follow-object, if any
- if (followObject != null)
- {
- if (followObject.Active)
- {
- Position = followObject.Position;
- }
- else
- {
- followObject = null;
- Stop(false);
- }
- }
- // update each system
- active = false;
- for (int i = 0; i < particleSystems.Count; ++i)
- {
- if (particleSystems[i].Active)
- {
- particleSystems[i].Update(elapsedTime);
- active = true;
- }
- }
- }
- #endregion
- #region Drawing Methods
- /// <summary>
- /// Draw the particle effect.
- /// </summary>
- /// <param name="spriteBatch">The SpriteBatch object used to draw.</param>
- /// <param name="blendMode">Filters the systems drawn in this pass.</param>
- public virtual void Draw(SpriteBatch spriteBatch, SpriteBlendMode blendMode)
- {
- if (!active)
- return;
- // draw each system
- for (int i = 0; i < particleSystems.Count; ++i)
- {
- if (particleSystems[i].BlendMode == blendMode)
- {
- particleSystems[i].Draw(spriteBatch);
- }
- }
- }
- #endregion
- #region Control Methods
- /// <summary>
- /// Stop the particle effect.
- /// </summary>
- /// <param name="immediately">
- /// If true, particles are no longer drawn or updated.
- /// Otherwise, only generation is halted.
- /// </param>
- public void Stop(bool immediately)
- {
- // stop all of the systems
- for (int i = 0; i < particleSystems.Count; ++i)
- {
- particleSystems[i].Stop(immediately);
- }
- // halt updating/drawing of the particles if requested
- if (immediately)
- {
- active = false;
- }
- }
- #endregion
- #region Serialization Interface
- public string Name
- {
- get { return name; }
- set { name = value; }
- }
- public Vector2 Position
- {
- get { return position; }
- set
- {
- position = value;
- for (int i = 0; i < particleSystems.Count; ++i)
- {
- particleSystems[i].Position = position;
- }
- }
- }
- public Collection<ParticleSystem> ParticleSystems
- {
- get { return particleSystems as Collection<ParticleSystem>; }
- }
- #endregion
- #region Static Initialization Methods
- /// <summary>
- /// Create a new ParticleEffect object from the data in an XML file.
- /// </summary>
- /// <param name="filepath">The filename of the XML file.</param>
- /// <returns>A new ParticleEffect object.</returns>
- public static ParticleEffect Load(string filepath)
- {
- XmlSerializer serializer = new XmlSerializer(typeof(ParticleEffect));
- return (ParticleEffect)serializer.Deserialize(File.OpenRead(filepath));
- }
- #endregion
- }
- }
|