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 /// Flag options to define attributes for individual tiles.
26 /// Small wrapper around a texture to provide easy access to
31 #region Public Properties
34 /// Get the texture for this tilemap.
36 public Texture2D Texture { get { return mTexture; } }
41 #region Public Methods
44 /// Construct a tilemap with a texture and dimensions in
47 /// <param name="texture">The texture.</param>
48 /// <param name="width">Number of tiles across.</param>
49 /// <param name="height">Number of tiles down.</param>
50 public Tilemap(Texture2D texture, int width, int height)
55 mTileW = mTexture.Width / mWidth;
56 mTileH = mTexture.Height / mHeight;
61 /// Get a tile rectangle from a tile coordinate.
63 /// <param name="point">Tile coordinates; [0,0] being the
64 /// top-left tile.</param>
65 /// <returns>Rectangle surrounding the tile.</returns>
66 public Rectangle GetRectangleForTile(Point point)
68 return GetRectangleForTile(point.X, point.Y);
72 /// Get a tile rectangle from a tile coordinate
74 /// <param name="x">X-coordinate.</param>
75 /// <param name="y">Y-coordinate.</param>
76 /// <returns>Rectangle surrounding the tile.</returns>
77 public Rectangle GetRectangleForTile(int x, int y)
79 Debug.Assert(0 <= x && x < mWidth && 0 <= y && y < mHeight);
80 return new Rectangle(x * mTileW, y * mTileH, mTileW, mTileH);
84 /// Get a tile rectangle from a tile character.
86 /// <param name="tile">Tile character.</param>
87 /// <returns>Rectangle surrounding the tile.</returns>
88 public Rectangle GetRectangleForTile(char tile)
95 /// Get the flags associated with a tile character.
97 /// <param name="tile">Tile character.</param>
98 /// <returns>Tile flags.</returns>
99 public TileFlags GetTileFlags(char tile)
106 /// Associate a tile character with tile coordinates. This
107 /// lets you access tiles by character.
109 /// <param name="tile">Tile character.</param>
110 /// <param name="point">Coordinates.</param>
111 public void SetTile(char tile, Point point, TileFlags flags)
113 mTiles.Add(tile, GetRectangleForTile(point));
114 mFlags.Add(tile, flags);
119 /// Draw a tile to the screen.
121 /// <param name="spriteBatch">The b2bomber.</param>
122 /// <param name="tile">The tile.</param>
123 /// <param name="screenRect">The screen rectangle to draw at.</param>
124 public void Draw(SpriteBatch spriteBatch, char tile, Rectangle screenRect)
126 spriteBatch.Draw(mTexture, screenRect, GetRectangleForTile(tile), Color.White);
132 #region Private Variables
139 Dictionary<char, Rectangle> mTiles = new Dictionary<char, Rectangle>();
140 Dictionary<char, TileFlags> mFlags = new Dictionary<char, TileFlags>();