--- /dev/null
+using System;\r
+using System.Collections.Generic;\r
+using System.Linq;\r
+using System.Text;\r
+using System.Diagnostics;\r
+using Microsoft.Xna.Framework;\r
+using Microsoft.Xna.Framework.Content;\r
+using Microsoft.Xna.Framework.Graphics;\r
+namespace CarFire\r
+{\r
+ /// <summary>\r
+ /// Small wrapper around a texture to provide easy access to\r
+ /// tile rectangles.\r
+ /// </summary>\r
+ public class Tilemap\r
+ {\r
+ #region Public Properties\r
+\r
+ /// <summary>\r
+ /// Get the texture for this tilemap.\r
+ /// </summary>\r
+ public Texture2D Texture { get { return mTexture; } }\r
+\r
+ #endregion\r
+\r
+\r
+ #region Public Methods\r
+\r
+ /// <summary>\r
+ /// Construct a tilemap with a texture and dimensions in\r
+ /// tiles.\r
+ /// </summary>\r
+ /// <param name="texture">The texture.</param>\r
+ /// <param name="width">Number of tiles across.</param>\r
+ /// <param name="height">Number of tiles down.</param>\r
+ public Tilemap(Texture2D texture, int width, int height)\r
+ {\r
+ mTexture = texture;\r
+ mWidth = width;\r
+ mHeight = height;\r
+ mTileW = mTexture.Width / mWidth;\r
+ mTileH = mTexture.Height / mHeight;\r
+ }\r
+\r
+\r
+ /// <summary>\r
+ /// Get a tile rectangle from a tile coordinate.\r
+ /// </summary>\r
+ /// <param name="point">Tile coordinates; [0,0] being the\r
+ /// top-left tile.</param>\r
+ /// <returns>Rectangle surrounding the tile.</returns>\r
+ public Rectangle GetRectangleForTile(Point point)\r
+ {\r
+ return GetRectangleForTile(point.X, point.Y);\r
+ }\r
+\r
+ /// <summary>\r
+ /// Get a tile rectangle from a tile coordinate\r
+ /// </summary>\r
+ /// <param name="x">X-coordinate.</param>\r
+ /// <param name="y">Y-coordinate.</param>\r
+ /// <returns>Rectangle surrounding the tile.</returns>\r
+ public Rectangle GetRectangleForTile(int x, int y)\r
+ {\r
+ Debug.Assert(0 <= x && x < mWidth && 0 <= y && y < mHeight);\r
+ return new Rectangle(x * mTileW, y * mTileH, mTileW, mTileH);\r
+ }\r
+\r
+ /// <summary>\r
+ /// Get a tile rectangle from a tile character.\r
+ /// </summary>\r
+ /// <param name="tile">Tile character.</param>\r
+ /// <returns>Rectangle surrounding the tile.</returns>\r
+ public Rectangle GetRectangleForTile(char tile)\r
+ {\r
+ return mTiles[tile];\r
+ }\r
+\r
+\r
+ /// <summary>\r
+ /// Associate a tile character with tile coordinates. This\r
+ /// lets you access tile rectangles by character.\r
+ /// </summary>\r
+ /// <param name="tile">Tile character.</param>\r
+ /// <param name="point">Coordinates.</param>\r
+ public void SetTile(char tile, Point point)\r
+ {\r
+ mTiles.Add(tile, GetRectangleForTile(point));\r
+ }\r
+\r
+\r
+ /// <summary>\r
+ /// Draw a tile to the screen.\r
+ /// </summary>\r
+ /// <param name="spriteBatch">The b2bomber.</param>\r
+ /// <param name="tile">The tile.</param>\r
+ /// <param name="screenRect">The screen rectangle to draw at.</param>\r
+ public void Draw(SpriteBatch spriteBatch, char tile, Rectangle screenRect)\r
+ {\r
+ spriteBatch.Draw(mTexture, screenRect, GetRectangleForTile(tile), Color.White);\r
+ }\r
+\r
+ #endregion\r
+\r
+\r
+ #region Private Variables\r
+\r
+ Texture2D mTexture;\r
+ int mWidth;\r
+ int mHeight;\r
+ int mTileW;\r
+ int mTileH;\r
+ Dictionary<char, Rectangle> mTiles = new Dictionary<char, Rectangle>();\r
+\r
+ #endregion\r
+ }\r
+}\r