Including the Player within a game |
Including the Player private void OnLoopySelected(object sender, EventArgs e) System.Diagnostics.Debug.Assert(screen != null, GameName, // Create the graphics items // And not showing the select screen // Fix the lights First, you want to get the SelectLoopyScreen object that fired this event, which you can do from the sender variable. Considering this event should only be fired from this object, it should never return null (thus the Assert here). You might be wondering what an assert is or why you would ever want to use such a thing. An assert is essentially an evaluation of a condition that should never be true, and if that condition is true, there is a big problem. What the Assert method does is, in debug builds, check whether that condition is TRue. If it isn't, execution proceeds normally; however, if it is true, execution halts and a dialog appears with the message you've included. The "graphics" items are created next (you will implement this method presently), the select screen should no longer be showing, and the lights should now be enabled for the level. If you remember from earlier, this method keys off the isSelectScreenShowing variable, so with it false, it enables the light for the level. private void CreateGraphicObjects(SelectLoopyScreen screen) // Store the materials for later use, and create the textures // Create our player object The code to create the sky-box mesh is the same as it was before, but now it is created only once. Because the mesh is never destroyed until the end of the application, creating it more than once is just a waste of time. When the levelMesh member is null, you can assume that the mesh hasn't been created yet and you can create it the first time. After that, the player object is created using the information from the Select Loopy screen to get the graphical content. // If the player is null, and you are on the select screen, clean up You've been working on the player for a while now, and you're probably thinking to yourself, "Self, what else do I need to do with the player?" Well, you need to declare it sometime, so how about now? // The player's object The only things left are drawing the player and updating the player. The update is simple because you already have a method in the game engine that will be called every frame. Add this section of code to your OnFrameMove method: if ((isQuitMenuShowing) || (isMainMenuShowing) || (isSelectScreenShowing)) // Update the player Obviously, if one of the user interface screens is showing, you don't need to update the player. If they are not showing, though, you simply call the player's update method. You probably noticed that you haven't declared the isQuitMenuShowing member variable yet, so go ahead and do that now: // Is the quit menu showing? You won't actually use this variable until later when you design the user interface screen for quitting, but this code allows you to compile for now. The last thing you need to do with the player is render it onscreen at the appropriate time. You also want to add the code back for rendering the sky box. Go back to the OnFrameRender method in your game engine, and notice that each of the "rendering" methods is encapsulated into a single call depending on the game state; the correct object is rendered depending on which state the game is currently in. Because no single object is used to render the game scene, you want a separate method to render the game scene so that you don't clutter your render method. Add this final else clause to your render method: else You then need to add the implementation of this method, which you will find in Listing 9.4. private void RenderGameScene(Device device, double appTime) device.Transform.World = Matrix.Scaling(15,15,15) * // Turn the zbuffer back on // Now, turn back on our light // Render the player } The first part of this method is naturally the replacement code for rendering the sky box that you removed earlier this chapter. The only new code here is the addition of the render call to the player. |
|