GAM456-SpaceInvaders / SpaceInvaders / 3_Timer / TimerEvent.cs
TimerEvent.cs
Raw
using System;
using System.Diagnostics;


namespace SpaceInvaders
{
    public class TimerEvent : DLink
    {
        //-------------------------------------------------------------------
        //  FIELDS
        //-------------------------------------------------------------------

        public enum EventName
        {
            AnimateGrid,
            AnimateOctopus,
            AnimateCrab,
            AnimateSquid,
            AnimateUFO,
            DeployUFO,

            AnimateBombDrop,
            AnimateBombZigZag,

            AnimateSprite,
            AnimateFont,

            PlayGridSound,

            ReviveShip,
            ResetLevel,
            GameOver,

            Attract1,
            Attract2,

            ChangeSelect,
            Uninitialized
        }

        public EventName name;
        private Command pCommand;
        private float triggerTime;   //global time
        private float deltaTime;     //seconds till active


        //-------------------------------------------------------------------
        //  CONSTRUCTION
        //-------------------------------------------------------------------

        public TimerEvent() : base()
        {
            this.name = EventName.Uninitialized;
            this.pCommand = null;
            this.triggerTime = 0.0f;
            this.deltaTime = 0.0f;
        }


        //-------------------------------------------------------------------
        //  PUBLIC METHODS
        //-------------------------------------------------------------------

        public void SetEvent(EventName name, Command pCommand, float deltaTriggerTime)
        {
            //check command is valid
            Debug.Assert(pCommand != null);

            //set values
            this.name = name;
            this.pCommand = pCommand;
            this.deltaTime = deltaTriggerTime;

            this.triggerTime = TimerEventMan.GetCurrTime() + deltaTriggerTime;
        }

        public void UpdateTriggerTime(float delta)
        {
            this.triggerTime += delta;
        }

        public float GetTriggerTime()
        {
            return this.triggerTime;
        }

        public float GetDeltaTime()
        {
            return this.deltaTime;
        }

        public void ProcessEvent()
        {
            //check command
            Debug.Assert(this.pCommand != null);
            
            //execute command
            this.pCommand.Execute(deltaTime);
        }

        public override bool ComparePriority(Node pNode)
        {
            //check incoming
            Debug.Assert(pNode != null);

            TimerEvent pEvent2 = (TimerEvent)pNode;
            if(this.triggerTime >= pEvent2.triggerTime)
            {
                return true;
            }

            return false;
        }

        public override bool Compare(Node pNode)
        {
            Debug.Assert(pNode != null);

            TimerEvent pEvent2 = (TimerEvent)pNode;
            if(this.name == pEvent2.name)
            {
                return true;
            }

            return false;
        }

        public override void Reset()
        {
            this.Clear();
        }

        public override object GetName()
        {
            return this.name;
        }

        public override void PrintStats()
        {
            Debug.WriteLine("   Name: {0} ({1})", this.name, this.GetHashCode());

            // Data:
            Debug.WriteLine("      Command: {0}", this.pCommand);
            Debug.WriteLine("   Event Name: {0}", this.name);
            Debug.WriteLine(" Trigger Time: {0}", this.triggerTime);
            Debug.WriteLine("   Delta Time: {0}", this.deltaTime);

            base.PrintStats();
        }


        //-------------------------------------------------------------------
        //  PRIVATE METHODS
        //-------------------------------------------------------------------

        private void Clear()
        {
            this.name = TimerEvent.EventName.Uninitialized;
            this.pCommand = null;
            this.triggerTime = 0.0f;
            this.deltaTime = 0.0f;
        }


    } //end class

} //end namespace