What you're probably thinking now is "What files?" Aside from the fact that numerous levels are included on the accompanying CD, I haven't yet mentioned how these files would be constructed. In Appendix A, "Developing a Level Creator," you will see an application that will act as the level creator for this game. Obviously, level creation is an important aspect of creating a top-notch game, but it isn't central to the 3D game development aspect this book covers, so it resides in the appendix. You might also notice that you never actually check whether the file exists. Add this public method to your class so the game engine can detect whether a given level exists before it tries to load it: public static bool DoesLevelExist(int level) The file format itself is a relatively simple binary file. The first four bytes of the file are the maximum time (in seconds) you have to complete the level. It is stored in a float variable, and the constructor creates a new byte array of the size of a float variable. This is the code that required the use of the unsafe keyword. After that byte array is filled, the BitConverter class is used to take that array and convert it back to the float value it is intended to be. private bool UpdatePlayerAndBlock(bool force, int newPlayerIndex) if (force) // Update the color of this block This code assumes that the player index is only on a currently valid block. Later, outside of the call to this method in the constructor, you will be checking for valid moves anyway, so the only time it's possible for this index to be invalid is in the constructor. Considering it comes from the file that describes the level (and the level creator does not allow the starting position to be an invalid block), this shouldn't be a problem. The force parameter is only used during the constructor, and it signifies that you want to move the player instantly to the position if true. Otherwise, the MoveTo method on the player is called, and if it is not successful, it returns that "failure" to the caller of this method. At the end of the method, the selected block is moved to the next available color. If the move was invalid, the method will have already returned and will never get to this code. The constructor ignores the return value because there is no way for the method to return any value other than TRue if the force parameter is true.
|
|