Overview

The Ferpect component model builds on the XNA Framework game component model defined in Microsoft.Xna.Framework.Game. The XNA Framework defines game components with IGameComponent, IUpdateable, and IDrawable. The Ferpect component model expands on these by defining additional interfaces for both components and their hosting environment. This allows game components to be hosted in any application, as long as it implements the required interfaces.

The Ferpect component model itself is just a set of interfaces. However, the Ferpect SDK contains a number of re-usable classes that implement these interfaces.

Anatomy of the Ferpect Game Starter Kit

The game template included in the SDK provides a simple structure for a game built out of components. A ScreenManager component is a non-visible component that manages a collection of Screen components. The ScreenManager class has properties StartScreen, MainScreen, and ExitScreen. Initially, all the screen components in the ScreenManager.Screens collection are disabled, except the screen assigned to StartScreen property.

The start screen, and in fact every screen, is responsible for invoking transitions to the other screens. Transitions are invoked by calling one of the ScreenManager's methods: Call, Return, ReturnAndCall, or Goto. These methods effectively manage a stack of enabled screens. Call pushes the named screen onto the stack and Return pops the topmost screen. ReturnAndCall pops the top screen and pushes the one specified, effectively replacing the top of the stack. Goto will clear the entire stack and then push the specified screen. If there is only one screen on the stack when Return is invoked, that screen is popped off the stack and the ExitScreen is pushed onto it. If ExitScreen is null, the game exits.

The starter kit's StartScreen provides an important function. It has a BasicInputHander component that raises an event when the start button is pressed on any game pad, or when the Enter key is pressed. If a game pad is used, the StartScreen initializes the IInputService.PlayerOne property with the PlayerIndex of the controller. The input service serves as a way to expose the PlayerIndex of the game's player one to all other components in the game. Everywhere else in the game, components can query for IInputService and use its GetInputState method to retrieve the input state of the player. The input service itself is implemented by a game component that updates once per frame, ensuring that game inputs are read once and remain consistent until the next Update.

An interesting note about the BasicInputComponent is that it has events, which you can view in the Properties Window. These events are raised by the component itself in its Update method. To control when these events occur relative to the handling component, simply modify the UpdateOrder property of the BasicInputComponent.

In the Ferpect component model, there is a parent-child relationship possible between components, but this relationship doesn't impose any limitations on DrawOrder or UpdateOrder. Instead, they are provided for logical grouping and for enabling access to each other in order to share state or functionality. One such feature is auto-enabling of child components. That is, when a child component has its AutoEnable property set, the child sets its own Enabled property to match that of the parent during the parent's EnabledChanged event.

In the MainMenu screen, there's a situation where auto-enable isn't enough. Pressing the Back button or the Escape key will bring up a pop-up dialog asking the user to confirm his decision to exit. The pop-up dialog is the ExitConfirmation screen, pushed onto the stack via ScreenManager.Call. In this case, the new screen is now on top of the stack, but it doesn't cover the MainMenu screen. That means the MainMenu screen is still visible, and is both enabled and drawing.

Because the MainMenu screen is still enabled, so are its children - including the MenuInputHandler child component. In order to disable the input handler when the MainMenu is not the active screen, the MainMenu class contains an event handler for Screen.FocusChanged. Focus is a property that is set by the ScreenManager when it pushes a screen onto its stack. The ScreenManager ensures that no more than one screen at a time has Focus set to true, and whenever one does, that screen is on top of the stack.

Last edited Sep 16, 2010 at 6:54 AM by BadCorporateLogo, version 9

Comments

No comments yet.