Jump to content

Game Creation with XNA/Programming/Reusable Components

From Wikibooks, open books for an open world

Reusable Components

[edit | edit source]

Overview

[edit | edit source]

There are many components out there that could be easily used in other games. An example is a 3D Radar Heads Up Display [3D Radar HUD]. In this chapter we want to show some of the most common ones, and especially show links where to find lots of these components. Afterwards we are going to say some words about how to create your own game component using XNA Framework that can be later reused.

Examples

[edit | edit source]

Game State Management

[edit | edit source]

The Game State Management example represents the menu system of the game and reacts on the user input by switching the screens. The starting point is the main menu with three entries: Play, Options and Exit.

In this example, there are several instances of the class GameScreen that are managed by the ScreenManager class. The GameScreen is an abstract class and with its Update, HandleInput and Draw methods creates a base for all other screens that are used in the menu system. The other classes representing different screens extend the GameScreen class. The actual gameplay is also a screen and must be set in the class GameplayScreen.

The MenuEntry is a helper class and is used to create a single entry of the menu (class MenuScreen) which sends an event OnSelectEntry when being selected. In this example the menu entry is just a string, but you can modify the representation according to your game design. An object of the MenuScreen class will have a collection of the menu entries and an index of the currently selected entry.

There is an instance of the ScreenManager class in the Game class created and two screens are added: the first one for the background and the second one for the main menu.

You can also find some another examples of the main menu in the Links chapter below, including the similar solution for multiplayer networked game containing menus to maintain the sessions and the error handling.

Heads Up Display (HUD)

[edit | edit source]

Score, Life, Health Bar ...

[edit | edit source]

Each game contain several elements that help the player to keep track on the progress. For example, if you got some bonuses, they will be shown on the screen. There other examples could be the health bar, the number of lifes and the score counter. All of them are the common part of a game and can be implemented using the game components.

There is a reusable library XNA Re-usable UI Components that provides these components. It consists of four classes:

  • Bar
  • Counter
  • Timer
  • GenericComponent

To be able to use the components, download the library, unzip the .dll file and add it to your project as a reference. Now you can create an object of the class you need and set the property values. These are, for example, bar position, score value, etc. In the Draw method of the class Game you can now add a call to the instance Draw method.

The library also provides event handling: if minimum or maximum value is reached, an event will be sent. These events can be overriden, so you can decide what should happen if the player has no lives or no fuel anymore.

The detailed documentation for the library can be found here.

3D Radar

[edit | edit source]

3D Radar HUD is another example of the HUD that shows how to integrate a 3D Radar into the 3D game using 2D Heads Up Display.


Creating a reusable component

[edit | edit source]

OK, we have learned that it is often a very good idea to create a game component if you are writing something that you will probably need in your next project. Now let's talk about how to do it. XNA Framework provides some classes for this purpose and using them you will be able to make a new game component that you can later reuse and/or share.

To do it, create a class that extends either the Microsoft.Xna.Framework.GameComponent or the Microsoft.Xna.Framework.DrawableGameComponent class. In the class constructor you have to pass a reference to the Game instance as a parameter.

You should derive you class from the GameComponent class if it contains functions working with user input, for example, react on pressing a specific key. In this case there will be two methods to override:

  • Initialize
  • Update

The DrawableGameComponent class should be used if there are some content to be drawn on the screen. It extends the previous one and have some more methods, including:

  • LoadContent
  • UnLoadContent
  • Draw

There are some tutorials here that you may want to review in order to learn more about creating game components and to find some examples:

Where to find more samples?

[edit | edit source]
[edit | edit source]

Some of the resources listed below contain complete projects that can be downloaded and used in your games. However, there are also some tutorials showing the process of creating a particular component.

User Interface Elements

[edit | edit source]

Game Menu

[edit | edit source]

Heads Up Display

[edit | edit source]

Authors

[edit | edit source]

Maria (wiki login: jasna)