]> Dogcows Code - chaz/carfire/blob - CarFire/CarFire/CarFire/Tilemap.cs
git-svn-id: https://bd85.net/svn/cs3505_group@168 92bb83a3-7c8f-8a45-bc97-515c4e399668
[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 /// Flag options to define attributes for individual tiles.
13 /// </summary>
14 [Flags]
15 public enum TileFlags
16 {
17 Open = 0x01,
18 Closed = 0x02,
19 Floor = 0x04,
20 Wall = 0x08,
21 Default = 0x05
22 }
23
24
25 /// <summary>
26 /// Small wrapper around a texture to provide easy access to
27 /// tile rectangles.
28 /// </summary>
29 public class Tilemap
30 {
31 #region Public Properties
32
33 /// <summary>
34 /// Get the texture for this tilemap.
35 /// </summary>
36 public Texture2D Texture { get { return mTexture; } }
37
38 #endregion
39
40
41 #region Public Methods
42
43 /// <summary>
44 /// Construct a tilemap with a texture and dimensions in
45 /// tiles.
46 /// </summary>
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)
51 {
52 mTexture = texture;
53 mWidth = width;
54 mHeight = height;
55 mTileW = mTexture.Width / mWidth;
56 mTileH = mTexture.Height / mHeight;
57 }
58
59
60 /// <summary>
61 /// Get a tile rectangle from a tile coordinate.
62 /// </summary>
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)
67 {
68 return GetRectangleForTile(point.X, point.Y);
69 }
70
71 /// <summary>
72 /// Get a tile rectangle from a tile coordinate
73 /// </summary>
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)
78 {
79 Debug.Assert(0 <= x && x < mWidth && 0 <= y && y < mHeight);
80 return new Rectangle(x * mTileW, y * mTileH, mTileW, mTileH);
81 }
82
83 /// <summary>
84 /// Get a tile rectangle from a tile character.
85 /// </summary>
86 /// <param name="tile">Tile character.</param>
87 /// <returns>Rectangle surrounding the tile.</returns>
88 public Rectangle GetRectangleForTile(char tile)
89 {
90 return mTiles[tile];
91 }
92
93
94 /// <summary>
95 /// Get the flags associated with a tile character.
96 /// </summary>
97 /// <param name="tile">Tile character.</param>
98 /// <returns>Tile flags.</returns>
99 public TileFlags GetTileFlags(char tile)
100 {
101 return mFlags[tile];
102 }
103
104
105 /// <summary>
106 /// Associate a tile character with tile coordinates. This
107 /// lets you access tiles by character.
108 /// </summary>
109 /// <param name="tile">Tile character.</param>
110 /// <param name="point">Coordinates.</param>
111 public void SetTile(char tile, Point point, TileFlags flags)
112 {
113 mTiles.Add(tile, GetRectangleForTile(point));
114 mFlags.Add(tile, flags);
115 }
116
117
118 /// <summary>
119 /// Draw a tile to the screen.
120 /// </summary>
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)
125 {
126 spriteBatch.Draw(mTexture, screenRect, GetRectangleForTile(tile), Color.White);
127 }
128
129 #endregion
130
131
132 #region Private Variables
133
134 Texture2D mTexture;
135 int mWidth;
136 int mHeight;
137 int mTileW;
138 int mTileH;
139 Dictionary<char, Rectangle> mTiles = new Dictionary<char, Rectangle>();
140 Dictionary<char, TileFlags> mFlags = new Dictionary<char, TileFlags>();
141
142 #endregion
143 }
144 }
This page took 0.037389 seconds and 4 git commands to generate.