X-Git-Url: https://git.brokenzipper.com/gitweb?a=blobdiff_plain;f=CarFire%2FCarFire%2FCarFire%2FTilemap.cs;fp=CarFire%2FCarFire%2FCarFire%2FTilemap.cs;h=d0441083c4810a181e8072414890a38f179c82e2;hb=fc34f843ea42a3496a7ff5dd04853695ba628e8b;hp=0000000000000000000000000000000000000000;hpb=b5eebc2087c00bb67b3a3b9ddcec4743aa7a8cdb;p=chaz%2Fcarfire diff --git a/CarFire/CarFire/CarFire/Tilemap.cs b/CarFire/CarFire/CarFire/Tilemap.cs new file mode 100644 index 0000000..d044108 --- /dev/null +++ b/CarFire/CarFire/CarFire/Tilemap.cs @@ -0,0 +1,117 @@ +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 + } +}