using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Graphics; namespace CarFire { /// /// Flag options to define attributes for individual tiles. /// [Flags] public enum TileFlags { Open = 0x01, Closed = 0x02, Floor = 0x04, Wall = 0x08, Default = 0x05 } /// /// Small wrapper around a texture to provide easy access to /// tile rectangles. /// public class Tilemap { #region Public Properties /// /// Get the texture for this tilemap. /// public Texture2D Texture { get { return mTexture; } } #endregion #region Public Methods /// /// Construct a tilemap with a texture and dimensions in /// tiles. /// /// The texture. /// Number of tiles across. /// Number of tiles down. public Tilemap(Texture2D texture, int width, int height) { mTexture = texture; mWidth = width; mHeight = height; mTileW = mTexture.Width / mWidth; mTileH = mTexture.Height / mHeight; } /// /// Get a tile rectangle from a tile coordinate. /// /// Tile coordinates; [0,0] being the /// top-left tile. /// Rectangle surrounding the tile. public Rectangle GetRectangleForTile(Point point) { return GetRectangleForTile(point.X, point.Y); } /// /// Get a tile rectangle from a tile coordinate /// /// X-coordinate. /// Y-coordinate. /// Rectangle surrounding the tile. public Rectangle GetRectangleForTile(int x, int y) { Debug.Assert(0 <= x && x < mWidth && 0 <= y && y < mHeight); return new Rectangle(x * mTileW, y * mTileH, mTileW, mTileH); } /// /// Get a tile rectangle from a tile character. /// /// Tile character. /// Rectangle surrounding the tile. public Rectangle GetRectangleForTile(char tile) { return mTiles[tile]; } /// /// Get the flags associated with a tile character. /// /// Tile character. /// Tile flags. public TileFlags GetTileFlags(char tile) { return mFlags[tile]; } /// /// Associate a tile character with tile coordinates. This /// lets you access tiles by character. /// /// Tile character. /// Coordinates. public void SetTile(char tile, Point point, TileFlags flags) { mTiles.Add(tile, GetRectangleForTile(point)); mFlags.Add(tile, flags); } /// /// Draw a tile to the screen. /// /// The b2bomber. /// The tile. /// The screen rectangle to draw at. public void Draw(SpriteBatch spriteBatch, char tile, Rectangle screenRect) { spriteBatch.Draw(mTexture, screenRect, GetRectangleForTile(tile), Color.White); } #endregion #region Private Variables Texture2D mTexture; int mWidth; int mHeight; int mTileW; int mTileH; Dictionary mTiles = new Dictionary(); Dictionary mFlags = new Dictionary(); #endregion } }