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
{
///
/// 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];
}
///
/// Associate a tile character with tile coordinates. This
/// lets you access tile rectangles by character.
///
/// Tile character.
/// Coordinates.
public void SetTile(char tile, Point point)
{
mTiles.Add(tile, GetRectangleForTile(point));
}
///
/// 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();
#endregion
}
}