#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 Spacewar
{
///
/// Represents a single particle
///
public class Particle : SceneItem
{
///
/// The color of this particle
///
private Vector4 color;
private Vector4 startColor;
private Vector4 endColor;
private TimeSpan endTime = TimeSpan.Zero;
private TimeSpan lifetime;
#region Properties
public Vector4 Color
{
get
{
return color;
}
}
#endregion
///
/// Creates a new particle, with a start and end color and a velocity
///
/// Instance of the game we are drawing fonts for
/// Start position
/// Velocity
/// Start Color including alpha
/// End Color including alpha
/// How long in seconds before it fades and disappears. This time will transition through the start/end color cycle
public Particle(Game game, Vector2 position, Vector2 velocity, Vector4 startColor, Vector4 endColor, TimeSpan lifetime)
: base(game, new Vector3(position, 0.0f))
{
Velocity = new Vector3(velocity, 0.0f);
this.startColor = startColor;
this.endColor = endColor;
this.lifetime = lifetime;
}
///
/// Update all this particle
///
/// Current game time
/// Elapsed time since last update
public override void Update(TimeSpan time, TimeSpan elapsedTime)
{
//Start the animation 1st time round
if (endTime == TimeSpan.Zero)
{
endTime = time + lifetime;
}
//End the animation when its time is due as long as lifet
if (time > endTime)
{
Delete = true;
}
//Fade between the colors
float percentLife = (float)((endTime.TotalSeconds - time.TotalSeconds) / lifetime.TotalSeconds);
color = Vector4.Lerp(endColor, startColor, percentLife);
//Do any velocity moving
base.Update(time, elapsedTime);
}
}
}