Presentation is loading. Please wait.

Presentation is loading. Please wait.

Gameprogrammeren: Game states

Similar presentations


Presentation on theme: "Gameprogrammeren: Game states"— Presentation transcript:

1 Gameprogrammeren: Game states
Arjan Egges Paul Bergervoet Wouter van Toll

2 Game states Veel games hebben verschillende game toestanden Hoofdmenu
Optiemenu Helpmenu Aan het spelen Game over Highscore

3 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!

4 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.

5 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!

6 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(); }

7 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; ... }

8 Game state manager class GameStateManager : IGameLoopObject {
protected Dictionary<string, IGameLoopObject> gameStates; protected IGameLoopObject currentGameState; public GameStateManager() gameStates = new Dictionary<string, IGameLoopObject>(); currentGameState = null; } ...

9 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;

10 public void SwitchTo(string name)
{ if (gameStates.ContainsKey(name)) currentGameState = gameStates[name]; }

11 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); ...

12 In PenguinPairs Static membervariabele: Met property:
protected static GameStateManager gameStateManager; public static GameStateManager GameStateManager { get { return gameStateManager; } }

13 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");

14 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; ...

15 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"); }

16 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); }

17 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();


Download ppt "Gameprogrammeren: Game states"

Similar presentations


Ads by Google