LP2-3OTVClassStructure

=OTV Class Structure =

=3. OTV Class Structure=

3.1. The OTV Miner game concept has been laid out. The next part of the process is to define the class objects that will make up the game play. My partner and I decided on the following class structure. This class is an abstract class and is the base for all of our objects.

3.1.1. Here is the code implementation



3.1.2. The OTVGameObject has two constructors.



3.1.3. And the remaining components in the class are get/set methods to access the individual class components.



3.2. The Ship class inherits from the OTVGameObject and is used as the main object for the game. At this time not all of the methods are implemented. The main ones completed are the movement actions.



3.2.1. The class starts off by implementing the characteristics that the ship class has that are unique to it.



3.2.2. The ship class has two constructors.



3.2.3. Here are the standard get/set methods.



3.2.4. The LoadContent method sets us the sound for the ship. At this time I don’t have a sound attached to it.



3.2.5. The RotateShip gets input from the gamepad of keyboard and translates it to movement.



3.2.6. The MoveShip method takes input from the keyboard/gamepad and translates it to movement. This method works with the new quadrant class and looks for the ship to leave the screen. When the ship reaches the top of the screen, for example, the method checks if the current quadrant can exited. If it can the quadrant is changed based upon the direction you are going and the ship is drawn to the appropriate part of the screen. It also looks to see if the ship is moving (by using the up or down arrows). If it is the moving variable is set to true so the ship animation can be drawn.



3.2.7. The method continues with the key down logic.



3.3. The ELCollection class is used to define laser and element objects. It inherits from the OTVObject class and adds its own functionality. Not all of the methods are implemented. Methods will be added as needed during the development of the game.



3.3.1. Here is the implementation of the ELCollection class.



3.3.2. The constructor and get/set methods.



3.4. The Quadrant class was created so I can track movement in the game and distribute rocks, elements, and enemies in the game. It is a straight forward implementation of a square grid with odd number dimensions. The smallest size (used in testing) is a 3x3 grid with a total of nine quadrants. By setting the constants in the code you can easily change the size of the quadrant space.



3.4.1. The class starts with the main variables that keep track of the actions. The curquad member holds the current quadrant. MAXQUAD is where you set the maximum quadrant that you want. In this example I want a 3x3 so my MAXQUAD is 9. QUADOFFSET is how many quadrants you need to move when you go up or down. STARTQUAD is the middle position where you start. I also set up a random variable to put objects in the game with.



3.4.2. The constructor and property setting are straight forward. The MoveUp method subtracts the offset constant and updates the current quadrant. The CheckMoveUp method calls a private function that iterates through the quadrants to find the current row you are at. If the row is one than you can’t move up.



3.4.3. The next set of methods are the remaining movement checks. You have to verify you can move before you actually move.



3.4.4. The two private functions in the class find the current row or column based upon the current quadrant. The main part of this function is to check the modulus of the loop counter and compare it to the offset constant. If it is zero then you are at the last column and thus the next number will be the next row. The loop goes until the loop counter is equal to the current quadrant.



3.4.5. The last method returns a random quadrant for object population.



3.5. The Animation class was modeled after the class used in the book “A Simple Introduction to Game Programming With C# and XNA 3.1 (C. Bennett)”. The class offered all of the functionality that I needed to make my animation work in this project.



3.5.1. The class starts out by creating a structure object to hold each image in the animation.



3.5.2. The class defines several tracking variables. One to check to see if looping is enabled, it the animation is stopped, and if the animation is playing. The most important part of the class is the List object that holds all of the frames for the animation. I added the position, rotation and center members to fit in with my objects.



3.5.3. The add cell method is how you add each image for the animation. I modified this from the original by centering the image location. When I was first using this class everything was centered on the bottom left corner. When you create an Animation object all you have to do is pass a position variable to establish a location. There are also two ways to set the position, you can pass in the X & Y coordinates or you can vend in a Vector2 object.



3.5.4. I added the SetRotation method so I could use the ship animation in the game. The SetMoveRight and SetMoveLeft methods are not used in the game in this point. It flips the image back and forth for side to side movement. The main method in the class is the LoopAll function. It sets the looping member variable and gives a starting and ending cell. It sets the current cell and a time shift variable.



3.5.5. The loop method has a set starting point and ending point. The GotoFrame method sets the current image to the specified index.



3.5.6. The PlayAll method sets the environment to play all of the frames. The Play method starts at the given start position and ends at the specified end position.



3.5.7. The Draw method looks to see what cell is currently active and if the list is empty, before the first image, or over the total images it returns without drawing. The update method is what does all of the animation. It checks to see if the image is stopped. If so nothing happens. If the image is not stopped then it evaluates the total time against the time shift. If total time is greater than it resets the total time the variable, increments the current cell, and checks to see if looping is turned on. If it is and the cell count is at the end, it resets to the beginning. If the animation is set for one pass and it is at the end, the playing variable is turned off and the current cell is set to the last frame.



3.5.8. The last two methods are ones I added. I placed all of the image setup in here to make my code a little cleaner.



3.6. The LaserObjects class is used to store the list object for the laser shots. I was able to consolidate all of the code for laser actions in this class.



3.6.1. The class consists of a constant MAXLASER, a list of ELCollection objects, and a sound effect variable. The constructor establishes the List collection.



3.6.2. The next method is the LoadContent procedure. This method creates the List of laser objects and adds the sound asset to the sound variable. The fire laser method looks for an open slot in the list to activate. It then sets the laser object parameters equal to that of the ship that fired it. It subtracts one form the laser fuel and plays the laser fire sound.



3.6.3. The update laser method looks to see if the laser is off screen. If it is it is then deactivated. In the final game version the laser may cross quadrants, it depends on how our next meeting goes. If the laser is still in “play” The laser rectangle is passed to the RockCollection to see it there is an intersection. If there is the RockCollection method returns a true indication and the laser is deactivated.



3.6.4. The last method is the draw method. It loops through the collection and draws all active lasers.



3.7. The RockObjects Class does the same thing as the LaserObjects class. It consolidates all of the functionality of the rocks in one class for more efficient code.



3.7.1. The RockObject class is similar to the LaserObject class with one addition. It includes an Animation object for the destroyed rocks and when the ship get destroyed. 3.8.3. The rest of the LoadContent sets the sound variable up. The ReleaseElements works the same way as the one in the RockObject class.



3.7.2. The LoadContent method is a little more involved that the LasserObject LoadContent. It defines a List collection of starting points. It uses this collection to randomly place rock objects on the screen. It also determines the starting quadrant and tries to avoid adding rocks to the center of the screen.



3.7.3. Once the center is avoided, the rock is inserted into the rock collection. It uses the random quadrant established when checking for the start position. Lastly it defines the explosion Animation objects with the explosion images. It uses the same images for the rocks and the ship



3.7.4. The next part of the method is to check if it intersects the ship. If it does and the ship hit count is zero, it plays the explosion animation. To make it a little more realistic it increases the scale of the explosion by two. It also plays the explosion sound.



3.7.5. The LaserShot method uses the laser rectangle to see if it intersects with any of the rocks in the collection. It plays the animation and sound if it does. The release rocks method activates all of the rocks in the collection. This method is intended for resetting the game.



3.7.6. The Draw method draws the animation and rocks to the screen.



3.8. The ElementObjects class is the same premise as the two previous classes. It is similar to the RockObjects class in the update because it has the same random placement function. Other than that it is a very straight forward class.



3.8.1. This class has the same set up as the previous classes.



3.8.2. The LoadContent sets the elements to a list like the rock and laser classes. The main difference is it randomly selects the type of element it uses. It then sets the element type and file name for the ELCollection constructor.



3.8.3. The rest of the LoadContent sets the sound variable up. The ReleaseElements works the same way as the one in the RockObject class.



3.8.4. The UpdateElement method rotates the elements at random speeds like the RockObject class. It also look to see if the ship impacts the element.



3.8.5. If the ship intersects with the element, it adds the element to the ship element values.



3.8.6. The Draw method loops through the collection and draws elements when they are in the correct quadrant.



Project Home Next Section