using System; using System.Diagnostics; namespace SpaceInvaders { public class GameContext { //------------------------------------------------------------------- // FIELDS //------------------------------------------------------------------- static GameContext poInstance = null; readonly State pStateAttract; State pCurrState; bool isTwoPlayer; int highscore; //------------------------------------------------------------------- // PRIVATE CONSTRUCTION //------------------------------------------------------------------- private GameContext() { this.pStateAttract = new State_Attract(); Debug.Assert(this.pStateAttract != null); this.pStateAttract.SetContext(this); this.pCurrState = this.pStateAttract; this.isTwoPlayer = false; this.highscore = 0; } //------------------------------------------------------------------- // PUBLIC METHODS //------------------------------------------------------------------- public bool GetPlayMode() { return this.isTwoPlayer; } public void ChangeState(State newState) { Debug.Assert(this.pCurrState != null); Debug.Assert(newState != null); this.pCurrState.OnExit(); this.pCurrState = newState; this.pCurrState.OnEnter(); } public void Restart() { Debug.Assert(this.pStateAttract != null); ((State_Attract)this.pStateAttract).Reset(); this.ChangeState(this.pStateAttract); } public void UpdateHighScore(int newScore) { if(this.highscore < newScore) { // save new score this.highscore = newScore; Font pFont = FontMan.FindFont(Font.FontName.HighScore); pFont.UpdateMessage(this.highscore.ToString("D4")); } } public int GetHighScore() { return this.highscore; } //------------------------------------------------------------------- // PUBLIC STATIC METHODS //------------------------------------------------------------------- public static void Create() { if(GameContext.poInstance == null) { GameContext.poInstance = new GameContext(); Debug.Assert(GameContext.poInstance != null); } GameContext.poInstance.pStateAttract.Init(); GameContext.poInstance.pStateAttract.OnEnter(); } public static void SetPlayMode(bool isTwoPlayer) { GameContext context = GameContext.GetInstance(); Debug.Assert(context != null); //do we have two players? context.isTwoPlayer = isTwoPlayer; } public static State GetCurrState() { GameContext context = GameContext.GetInstance(); Debug.Assert(context != null); return context.pCurrState; } public static void ResetLevel() { GameContext context = GameContext.GetInstance(); Debug.Assert(context != null); Debug.Assert(context.pCurrState != null); ((State_Play)context.pCurrState).ResetLevel(); } public static void Update(float sysTime) { GameContext context = GameContext.GetInstance(); Debug.Assert(context != null); context.pCurrState.Update(sysTime); } public static void Draw() { GameContext context = GameContext.GetInstance(); Debug.Assert(context != null); context.pCurrState.Draw(); } //------------------------------------------------------------------- // PRIVATE METHODS //------------------------------------------------------------------- private static GameContext GetInstance() { if(GameContext.poInstance == null) { GameContext.Create(); Debug.Assert(GameContext.poInstance != null); } return GameContext.poInstance; } } // end class } // end namespace