]> Dogcows Code - chaz/carfire/blob - CarFire/CarFire/CarFire/Map.cs
Entity loading implemented.
[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 using System.Reflection;
11
12 namespace CarFire
13 {
14 /// <summary>
15 /// A map object represents the map or virtual world where players and other
16 /// game entities exist. The map consists of a grid where each grid space can
17 /// contain static scenery and/or game entities which can move and interact
18 /// with other game entities.
19 /// </summary>
20 public class Map
21 {
22 // DEBUG: Tilesets not implemented at all.
23 public static Texture2D DefaultTile;
24
25 #region Public Constants
26
27 public const float PixelsToUnitSquares = 8.0f;
28
29 #endregion
30
31
32 #region Public Types
33
34 /// <summary>
35 /// The type of a map helps determine how the map is intended to be used.
36 /// </summary>
37 public enum Type
38 {
39 None,
40 Campaign,
41 Battle
42 }
43
44 /// <summary>
45 /// The container class for map metadata.
46 /// </summary>
47 public class Metadata
48 {
49 public string Name;
50 public Type Type;
51 public string Author;
52 public HashSet<int> NumPlayers = new HashSet<int>();
53 public string Tileset;
54 public int GridWidth;
55 public int GridHeight;
56 }
57
58 /// <summary>
59 /// The container class for information about an entity defined in the map.
60 /// </summary>
61 public class RawEntity
62 {
63 public char Id;
64 public Point Position;
65 public Dictionary<string, string> Attributes = new Dictionary<string, string>();
66 }
67
68 #endregion
69
70
71 #region Public Attributes
72
73 /// <summary>
74 /// Get the name of the map.
75 /// </summary>
76 public string Name { get { return mData.Metadata.Name; } }
77
78 /// <summary>
79 /// Get the type of the map.
80 /// </summary>
81 //public Type Type { get { return mData.mMetadata.Type; } }
82
83 /// <summary>
84 /// Get the author of the map.
85 /// </summary>
86 public string Author { get { return mData.Metadata.Author; } }
87
88 /// <summary>
89 /// Get a set of integers containing each allowable number of players.
90 /// </summary>
91 public HashSet<int> NumPlayers { get { return mData.Metadata.NumPlayers; } }
92
93 /// <summary>
94 /// Get the width of the map, in grid units.
95 /// </summary>
96 public int Width { get { return mData.Metadata.GridWidth; } }
97
98 /// <summary>
99 /// Get the height of the map, in grid units.
100 /// </summary>
101 public int Height { get { return mData.Metadata.GridHeight; } }
102
103 // TODO: This should return whatever object we end up using for tilesets.
104 public string Tileset { get { return mData.Metadata.Tileset; } }
105
106 /// <summary>
107 /// Get a list of the raw entity containers loaded with the map.
108 /// </summary>
109 public List<RawEntity> RawEntities { get { return mData.Entities; } }
110
111
112 /// <summary>
113 /// Get and set the coordinate of the grid cell that should be in
114 /// the center of the screen when the map is drawn.
115 /// </summary>
116 public Vector2 CenterCell
117 {
118 get { return mView.CenterCell; }
119 set { mView.CenterCell = value; }
120 }
121
122 #endregion
123
124
125 #region Public Methods
126
127 /// <summary>
128 /// Construct a map with the provided map data.
129 /// </summary>
130 /// <param name="data">Map data.</param>
131 public Map(Metadata metadata, char[,] grid, List<RawEntity> entities)
132 {
133 mData = new Modal(metadata, grid, entities);
134 mView = new View(mData);
135 }
136
137
138 /// <summary>
139 /// Draw a representation of the map to the screen.
140 /// </summary>
141 /// <param name="spriteBatch">The jeewiz.</param>
142 public void Draw(SpriteBatch spriteBatch)
143 {
144 mView.Draw(spriteBatch);
145 }
146
147
148 /// <summary>
149 /// Get a point in screen-space from a coordinate in gridspace.
150 /// </summary>
151 /// <param name="x">X-coordinate.</param>
152 /// <param name="y">Y-coordinate.</param>
153 /// <returns>Transformed point.</returns>
154 public Point GetPointFromCoordinates(float x, float y)
155 {
156 return mView.GetPointFromCoordinates(x, y);
157 }
158
159 /// <summary>
160 /// Get a point in screen-space from a coordinate in gridspace.
161 /// </summary>
162 /// <param name="point">X,Y-coordinates.</param>
163 /// <returns>Transformed point.</returns>
164 public Point GetPointFromCoordinates(Vector2 point)
165 {
166 return mView.GetPointFromCoordinates(point.X, point.Y);
167 }
168
169 /// <summary>
170 /// Get a rectangle in screen-space centered around a coordinate in gridspace.
171 /// </summary>
172 /// <param name="x">X-coordinate.</param>
173 /// <param name="y">Y-coordinate.</param>
174 /// <returns>Transformed rectangle.</returns>
175 public Rectangle GetRectangleFromCoordinates(float x, float y)
176 {
177 return mView.GetRectangleFromCoordinates(x, y);
178 }
179
180 /// <summary>
181 /// Get a rectangle in screen-space centered around a coordinate in gridspace.
182 /// </summary>
183 /// <param name="point">X,Y-coordinates.</param>
184 /// <returns>Transformed rectangle.</returns>
185 public Rectangle GetRectangleFromCoordinates(Vector2 point)
186 {
187 return mView.GetRectangleFromCoordinates(point.X, point.Y);
188 }
189
190
191 /// <summary>
192 /// Determine whether or not a cell can be occupied by a game entity.
193 /// </summary>
194 /// <param name="x">X-coordinate.</param>
195 /// <param name="y">Y-coordinate.</param>
196 /// <returns>True if cell can be occupied, false otherwise.</returns>
197 public bool IsCellOpen(int x, int y)
198 {
199 return mData.IsCellOpen(x, y);
200 }
201
202 /// <summary>
203 /// Determine whether or not a cell can be occupied by a game entity.
204 /// </summary>
205 /// <param name="point">X,Y-coordinates.</param>
206 /// <returns>True if cell can be occupied, false otherwise.</returns>
207 public bool IsCellOpen(Point point)
208 {
209 return mData.IsCellOpen(point.X, point.Y);
210 }
211
212
213 /// <summary>
214 /// Get all the entities loaded from the map file. Exceptions could be
215 /// thrown if there are entities without associated classes.
216 /// </summary>
217 /// <returns>List of entity objects loaded.</returns>
218 public List<object> GetAllEntities()
219 {
220 return mData.GetAllEntities();
221 }
222
223 /// <summary>
224 /// Get the entities of a certain type loaded from the map file. Exceptions
225 /// could be thrown if there are entities without associated classes.
226 /// </summary>
227 /// <typeparam name="T">Type of the entity you want a list of.</typeparam>
228 /// <returns>List of entity objects loaded.</returns>
229 public List<T> GetEntities<T>()
230 {
231 return mData.GetEntities<T>();
232 }
233
234 #endregion
235
236
237 #region Private Types
238
239 class Modal
240 {
241 Metadata mMetadata;
242 char[,] mGrid;
243 List<RawEntity> mEntities;
244
245 public Modal(Metadata metadata, char[,] grid, List<RawEntity> entities)
246 {
247 Debug.Assert(metadata != null);
248 Debug.Assert(grid != null);
249 Debug.Assert(entities != null);
250 Debug.Assert(metadata.GridWidth * metadata.GridHeight == grid.Length);
251
252 mMetadata = metadata;
253 mGrid = grid;
254 mEntities = entities;
255
256 #if DEBUG
257 Console.WriteLine("Loaded map {0} of type {1} written by {2}.",
258 metadata.Name,
259 metadata.Type,
260 metadata.Author);
261 #endif
262 }
263
264
265 public Metadata Metadata { get { return mMetadata; } }
266 public List<RawEntity> Entities { get { return mEntities; } }
267
268
269 public bool IsCellOpen(int x, int y)
270 {
271 // TODO: Still need to define characters for types of scenery.
272 return mGrid[x, y] == ' ';
273 }
274
275
276 public List<object> GetAllEntities()
277 {
278 List<object> list = new List<object>();
279
280 foreach (RawEntity raw in mEntities)
281 {
282 if (raw.Attributes.ContainsKey("type"))
283 {
284 string typename = raw.Attributes["type"];
285
286 object[] args = new object[3];
287 args[0] = raw.Id;
288 args[1] = raw.Position;
289 args[2] = raw.Attributes;
290
291 object entity = Activator.CreateInstance(System.Type.GetType("CarFire." + typename), args);
292 if (entity != null) list.Add(entity);
293 else Console.WriteLine("Entity of type " + typename + " not loaded because an entity class can't be found.");
294 }
295 else
296 {
297 Console.WriteLine("Ignoring entity with identifier " + raw.Id + " since it has no type key.");
298 }
299 }
300
301 return list;
302 }
303
304 public List<T> GetEntities<T>()
305 {
306 System.Type type = typeof(T);
307 List<T> list = new List<T>();
308
309 string typename = typeof(T).Name;
310 foreach (RawEntity raw in mEntities)
311 {
312 if (raw.Attributes.ContainsKey("type") && typename == raw.Attributes["type"])
313 {
314 object[] args = new object[3];
315 args[0] = raw.Id;
316 args[1] = raw.Position;
317 args[2] = raw.Attributes;
318
319 T entity = (T)Activator.CreateInstance(type, args);
320 if (entity != null) list.Add(entity);
321 else Console.WriteLine("Entity of type " + typename + " not loaded because an entity class can't be found.");
322 }
323 }
324
325 return list;
326 }
327 }
328
329 class View
330 {
331 Modal mData;
332
333 public Vector2 CenterCell;
334 Viewport mViewport;
335
336
337 public View(Modal data)
338 {
339 Debug.Assert(data != null);
340 mData = data;
341 }
342
343 public void Draw(SpriteBatch spriteBatch)
344 {
345 mViewport = spriteBatch.GraphicsDevice.Viewport;
346
347 // TODO: There is no culling yet, but it runs so fast that it probably won't ever need it.
348 for (int y = 0; y < mData.Metadata.GridHeight; y++)
349 {
350 for (int x = 0; x < mData.Metadata.GridWidth; x++)
351 {
352 if (mData.IsCellOpen(x, y))
353 {
354 spriteBatch.Draw(Map.DefaultTile, GetRectangleFromCoordinates(x, y), Color.White);
355 }
356 else
357 {
358 spriteBatch.Draw(Map.DefaultTile, GetRectangleFromCoordinates(x, y), Color.DarkBlue);
359 }
360 }
361 }
362 }
363
364 /// <summary>
365 /// Get a matrix to transform a point from grid-space to screen coordinates. This
366 /// method uses the viewport to bound the edges of the map such that the camera
367 /// will not show anything outside of the grid.
368 /// </summary>
369 /// <param name="center">The point to put in the center.</param>
370 /// <returns>The transformation matrix.</returns>
371 Matrix GetTransformation(Vector2 center)
372 {
373 float halfRatio = PixelsToUnitSquares * 0.5f;
374 Matrix transform = Matrix.CreateTranslation(-center.X, -center.Y, 0.0f);
375 transform *= Matrix.CreateScale(PixelsToUnitSquares);
376 transform *= Matrix.CreateTranslation(mViewport.Width * 0.5f - halfRatio,
377 mViewport.Height * 0.5f - halfRatio, 0.0f);
378
379 Vector2 topLeft = Vector2.Transform(new Vector2(0.0f, 0.0f), transform);
380 topLeft.X = Math.Max(mViewport.X, topLeft.X);
381 topLeft.Y = Math.Max(mViewport.Y, topLeft.Y);
382 transform *= Matrix.CreateTranslation(-topLeft.X, -topLeft.Y, 0.0f);
383
384 Vector2 bottomRight = Vector2.Transform(new Vector2((float)mData.Metadata.GridWidth,
385 (float)mData.Metadata.GridHeight), transform);
386 float right = mViewport.X + mViewport.Width;
387 float bottom = mViewport.Y + mViewport.Height;
388 bottomRight.X = Math.Min(right, bottomRight.X) - right;
389 bottomRight.Y = Math.Min(bottom, bottomRight.Y) - bottom;
390 transform *= Matrix.CreateTranslation(-bottomRight.X, -bottomRight.Y, 0.0f);
391
392 return transform;
393 }
394
395
396 public Point GetPointFromCoordinates(float x, float y)
397 {
398 Matrix transform = GetTransformation(CenterCell);
399 Vector2 point = Vector2.Transform(new Vector2(x, y), transform);
400
401 return new Point((int)point.X, (int)point.Y);
402 }
403
404 public Rectangle GetRectangleFromCoordinates(float x, float y)
405 {
406 Matrix transform = GetTransformation(CenterCell);
407 Vector2 point = Vector2.Transform(new Vector2(x, y), transform);
408
409 return new Rectangle((int)point.X, (int)point.Y, (int)PixelsToUnitSquares, (int)PixelsToUnitSquares);
410 }
411 }
412
413 #endregion
414
415
416 #region Private Variables
417
418 Modal mData;
419 View mView;
420
421 #endregion
422 }
423 }
This page took 0.048825 seconds and 5 git commands to generate.