]> Dogcows Code - chaz/carfire/blob - CarFire/CarFire/CarFire/Tilemap.cs
Implemented map tiles; started new Key entity; added missing variables to Melee.cs...
[chaz/carfire] / CarFire / CarFire / CarFire / Tilemap.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Diagnostics;
6 using Microsoft.Xna.Framework;
7 using Microsoft.Xna.Framework.Content;
8 using Microsoft.Xna.Framework.Graphics;
9 namespace CarFire
10 {
11 /// <summary>
12 /// Small wrapper around a texture to provide easy access to
13 /// tile rectangles.
14 /// </summary>
15 public class Tilemap
16 {
17 #region Public Properties
18
19 /// <summary>
20 /// Get the texture for this tilemap.
21 /// </summary>
22 public Texture2D Texture { get { return mTexture; } }
23
24 #endregion
25
26
27 #region Public Methods
28
29 /// <summary>
30 /// Construct a tilemap with a texture and dimensions in
31 /// tiles.
32 /// </summary>
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)
37 {
38 mTexture = texture;
39 mWidth = width;
40 mHeight = height;
41 mTileW = mTexture.Width / mWidth;
42 mTileH = mTexture.Height / mHeight;
43 }
44
45
46 /// <summary>
47 /// Get a tile rectangle from a tile coordinate.
48 /// </summary>
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)
53 {
54 return GetRectangleForTile(point.X, point.Y);
55 }
56
57 /// <summary>
58 /// Get a tile rectangle from a tile coordinate
59 /// </summary>
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)
64 {
65 Debug.Assert(0 <= x && x < mWidth && 0 <= y && y < mHeight);
66 return new Rectangle(x * mTileW, y * mTileH, mTileW, mTileH);
67 }
68
69 /// <summary>
70 /// Get a tile rectangle from a tile character.
71 /// </summary>
72 /// <param name="tile">Tile character.</param>
73 /// <returns>Rectangle surrounding the tile.</returns>
74 public Rectangle GetRectangleForTile(char tile)
75 {
76 return mTiles[tile];
77 }
78
79
80 /// <summary>
81 /// Associate a tile character with tile coordinates. This
82 /// lets you access tile rectangles by character.
83 /// </summary>
84 /// <param name="tile">Tile character.</param>
85 /// <param name="point">Coordinates.</param>
86 public void SetTile(char tile, Point point)
87 {
88 mTiles.Add(tile, GetRectangleForTile(point));
89 }
90
91
92 /// <summary>
93 /// Draw a tile to the screen.
94 /// </summary>
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)
99 {
100 spriteBatch.Draw(mTexture, screenRect, GetRectangleForTile(tile), Color.White);
101 }
102
103 #endregion
104
105
106 #region Private Variables
107
108 Texture2D mTexture;
109 int mWidth;
110 int mHeight;
111 int mTileW;
112 int mTileH;
113 Dictionary<char, Rectangle> mTiles = new Dictionary<char, Rectangle>();
114
115 #endregion
116 }
117 }
This page took 0.032924 seconds and 4 git commands to generate.