]> Dogcows Code - chaz/carfire/blob - CarFire/CarFire/CarFire/Map.cs
forgot to document Map.CenterCell
[chaz/carfire] / CarFire / CarFire / CarFire / Map.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.IO;
6 using System.Runtime.Serialization;
7 using System.Diagnostics;
8 using Microsoft.Xna.Framework;
9 using Microsoft.Xna.Framework.Graphics;
10
11 namespace CarFire
12 {
13 /// <summary>
14 /// A map object represents the map or virtual world where players and other
15 /// game entities exist. The map consists of a grid where each grid space can
16 /// contain static scenery and/or game entities which can move and interact
17 /// with other game entities.
18 /// </summary>
19 public class Map
20 {
21 // DEBUG: Tilesets not implemented at all.
22 public static Texture2D DefaultTile;
23
24 #region Public Constants
25
26 public const float PixelsToUnitSquares = 64.0f;
27
28 #endregion
29
30
31 #region Public Types
32
33 /// <summary>
34 /// The type of a map helps determine how the map is intended to be used.
35 /// </summary>
36 public enum Type
37 {
38 None,
39 Campaign,
40 Battle
41 }
42
43 /// <summary>
44 /// The container class for map metadata.
45 /// </summary>
46 public class Metadata
47 {
48 public string Name;
49 public Type Type;
50 public string Author;
51 public HashSet<int> NumPlayers = new HashSet<int>();
52 public string Tileset;
53 public int GridWidth;
54 public int GridHeight;
55 }
56
57 #endregion
58
59
60 #region Public Attributes
61
62 /// <summary>
63 /// Get the name of the map.
64 /// </summary>
65 public string Name { get { return mData.Metadata.Name; } }
66
67 /// <summary>
68 /// Get the type of the map.
69 /// </summary>
70 //public Type Type { get { return mData.mMetadata.Type; } }
71
72 /// <summary>
73 /// Get the author of the map.
74 /// </summary>
75 public string Author { get { return mData.Metadata.Author; } }
76
77 /// <summary>
78 /// Get a set of integers containing each allowable number of players.
79 /// </summary>
80 public HashSet<int> NumPlayers { get { return mData.Metadata.NumPlayers; } }
81
82 /// <summary>
83 /// Get the width of the map, in grid units.
84 /// </summary>
85 public int Width { get { return mData.Metadata.GridWidth; } }
86
87 /// <summary>
88 /// Get the height of the map, in grid units.
89 /// </summary>
90 public int Height { get { return mData.Metadata.GridHeight; } }
91
92 // TODO: This should return whatever object we end up using for tilesets.
93 public string Tileset { get { return mData.Metadata.Tileset; } }
94
95
96 /// <summary>
97 /// Get and set the coordinate of the grid cell that should be in
98 /// the center of the screen when the map is drawn.
99 /// </summary>
100 public Vector2 CenterCell
101 {
102 get { return mView.CenterCell; }
103 set { mView.CenterCell = value; }
104 }
105
106 #endregion
107
108
109 #region Public Methods
110
111 /// <summary>
112 /// Construct a map with the provided map data.
113 /// </summary>
114 /// <param name="data">Map data.</param>
115 public Map(Metadata metadata, char[,] grid, Dictionary<char, Dictionary<string, string>> entities)
116 {
117 mData = new Modal(metadata, grid, entities);
118 mView = new View(mData);
119 }
120
121
122 /// <summary>
123 /// Draw a representation of the map to the screen.
124 /// </summary>
125 /// <param name="spriteBatch">The jeewiz.</param>
126 public void Draw(SpriteBatch spriteBatch)
127 {
128 mView.Draw(spriteBatch);
129 }
130
131
132 /// <summary>
133 /// Get a point in screen-space from a coordinate in gridspace.
134 /// </summary>
135 /// <param name="x">X-coordinate.</param>
136 /// <param name="y">Y-coordinate.</param>
137 /// <returns>Transformed point.</returns>
138 public Point GetPointFromCoordinates(float x, float y)
139 {
140 return mView.GetPointFromCoordinates(x, y);
141 }
142
143 /// <summary>
144 /// Get a point in screen-space from a coordinate in gridspace.
145 /// </summary>
146 /// <param name="point">X,Y-coordinates.</param>
147 /// <returns>Transformed point.</returns>
148 public Point GetPointFromCoordinates(Vector2 point)
149 {
150 return mView.GetPointFromCoordinates(point.X, point.Y);
151 }
152
153 /// <summary>
154 /// Get a rectangle in screen-space centered around a coordinate in gridspace.
155 /// </summary>
156 /// <param name="x">X-coordinate.</param>
157 /// <param name="y">Y-coordinate.</param>
158 /// <returns>Transformed rectangle.</returns>
159 public Rectangle GetRectangleFromCoordinates(float x, float y)
160 {
161 return mView.GetRectangleFromCoordinates(x, y);
162 }
163
164 /// <summary>
165 /// Get a rectangle in screen-space centered around a coordinate in gridspace.
166 /// </summary>
167 /// <param name="point">X,Y-coordinates.</param>
168 /// <returns>Transformed rectangle.</returns>
169 public Rectangle GetRectangleFromCoordinates(Vector2 point)
170 {
171 return mView.GetRectangleFromCoordinates(point.X, point.Y);
172 }
173
174
175 /// <summary>
176 /// Determine whether or not a cell can be occupied by a game entity.
177 /// </summary>
178 /// <param name="x">X-coordinate.</param>
179 /// <param name="y">Y-coordinate.</param>
180 /// <returns>True if cell can be occupied, false otherwise.</returns>
181 public bool IsCellOpen(int x, int y)
182 {
183 return mData.IsCellOpen(x, y);
184 }
185
186 /// <summary>
187 /// Determine whether or not a cell can be occupied by a game entity.
188 /// </summary>
189 /// <param name="x">X,Y-coordinates.</param>
190 /// <returns>True if cell can be occupied, false otherwise.</returns>
191 public bool IsCellOpen(Point point)
192 {
193 return mData.IsCellOpen(point.X, point.Y);
194 }
195
196
197 /// <summary>
198 /// Get the entities loaded from the map file.
199 /// </summary>
200 /// <returns>Dictionary of entities. The keys are the entity
201 /// identifiers and the value is a dictionary of key-value pairs
202 /// associated with that entity.</returns>
203 public Dictionary<char, Dictionary<string, string>> GetEntities()
204 {
205 return mData.Entities;
206 }
207
208 #endregion
209
210
211 #region Private Types
212
213 class Modal
214 {
215 Metadata mMetadata;
216 char[,] mGrid;
217 Dictionary<char, Dictionary<string, string>> mEntities;
218
219 public Modal(Metadata metadata, char[,] grid, Dictionary<char, Dictionary<string, string>> entities)
220 {
221 Debug.Assert(metadata != null);
222 Debug.Assert(grid != null);
223 Debug.Assert(entities != null);
224 Debug.Assert(metadata.GridWidth * metadata.GridHeight == grid.Length);
225
226 mMetadata = metadata;
227 mGrid = grid;
228 mEntities = entities;
229
230 #if DEBUG
231 Console.WriteLine("Loaded map {0} of type {1} written by {2}.",
232 metadata.Name,
233 metadata.Type,
234 metadata.Author);
235 #endif
236 }
237
238
239 public Metadata Metadata { get { return mMetadata; } }
240 public Dictionary<char, Dictionary<string, string>> Entities { get { return mEntities; } }
241
242
243 public bool IsCellOpen(int x, int y)
244 {
245 // TODO: Still need to define characters for types of scenery.
246 return mGrid[x, y] == ' ';
247 }
248 }
249
250 class View
251 {
252 Modal mData;
253
254 public Vector2 CenterCell;
255 Viewport mViewport;
256
257
258 public View(Modal data)
259 {
260 Debug.Assert(data != null);
261 mData = data;
262 }
263
264 public void Draw(SpriteBatch spriteBatch)
265 {
266 mViewport = spriteBatch.GraphicsDevice.Viewport;
267
268 // TODO: There is no culling yet, but it runs so fast that it probably won't ever need it.
269 for (int y = 0; y < mData.Metadata.GridHeight; y++)
270 {
271 for (int x = 0; x < mData.Metadata.GridWidth; x++)
272 {
273 if (mData.IsCellOpen(x, y))
274 {
275 spriteBatch.Draw(Map.DefaultTile, GetRectangleFromCoordinates(x, y), Color.White);
276 }
277 else
278 {
279 spriteBatch.Draw(Map.DefaultTile, GetRectangleFromCoordinates(x, y), Color.DarkBlue);
280 }
281 }
282 }
283 }
284
285 /// <summary>
286 /// Get a matrix to transform a point from grid-space to screen coordinates. This
287 /// method uses the viewport to bound the edges of the map such that the camera
288 /// will not show anything outside of the grid.
289 /// </summary>
290 /// <param name="center">The point to put in the center.</param>
291 /// <returns>The transformation matrix.</returns>
292 Matrix GetTransformation(Vector2 center)
293 {
294 float halfRatio = PixelsToUnitSquares * 0.5f;
295 Matrix transform = Matrix.CreateTranslation(-center.X, -center.Y, 0.0f);
296 transform *= Matrix.CreateScale(PixelsToUnitSquares);
297 transform *= Matrix.CreateTranslation(mViewport.Width * 0.5f - halfRatio,
298 mViewport.Height * 0.5f - halfRatio, 0.0f);
299
300 Vector2 topLeft = Vector2.Transform(new Vector2(0.0f, 0.0f), transform);
301 topLeft.X = Math.Max(mViewport.X, topLeft.X);
302 topLeft.Y = Math.Max(mViewport.Y, topLeft.Y);
303 transform *= Matrix.CreateTranslation(-topLeft.X, -topLeft.Y, 0.0f);
304
305 Vector2 bottomRight = Vector2.Transform(new Vector2((float)mData.Metadata.GridWidth,
306 (float)mData.Metadata.GridHeight), transform);
307 float right = mViewport.X + mViewport.Width;
308 float bottom = mViewport.Y + mViewport.Height;
309 bottomRight.X = Math.Min(right, bottomRight.X) - right;
310 bottomRight.Y = Math.Min(bottom, bottomRight.Y) - bottom;
311 transform *= Matrix.CreateTranslation(-bottomRight.X, -bottomRight.Y, 0.0f);
312
313 return transform;
314 }
315
316
317 public Point GetPointFromCoordinates(float x, float y)
318 {
319 Matrix transform = GetTransformation(CenterCell);
320 Vector2 point = Vector2.Transform(new Vector2(x, y), transform);
321
322 return new Point((int)point.X, (int)point.Y);
323 }
324
325 public Rectangle GetRectangleFromCoordinates(float x, float y)
326 {
327 Matrix transform = GetTransformation(CenterCell);
328 Vector2 point = Vector2.Transform(new Vector2(x, y), transform);
329
330 return new Rectangle((int)point.X, (int)point.Y, (int)PixelsToUnitSquares, (int)PixelsToUnitSquares);
331 }
332 }
333
334 #endregion
335
336
337 #region Private Variables
338
339 Modal mData;
340 View mView;
341
342 #endregion
343 }
344 }
This page took 0.053369 seconds and 5 git commands to generate.