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


namespace SpaceInvaders
{
    public class AnimateBombDrop : Command
    {
        //-------------------------------------------------------------------
        //  FIELDS
        //-------------------------------------------------------------------

        GameObject pAlienGrid;
        Bomb pBomb;
        Random rand = new Random();


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

        public AnimateBombDrop()
        {
            this.pAlienGrid = ObjectNodeMan.FindObject(GameObject.ObjectName.AlienGrid);
            this.pBomb = null;
        }


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

        public override void Execute(float deltaTime)
        {
            SpriteBatch pBombBatch = SpriteBatchMan.FindBatch(SpriteBatch.BatchName.Bombs);
            SpriteBatch pBoxBatch = SpriteBatchMan.FindBatch(SpriteBatch.BatchName.Boxes);

            GameObject pBombRoot = ObjectNodeMan.FindObject(GameObject.ObjectName.BombRoot);

            //if grid has 1 column
            if (ForwardIterator.GetChild(pAlienGrid) != null)
            {
                //TODO: Change 
                //find col & drop pos
                int dropIndex = rand.Next(0, ((Composite)pAlienGrid).GetNumNodes());
                GameObject pCol = ((Composite)pAlienGrid).GetNodeIndex(dropIndex);

                float x = pCol.pColObj.pCollisionRect.x + (pCol.pColObj.pCollisionRect.width * 0.05f);
                float y = pCol.pColObj.pCollisionRect.y - (pCol.pColObj.pCollisionRect.height * 0.5f);

                //random bomb type
                FallStrategy pFallStrategy = null;

                int bombType = rand.Next(0, 3);
                switch (bombType)
                {
                    case 0:
                        bombType = (int)Sprite.SpriteName.Bomb_Straight;
                        pFallStrategy = new FallStraight();
                        break;
                    case 1:
                        bombType = (int)Sprite.SpriteName.Bomb_Cross;
                        pFallStrategy = new FallDagger();
                        break;
                    case 2:
                        bombType = (int)Sprite.SpriteName.Bomb_Zigzag;
                        pFallStrategy = new FallZigzag();
                        break;
                    default:
                        Debug.Assert(false);    //yell at me
                        break;
                }

                ObjectNode pObjNode = GhostMan.FindObj(GameObject.ObjectName.Bomb);
                if (pObjNode == null)
                {
                    //spawn new bomb
                    pBomb = new Bomb(GameObject.ObjectName.Bomb, (Sprite.SpriteName)bombType, pFallStrategy, x, y);
                }
                else
                {
                    //resurrect old one w/ new position
                    pBomb = (Bomb)pObjNode.GetObject();
                    GhostMan.RemoveObj(pObjNode);

                    pBomb.ResurrectBomb(x, y);
                    pBomb.SwapBombType((Sprite.SpriteName)bombType, pFallStrategy);
                }

                //attach to batch
                pBomb.ActivateCollisionSprite(pBoxBatch);
                pBomb.ActivateSprite(pBombBatch);

                //attach to root
                pBombRoot.AddComponent(pBomb);

            }
        }


    } // end class

} // end namespace