Time for action - building the Gemstone class

  1. Add a new class file called Gemstone.cs to the Gemstone Hunter project.
  2. Add the following using directives to the Gemstone class:
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Content;
    using Microsoft.Xna.Framework.Graphics;
    using Tile_Engine;
    
  3. Modify the declaration of the Gemstone class to make the class public, and derive it from the GameObject class:
    public class Gemstone : GameObject
    
  4. Add a constructor for the Gemstone class:
    #region Constructor
    public Gemstone(ContentManager Content, int cellX, int cellY)
    {
    worldLocation.X = TileMap.TileWidth * cellX;
    worldLocation.Y = TileMap.TileHeight * cellY;
    frameWidth = TileMap.TileWidth;
    frameHeight = TileMap.TileHeight;
    animations.Add("idle",
    new AnimationStrip(
    Content.Load<Texture2D>(@"TexturesGem"),
    48,
    "idle"));
    animations["idle"].LoopAnimation = true;
    animations["idle"].FrameLength = 0.15f;
    PlayAnimation("idle");
    drawDepth = 0.875f;
    CollisionRectangle = new Rectangle(9, 24, 30, 24);
    enabled = true;
    } #endregion
    
  5. Back in the LevelManager class, add a declaration to hold a list of Gemstones:
    private static List<Gemstone> gemstones = new List<Gemstone>();
    
  6. In the LoadLevel() method of the LevelManager class, right after the call to TileMap.LoadMap(), clear the gemstones list:
    gemstones.Clear();
    
  7. Still in the LoadLevel() method, add a condition to the loop that examines the code values in each square to check for GEM codes. This can be placed right after the condition for START codes:
    if (TileMap.CellCodeValue(x, y) == "GEM")
    {
    gemstones.Add(new Gemstone(Content, x, y));
    }
    
  8. Add a new Update() method to the LevelManager class:
    public static void Update(GameTime gameTime)
    {
    foreach (Gemstone gemstone in gemstones)
    {
    gemstone.Update(gameTime);
    }
    }
    
  9. Add a new Draw() method to the LevelManager class:
    public static void Draw(SpriteBatch spriteBatch)
    {
    foreach (Gemstone gem in gemstones)
    gem.Draw(spriteBatch);
    }
    
  10. In the Game1 class, call the LevelManager's Update() method after the player has been updated:
    LevelManager.Update(gameTime);
    
  11. Still in the Game1 class, modify the Draw() method to include a call to draw the level manager right after the player has been drawn:
    LevelManager.Draw(spriteBatch);
    
  12. Execute the Gemstone Hunter application:
Time for action - building the Gemstone class

What just happened?

Because our GameObject class provides all of the behavior we need for gemstones, our Gemstone class only needs a constructor, which will load the appropriate AnimationStrip and initialize its location and collision area.

We have updated the LoadLevel() method in the LevelManager class to scan the level for GEM code values and create gems at these locations. Since the Gemstone class defines no movement capabilities, the gemstones will remain stationary in the blocks they spawn in, not subject to the whims of gravity.

Scoring

The player needs to be able to pick up the gemstones, and receive points for doing so. In order to allow for this, we will need to make a handful of additions to the Player class, and further modify the LevelManager class to detect collisions between the player and the gemstones spawned in the world.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.29.145