using System; using System.Diagnostics; namespace SpaceInvaders { public class ShipMan { //------------------------------------------------------------------- // FIELDS //------------------------------------------------------------------- public enum MissileState { Ready, Flying } public enum MoveState { MoveLeft, MoveRight, BothDirections } private Ship pShip; private Missile pMissile; private ShipMissileReady pMissileReady; private ShipMissileFlying pMissileFlying; private ShipMoveLeft pMoveLeft; private ShipMoveRight pMoveRight; private ShipMoveBoth pBothDirections; private static ShipMan pInstance = null; //------------------------------------------------------------------- // PRIVATE CONSTRUCTION //------------------------------------------------------------------- private ShipMan() { //store states this.pMissileReady = new ShipMissileReady(); this.pMissileFlying = new ShipMissileFlying(); this.pMoveLeft = new ShipMoveLeft(); this.pMoveRight = new ShipMoveRight(); this.pBothDirections = new ShipMoveBoth(); //set active this.pShip = null; this.pMissile = null; } //------------------------------------------------------------------- // PUBLIC METHODS //------------------------------------------------------------------- public static void Create() { if(ShipMan.pInstance == null) { ShipMan.pInstance = new ShipMan(); Debug.Assert(ShipMan.pInstance != null); } pInstance.pShip = ActivateShip(); pInstance.pShip.SetMoveState(ShipMan.MoveState.BothDirections); pInstance.pShip.SetMissileState(ShipMan.MissileState.Ready); } public static Ship GetShip() { ShipMan pManager = ShipMan.GetInstance(); Debug.Assert(pManager != null); Debug.Assert(pManager.pShip != null); return pManager.pShip; } public static ShipMissileState GetMissileState(MissileState state) { ShipMan pManager = ShipMan.GetInstance(); ShipMissileState pState = null; switch (state) { case ShipMan.MissileState.Ready: pState = pManager.pMissileReady; break; case ShipMan.MissileState.Flying: pState = pManager.pMissileFlying; break; default: Debug.Assert(false); break; } return pState; } public static ShipMoveState GetMoveState(MoveState state) { ShipMan pManager = ShipMan.GetInstance(); ShipMoveState pState = null; switch(state) { case ShipMan.MoveState.MoveLeft: pState = pManager.pMoveLeft; break; case ShipMan.MoveState.MoveRight: pState = pManager.pMoveRight; break; case ShipMan.MoveState.BothDirections: pState = pManager.pBothDirections; break; default: Debug.Assert(false); break; } return pState; } public static Ship ActivateShip() { //get instance ShipMan pManager = ShipMan.GetInstance(); ObjectNode pObjectNode = GhostMan.FindObj(GameObject.ObjectName.Ship); Ship pShip; if (pObjectNode == null) { //copy pShip = new Ship(GameObject.ObjectName.Ship, Sprite.SpriteName.Ship, 425, 100); } else { pShip = (Ship)pObjectNode.GetObject(); GhostMan.RemoveObj(pObjectNode); pShip.ResurrectShip(425, 100); } pManager.pShip = pShip; //attach sprite to batch SpriteBatch pShipBatch = SpriteBatchMan.FindBatch(SpriteBatch.BatchName.Ships); pShipBatch.AttachBatch(pShip); //attach missile to missile root GameObject pShipRoot = ObjectNodeMan.FindObject(GameObject.ObjectName.ShipRoot); Debug.Assert(pShipRoot != null); //add to tree pShipRoot.AddComponent(pManager.pShip); pManager.pShip.ActivateCollisionSprite(SpriteBatchMan.FindBatch(SpriteBatch.BatchName.Boxes)); return pManager.pShip; } public static Missile ActivateMissile() { //get instance ShipMan pManager = ShipMan.GetInstance(); //copy Missile pMissile = null; ObjectNode pObjNode = GhostMan.FindObj(GameObject.ObjectName.Missile); //call new if it doesn't exist in ghost man if(pObjNode == null) { pMissile = new Missile(Sprite.SpriteName.Missile, 200, 100); } else { //recycle pMissile = (Missile)pObjNode.GetObject(); GhostMan.RemoveObj(pObjNode); pMissile.Resurrect(200, 100); } pManager.pMissile = pMissile; //attach to batch SpriteBatch pMissileBatch = SpriteBatchMan.FindBatch(SpriteBatch.BatchName.Missles); SpriteBatch pBoxBatch = SpriteBatchMan.FindBatch(SpriteBatch.BatchName.Boxes); pMissile.ActivateCollisionSprite(pBoxBatch); pMissile.ActivateSprite(pMissileBatch); //attach missile to missile root GameObject pMissileGroup = ObjectNodeMan.FindObject(GameObject.ObjectName.MissileGroup); Debug.Assert(pMissileGroup != null); //add to tree pMissileGroup.AddComponent(pManager.pMissile); return pManager.pMissile; } public static void GhostShips() { //get instance ShipMan pManager = ShipMan.GetInstance(); // Clear out Ships ShipRoot pRoot = (ShipRoot)ObjectNodeMan.FindObject(GameObject.ObjectName.ShipRoot); Debug.Assert(pRoot != null); Ship pShip = (Ship)ForwardIterator.GetChild(pRoot); while(pShip != null) { pShip.Remove(); pShip = (Ship)ForwardIterator.GetChild(pRoot); } // clear reference pManager.pShip = null; } //------------------------------------------------------------------- // PRIVATE METHODS //------------------------------------------------------------------- private static ShipMan GetInstance() { //if instance is null, create it if(ShipMan.pInstance == null) { ShipMan.Create(); } Debug.Assert(ShipMan.pInstance != null); return ShipMan.pInstance; } } //end class } // end namespace