Gemstone.cs
to the Gemstone Hunter project. using
directives to the Gemstone class:using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Graphics; using Tile_Engine;
public class Gemstone : GameObject
#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
private static List<Gemstone> gemstones = new List<Gemstone>();
LoadLevel()
method of the LevelManager class, right after the call to TileMap.LoadMap()
, clear the gemstones list:gemstones.Clear();
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)); }
Update()
method to the LevelManager class:public static void Update(GameTime gameTime) { foreach (Gemstone gemstone in gemstones) { gemstone.Update(gameTime); } }
Draw()
method to the LevelManager class:public static void Draw(SpriteBatch spriteBatch) { foreach (Gemstone gem in gemstones) gem.Draw(spriteBatch); }
Update()
method after the player has been updated:LevelManager.Update(gameTime);
Draw()
method to include a call to draw the level manager right after the player has been drawn:LevelManager.Draw(spriteBatch);
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.
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.
3.141.29.145