GAM456-SpaceInvaders / SpaceInvaders / 7_GameState / State_Attract.cs
State_Attract.cs
Raw
using System;
using System.Diagnostics;


namespace SpaceInvaders
{
    public class State_Attract : State
    {
        //-------------------------------------------------------------------
        //  FIELDS
        //-------------------------------------------------------------------

        SpriteBatchMan poSpriteBatchMan;
        ObjectNodeMan poObjNodeMan;
        FontMan poFontMan;
        TimerEventMan poTimerMan;

        Sprite pUFOSprite;
        Sprite pOctopusSprite;

        Attract1 attract;
        State nextState;


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

        public State_Attract() : base()
        {  }


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

        public override void Init()
        {
            //---------------------------------------------------------------
            // LOAD MANAGERS
            //---------------------------------------------------------------

            this.poSpriteBatchMan = new SpriteBatchMan();
            Debug.Assert(this.poSpriteBatchMan != null);
            SpriteBatchMan.SetActiveMan(this.poSpriteBatchMan);

            this.poObjNodeMan = new ObjectNodeMan();
            Debug.Assert(this.poObjNodeMan != null);
            ObjectNodeMan.SetActiveMan(this.poObjNodeMan);

            this.poFontMan = new FontMan();
            Debug.Assert(this.poFontMan != null);
            FontMan.SetActiveMan(this.poFontMan);

            this.poTimerMan = new TimerEventMan();
            Debug.Assert(this.poTimerMan != null);
            
            TimerEventMan.SetActiveMan(this.poTimerMan);
            TimerEventMan.PauseAllEvents();


            //---------------------------------------------------------------
            // LOAD SPRITE BATCH
            //---------------------------------------------------------------

            SpriteBatch pAlienBatch = SpriteBatchMan.AddBatch(SpriteBatch.BatchName.Aliens, 1);
            pAlienBatch.SetToggle(false);   //don't draw until we get to attract 2 screen

            SpriteBatchMan.AddBatch(SpriteBatch.BatchName.Text_UI, 2);
            SpriteBatchMan.AddBatch(SpriteBatch.BatchName.Text_Attract1, 3);
            SpriteBatchMan.AddBatch(SpriteBatch.BatchName.Text_Attract2, 4);


            //---------------------------------------------------------------
            // LOAD ALIENS
            //---------------------------------------------------------------

            //TODO: move to factory <special create method>
            AlienGrid pAliens = new AlienGrid(GameObject.ObjectName.AlienGrid, Sprite.SpriteName.NullObject, 0.0f, 0.0f);
            pAliens.ActivateSprite(pAlienBatch);
            ObjectNodeMan.AttachObject(pAliens);

            UFOAlien pUFO = new UFOAlien(Sprite.SpriteName.UFO, 325, 340);
            pUFO.ActivateSprite(pAlienBatch);

            SquidAlien pSquid = new SquidAlien(Sprite.SpriteName.Squid, 320, 270);
            pSquid.ActivateSprite(pAlienBatch);

            CrabAlien pCrab = new CrabAlien(Sprite.SpriteName.Crab, 320, 200);
            pCrab.ActivateSprite(pAlienBatch);

            OctopusAlien pOctopus = new OctopusAlien(Sprite.SpriteName.Octopus, 320, 130);
            pOctopus.ActivateSprite(pAlienBatch);

            pAliens.AddComponent(pUFO);
            pAliens.AddComponent(pSquid);
            pAliens.AddComponent(pCrab);
            pAliens.AddComponent(pOctopus);


            //change color just for attract screen
            pUFOSprite = SpriteMan.FindSprite(Sprite.SpriteName.UFO);
            Debug.Assert(pUFOSprite != null);
            pUFOSprite.ChangeColor(Sprite.pWhite);

            pOctopusSprite = SpriteMan.FindSprite(Sprite.SpriteName.Octopus);
            Debug.Assert(pOctopusSprite != null);
            pOctopusSprite.ChangeColor(Sprite.pGreen);


            //---------------------------------------------------------------
            // LOAD FONT
            //---------------------------------------------------------------

            FontMan.AddFont(Font.FontName.P1_Score_Title, Glyph.GlyphName.InvaderFont, SpriteBatch.BatchName.Text_UI, "SCORE<1>", 40, 980, 3.9f, 4.4f, 1.35f);
            FontMan.AddFont(Font.FontName.P1_Score, Glyph.GlyphName.InvaderFont, SpriteBatch.BatchName.Text_UI, "0000", 90, 930, 3.9f, 4.4f, 1.35f);

            FontMan.AddFont(Font.FontName.P2_Score_Title, Glyph.GlyphName.InvaderFont, SpriteBatch.BatchName.Text_UI, "SCORE<2>", 620, 980, 3.9f, 4.4f, 1.35f);
            FontMan.AddFont(Font.FontName.P2_Score, Glyph.GlyphName.InvaderFont, SpriteBatch.BatchName.Text_UI, "0000", 680, 930, 3.9f, 4.4f, 1.35f);

            FontMan.AddFont(Font.FontName.HighScore_Title, Glyph.GlyphName.InvaderFont, SpriteBatch.BatchName.Text_UI, "HI-SCORE", 330, 980, 3.9f, 4.4f, 1.35f);
            FontMan.AddFont(Font.FontName.HighScore, Glyph.GlyphName.InvaderFont, SpriteBatch.BatchName.Text_UI, "0000", 380, 930, 3.9f, 4.4f, 1.35f);

            FontMan.AddFont(Font.FontName.GameCredits, Glyph.GlyphName.InvaderFont, SpriteBatch.BatchName.Text_UI, "CREDIT 00", 570, 40, 3.9f, 4.4f, 1.35f);


            //---------------------------------------------------------------
            // LOAD INPUT
            //---------------------------------------------------------------

            nextState = new State_Play();
            Debug.Assert(nextState != null);
            nextState.Init();
        }

        public override void OnEnter()
        {
            InputSubject pInputSubject = InputMan.GetKey1();
            pInputSubject.Attach(new StateObserver(this));

            //pInputSubject = InputMan.GetKey2();
            //pInputSubject.Attach(new StateObserver(this));  

            //make sure proper managers are active
            SpriteBatchMan.SetActiveMan(this.poSpriteBatchMan);
            ObjectNodeMan.SetActiveMan(this.poObjNodeMan);
            FontMan.SetActiveMan(this.poFontMan);
            TimerEventMan.SetActiveMan(this.poTimerMan);
            Simulation.SetSimState(Simulation.SimState.Realtime);

            if (pContext.GetHighScore() > 0)
            {
                Font pFont = FontMan.FindFont(Font.FontName.HighScore);
                Debug.Assert(pFont != null);

                pFont.UpdateMessage(pContext.GetHighScore().ToString("D4"));
            }

            //update time on events
            TimerEventMan.UnpauseEvents();
            if(this.attract == null)
            {
                attract = new Attract1(0.1f);
            }
            TimerEventMan.AddEvent(TimerEvent.EventName.Attract1, attract, 2.0f);
        }

        public override void Update(float sysTime)
        {
            InputMan.Update();
            Simulation.Update(sysTime);

            if (Simulation.GetTimestep() > 0.0f)
            {
                ObjectNodeMan.UpdateObject();
                TimerEventMan.ProcessEvent(Simulation.GetTotalTime());
            }
        }

        public override void Draw()
        {
            SpriteBatchMan.DrawBatch();
        }

        public override void OnExit()
        {
            // Unattach input
            InputSubject pInput = InputMan.GetKey1();
            pInput.DetachInputs();

            TimerEventMan.PauseAllEvents();

            //make sure colors are changed
            pUFOSprite.ChangeColor(Sprite.pRed);
            pOctopusSprite.ChangeColor(Sprite.pWhite);

            SpriteBatchMan.UnactivateMan();
            FontMan.UnactivateMan();
            TimerEventMan.UnactivateMan();
            ObjectNodeMan.UnactivateMan();
        }

        public override void Handle()
        {
            State.pContext.ChangeState(nextState);
        }

        public void Reset()
        {
            PlayerMan.ResetPlayers();

            FontMan.SetActiveMan(this.poFontMan);
            if(pContext.GetPlayMode())
            {
                Font pFont = FontMan.FindFont(Font.FontName.P1_Score);
                pFont.UpdateMessage("0000");

                pFont = FontMan.FindFont(Font.FontName.P2_Score);
                pFont.UpdateMessage("0000");
            }
            else
            {
                Font pFont = FontMan.FindFont(Font.FontName.P1_Score);
                pFont.UpdateMessage("0000");

            }
            FontMan.UnactivateMan();


            Debug.Assert(this.nextState != null);
            ((State_Play)this.nextState).Reset();
        }

    } // end class

} // end namespace