Gameprogrammeren: Game states Arjan Egges Paul Bergervoet Wouter van Toll
Game states Veel games hebben verschillende game toestanden Hoofdmenu Optiemenu Helpmenu Aan het spelen Game over Highscore …
goede klassenstructuur! Game states Noemen we ook wel: Game states Hoe representeren we die toestanden? Hoe wisselen we tussen toestanden? Maak gebruik van een goede klassenstructuur!
Ad hoc manier in Jewel Jam public override void HandleInput(InputHelper inputHelper) { if (title.Visible) { if (inputHelper.KeyPressed(Keys.Space)) title.Visible = false; } else if (this.GameOver) this.Reset(); else ... Per game state andere invoer afhandelen.
Ad hoc manier in Jewel Jam Zelfde idee in Update. public override void Update(GameTime gameTime) { if (this.GameOver && !gameover.Visible) gameover.Visible = true; JewelJam.AssetManager.playSound("snd_gameover"); } else if (!helpFrame.Visible && !title.Visible) base.Update(gameTime); Kan tot verwarring leiden!
Algemeen game loop object Definieer een interface met daarin alle belangrijke game loop methoden. interface IGameLoopObject { void HandleInput(InputHelper inputHelper); void Update(GameTime gameTime); void Draw(GameTime gameTime, SpriteBatch spriteBatch); void Reset(); }
Klassen implementeren de interface Ook de GameObject klasse! class GameObject : IGameLoopObject { protected GameObject parent; protected Vector2 position, velocity; protected int layer; protected string id; protected bool visible; ... }
Game state manager class GameStateManager : IGameLoopObject { protected Dictionary<string, IGameLoopObject> gameStates; protected IGameLoopObject currentGameState; public GameStateManager() gameStates = new Dictionary<string, IGameLoopObject>(); currentGameState = null; } ...
Game state manager public void AddGameState(string name, IGameLoopObject state) { gameStates[name] = state; } public IGameLoopObject GetGameState(string name) return gameStates[name]; public IGameLoopObject CurrentGameState get return currentGameState;
public void SwitchTo(string name) { if (gameStates.ContainsKey(name)) currentGameState = gameStates[name]; }
Game state manager public void HandleInput(InputHelper inputHelper) { if (currentGameState != null) currentGameState.HandleInput(inputHelper); } public void Update(GameTime gameTime) { currentGameState.Update(gameTime); public void Draw(GameTime gameTime, SpriteBatch spriteBatch) { currentGameState.Draw(gameTime, spriteBatch); ...
In PenguinPairs Static membervariabele: Met property: protected static GameStateManager gameStateManager; public static GameStateManager GameStateManager { get { return gameStateManager; } }
class TitleMenuState : GameObjectList { protected Button playButton, optionButton, helpButton; public TitleMenuState() { // create the game objects... } public override void HandleInput(InputHelper inputHelper) { base.HandleInput(inputHelper); if (playButton.Pressed) PenguinPairs.GameStateManager.SwitchTo("levelMenu"); else if (optionButton.Pressed) PenguinPairs.GameStateManager.SwitchTo("optionsMenu"); else if (helpButton.Pressed) PenguinPairs.GameStateManager.SwitchTo("helpState");
class OptionsMenuState : GameObjectList { protected Button backButton; protected Slider musicVolumeSlider; protected OnOffButton onOffButton; public override void HandleInput(InputHelper inputHelper) { base.HandleInput(inputHelper); if (backButton.Pressed) PenguinPairs.GameStateManager.SwitchTo("titleMenu"); } public override void Update(GameTime gameTime) { base.Update(gameTime); MediaPlayer.Volume = musicVolumeSlider.Value; ...
In PenguinPairs protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); screen = new Point(1200, 900); this.SetFullScreen(false); gameStateManager.AddGameState("titleMenu", new TitleMenuState()); gameStateManager.AddGameState("optionsMenu", new OptionsMenuState()); gameStateManager.AddGameState("levelMenu", new LevelMenuState()); gameStateManager.AddGameState("helpState", new HelpState()); gameStateManager.SwitchTo("titleMenu"); }
In PenguinPairs protected void HandleInput() { inputHelper.Update(); if (inputHelper.KeyPressed(Keys.Escape)) this.Exit(); if (inputHelper.KeyPressed(Keys.F5)) SetFullScreen(!graphics.IsFullScreen); gameStateManager.HandleInput(inputHelper); }
In PenguinPairs protected override void Update(GameTime gameTime) { HandleInput(); gameStateManager.Update(gameTime); } protected override void Draw(GameTime gameTime) GraphicsDevice.Clear(Color.Black); spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, spriteScale); gameStateManager.Draw(gameTime, spriteBatch); spriteBatch.End();