2 using System.Collections.Generic;
5 using System.Diagnostics;
6 using Microsoft.Xna.Framework;
7 using Microsoft.Xna.Framework.Content;
8 using Microsoft.Xna.Framework.Graphics;
12 /// Small wrapper around a texture to provide easy access to
17 #region Public Properties
20 /// Get the texture for this tilemap.
22 public Texture2D Texture { get { return mTexture; } }
27 #region Public Methods
30 /// Construct a tilemap with a texture and dimensions in
33 /// <param name="texture">The texture.</param>
34 /// <param name="width">Number of tiles across.</param>
35 /// <param name="height">Number of tiles down.</param>
36 public Tilemap(Texture2D texture, int width, int height)
41 mTileW = mTexture.Width / mWidth;
42 mTileH = mTexture.Height / mHeight;
47 /// Get a tile rectangle from a tile coordinate.
49 /// <param name="point">Tile coordinates; [0,0] being the
50 /// top-left tile.</param>
51 /// <returns>Rectangle surrounding the tile.</returns>
52 public Rectangle GetRectangleForTile(Point point)
54 return GetRectangleForTile(point.X, point.Y);
58 /// Get a tile rectangle from a tile coordinate
60 /// <param name="x">X-coordinate.</param>
61 /// <param name="y">Y-coordinate.</param>
62 /// <returns>Rectangle surrounding the tile.</returns>
63 public Rectangle GetRectangleForTile(int x, int y)
65 Debug.Assert(0 <= x && x < mWidth && 0 <= y && y < mHeight);
66 return new Rectangle(x * mTileW, y * mTileH, mTileW, mTileH);
70 /// Get a tile rectangle from a tile character.
72 /// <param name="tile">Tile character.</param>
73 /// <returns>Rectangle surrounding the tile.</returns>
74 public Rectangle GetRectangleForTile(char tile)
81 /// Associate a tile character with tile coordinates. This
82 /// lets you access tile rectangles by character.
84 /// <param name="tile">Tile character.</param>
85 /// <param name="point">Coordinates.</param>
86 public void SetTile(char tile, Point point)
88 mTiles.Add(tile, GetRectangleForTile(point));
93 /// Draw a tile to the screen.
95 /// <param name="spriteBatch">The b2bomber.</param>
96 /// <param name="tile">The tile.</param>
97 /// <param name="screenRect">The screen rectangle to draw at.</param>
98 public void Draw(SpriteBatch spriteBatch, char tile, Rectangle screenRect)
100 spriteBatch.Draw(mTexture, screenRect, GetRectangleForTile(tile), Color.White);
106 #region Private Variables
113 Dictionary<char, Rectangle> mTiles = new Dictionary<char, Rectangle>();