|
Links of Interest
MainDownload Prerequisites Screenshots Articles/Code Snippets DBP Warmup Entries Blog Contact/Feedback Quick Downloads:
Diaspora
XNA Requirements Installer
External Links
Heather [layout designer]XNA |
Game Screen Extension
Using this article on game screens as a base for my game engine structure, I decided to add to the class to be more versatile than the one described in the article.
First off, I found myself wanting to pop mutiple screens off a stack, when quitting the game in pause screen for example. So I added this simple function, which utilizes Types.
public void PopUntil(Type type)
The usage is pretty simple:
{ while (screens.Count > 0) { if (screens.Peek().GetType().Equals(type)) break; else screens.Pop(); } } PopUntil(typeof(MainMenuScreen));
All it does is continue popping screens off the stack until it gets to a screen of the specified type. You could add error checking in case you specify a type that isn't contained in the stack, but I didn't feel a need to add that for my game.
Second, I added event-driven keyboard input. This is trivial to do, but it is not always an obvious thing to add. This way, you won't get duplicate commands to underlying screens. Hook the events:
KeyboardHelper.OnKeyDown += new KeyboardHelper.KeyEventHandler(KeyboardHelper_OnKeyDown);
And the actual methods:
KeyboardHelper.OnKeyPressed += new KeyboardHelper.KeyEventHandler(KeyboardHelper_OnKeyPressed); KeyboardHelper.OnKeyUp += new KeyboardHelper.KeyEventHandler(KeyboardHelper_OnKeyUp); void KeyboardHelper_OnKeyUp(Keys key)
Third, I decided to register my GameScreenManager a service so I don't need to expose the manager through my game class. Declare the interface and then extend your game screen manager class.
{ foreach (GameScreen gs in screens) { gs.OnKeyUp(key); if (gs.BlockUpdate) return; } } void KeyboardHelper_OnKeyDown(Keys key) { foreach (GameScreen gs in screens) { gs.OnKeyDown(key); if (gs.BlockUpdate) return; } } void KeyboardHelper_OnKeyPressed(Keys key) { foreach (GameScreen gs in screens) { gs.OnKeyPressed(key); if (gs.BlockUpdate) return; } } interface IGameScreenService
Then, inside my main game class, I registered my game screen service.
{ void Push(GameScreen screen); void Pop(); void PopUntil(Type type); } gameScreenManager = new GameScreenManager(this);
And to retrieve it from any class with a reference to Game:
Services.AddService(typeof(IGameScreenService), gameScreenManager); IGameScreenService gameScreenService = (IGameScreenService)game.Services.GetService(typeof(IGameScreenService));
gameScreenService.Pop(); |