#region Using directives

using System;
using System.Drawing;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

#endregion

namespace PunkMan.Game
{
    /// <summary>
    /// Particle class
    /// </summary>
    public class Particle
    {
        /// <summary>
        /// particle texture
        /// </summary>
        private Texture tex;
        /// <summary>
        /// particle position
        /// </summary>
        private Vector3 position;
        /// <summary>
        /// particle direction
        /// </summary>
        private Vector3 direction;
        /// <summary>
        /// particle time of death
        /// </summary>
        private float deathTime;
        /// <summary>
        /// particle speed
        /// </summary>
        private float speed;
        
        /// <summary>
        /// Direct3D device
        /// </summary>
        private Device dev;
        /// <summary>
        /// is particle dead?
        /// </summary>
        private bool dead = false;
        /// <summary>
        /// particle vertex buffer
        /// </summary>
        private VertexBuffer vb = null;

        /// <summary>
        /// Is particle dead
        /// </summary>
        /// <value>dead?</value>
        public bool Dead
        {
            get { return dead; }
        }

        /// <summary>
        /// Mighty constructor
        /// </summary>
        /// <param name="dev">Direct3D device</param>
        /// <param name="vb">particle vertex buffer</param>
        /// <param name="tex">particle texture</param>
        /// <param name="pos">particle position</param>
        /// <param name="dir">particle direction</param>
        /// <param name="speed">particle speed</param>
        /// <param name="death">particle live</param>
        public Particle(Device dev, VertexBuffer vb, Texture tex, Vector3 pos, 
            Vector3 dir, float speed, float death)
        {
            this.vb = vb;
            this.dev = dev;
            this.tex = tex;
            position = pos;
            direction = dir;
            this.speed = speed;
            deathTime = Game.Time() + death;

            
        }

        /// <summary>
        /// Draw particle
        /// </summary>
        public void Render()
        {
            if (dead) return;
            Matrix world = dev.Transform.World;
            Matrix oldWorld = world;
            world.Multiply(Matrix.Translation(position));
            world.M11 = world.M22 = world.M33 = 0.25f;
            world.M12 = world.M13 = 0;
            world.M21 = world.M23 = 0;
            world.M31 = world.M32 = 0;
            dev.Transform.World = world;
            dev.SetTexture(0, tex);
            dev.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2);

            dev.Transform.World = oldWorld;

        }

        /// <summary>
        /// time of last particle update
        /// </summary>
        private float lastTime = -1;
        /// <summary>
        /// update particle
        /// </summary>
        public void Update()
        {
            if (dead) return;
            if (lastTime > 0)
            {
                float delta = Game.Time() - lastTime;
                Vector3 v = direction;
                v.Multiply(delta * speed);
                position.Add(v);

            }
            lastTime = Game.Time();
            if (lastTime > deathTime) dead = true;
        }
    }
}
