2 using System.Collections.Generic;
6 using System.Runtime.Serialization;
7 using System.Diagnostics;
8 using Microsoft.Xna.Framework;
9 using Microsoft.Xna.Framework.Graphics;
10 using System.Reflection;
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.
22 // DEBUG: Tilesets not implemented at all.
23 public static Texture2D DefaultTile;
25 #region Public Exceptions
28 /// This exception is thrown during the loading of a map if any
29 /// part of the map file is inconsistent with the expected format
32 public class RuntimeException : System.ApplicationException
34 public RuntimeException() { }
36 public RuntimeException(string message) :
39 public RuntimeException(string message, System.Exception inner) :
40 base(message, inner) { }
42 protected RuntimeException(SerializationInfo info, StreamingContext context) :
43 base(info, context) { }
49 #region Public Constants
51 public const float PixelsToUnitSquares = 8.0f;
59 /// The type of a map helps determine how the map is intended to be used.
69 /// The container class for map metadata.
76 public HashSet<int> NumPlayers = new HashSet<int>();
77 public string Tileset;
79 public int GridHeight;
83 /// The container class for information about an entity defined in the map.
85 public class RawEntity
88 public Point Position;
89 public Dictionary<string, string> Attributes = new Dictionary<string, string>();
95 #region Public Attributes
98 /// Get the name of the map.
100 public string Name { get { return mData.Metadata.Name; } }
103 /// Get the type of the map.
105 //public Type Type { get { return mData.mMetadata.Type; } }
108 /// Get the author of the map.
110 public string Author { get { return mData.Metadata.Author; } }
113 /// Get a set of integers containing each allowable number of players.
115 public HashSet<int> NumPlayers { get { return mData.Metadata.NumPlayers; } }
118 /// Get the width of the map, in grid units.
120 public int Width { get { return mData.Metadata.GridWidth; } }
123 /// Get the height of the map, in grid units.
125 public int Height { get { return mData.Metadata.GridHeight; } }
127 // TODO: This should return whatever object we end up using for tilesets.
128 public string Tileset { get { return mData.Metadata.Tileset; } }
131 /// Get a list of the raw entity containers loaded with the map.
133 public List<RawEntity> RawEntities { get { return mData.Entities; } }
137 /// Get and set the coordinate of the grid cell that should be in
138 /// the center of the screen when the map is drawn.
140 public Vector2 CenterCell
142 get { return mView.CenterCell; }
143 set { mView.CenterCell = value; }
149 #region Public Methods
152 /// Construct a map with the provided map data.
154 /// <param name="data">Map data.</param>
155 public Map(Metadata metadata, char[,] grid, List<RawEntity> entities)
157 mData = new Modal(metadata, grid, entities);
158 mView = new View(mData);
163 /// Draw a representation of the map to the screen.
165 /// <param name="spriteBatch">The jeewiz.</param>
166 public void Draw(SpriteBatch spriteBatch)
168 mView.Draw(spriteBatch);
173 /// Get a point in screen-space from a coordinate in gridspace.
175 /// <param name="x">X-coordinate.</param>
176 /// <param name="y">Y-coordinate.</param>
177 /// <returns>Transformed point.</returns>
178 public Point GetPointFromCoordinates(float x, float y)
180 return mView.GetPointFromCoordinates(x, y);
184 /// Get a point in screen-space from a coordinate in gridspace.
186 /// <param name="point">X,Y-coordinates.</param>
187 /// <returns>Transformed point.</returns>
188 public Point GetPointFromCoordinates(Vector2 point)
190 return mView.GetPointFromCoordinates(point.X, point.Y);
194 /// Get a rectangle in screen-space centered around a coordinate in gridspace.
196 /// <param name="x">X-coordinate.</param>
197 /// <param name="y">Y-coordinate.</param>
198 /// <returns>Transformed rectangle.</returns>
199 public Rectangle GetRectangleFromCoordinates(float x, float y)
201 return mView.GetRectangleFromCoordinates(x, y);
205 /// Get a rectangle in screen-space centered around a coordinate in gridspace.
207 /// <param name="point">X,Y-coordinates.</param>
208 /// <returns>Transformed rectangle.</returns>
209 public Rectangle GetRectangleFromCoordinates(Vector2 point)
211 return mView.GetRectangleFromCoordinates(point.X, point.Y);
216 /// Determine whether or not a cell can be occupied by a game entity.
218 /// <param name="x">X-coordinate.</param>
219 /// <param name="y">Y-coordinate.</param>
220 /// <returns>True if cell can be occupied, false otherwise.</returns>
221 public bool IsCellOpen(int x, int y)
223 return mData.IsCellOpen(x, y);
227 /// Determine whether or not a cell can be occupied by a game entity.
229 /// <param name="point">X,Y-coordinates.</param>
230 /// <returns>True if cell can be occupied, false otherwise.</returns>
231 public bool IsCellOpen(Point point)
233 return mData.IsCellOpen(point.X, point.Y);
238 /// Get all the entities loaded from the map file. Exceptions could be
239 /// thrown if there are entities without associated classes.
241 /// <returns>List of entity objects loaded.</returns>
242 public List<object> GetAllEntities()
244 return mData.GetAllEntities();
248 /// Get the entities of a certain type loaded from the map file. Exceptions
249 /// could be thrown if there are entities without associated classes.
251 /// <typeparam name="T">Type of the entity you want a list of.</typeparam>
252 /// <returns>List of entity objects loaded.</returns>
253 public List<T> GetEntities<T>()
255 return mData.GetEntities<T>();
261 #region Private Types
267 List<RawEntity> mEntities;
269 public Modal(Metadata metadata, char[,] grid, List<RawEntity> entities)
271 Debug.Assert(metadata != null);
272 Debug.Assert(grid != null);
273 Debug.Assert(entities != null);
274 Debug.Assert(metadata.GridWidth * metadata.GridHeight == grid.Length);
276 mMetadata = metadata;
278 mEntities = entities;
281 Console.WriteLine("Loaded map {0} of type {1} written by {2}.",
289 public Metadata Metadata { get { return mMetadata; } }
290 public List<RawEntity> Entities { get { return mEntities; } }
293 public bool IsCellOpen(int x, int y)
295 // TODO: Still need to define characters for types of scenery.
296 return mGrid[x, y] == ' ';
300 public List<object> GetAllEntities()
302 List<object> list = new List<object>();
304 foreach (RawEntity raw in mEntities)
306 if (raw.Attributes.ContainsKey("type"))
308 string typename = raw.Attributes["type"];
310 object[] args = new object[3];
312 args[1] = raw.Position;
313 args[2] = raw.Attributes;
318 object entity = Activator.CreateInstance(System.Type.GetType("CarFire." + typename), args);
319 if (entity != null) list.Add(entity);
320 else throw new RuntimeException();
322 #pragma warning disable 0168
323 catch (System.Exception ex)
324 #pragma warning restore 0168
326 throw new RuntimeException("Entity of type " + typename + " not loaded because an entity class can't be found.");
331 Console.WriteLine("Ignoring entity with identifier " + raw.Id + " since it has no type key.");
338 public List<T> GetEntities<T>()
340 System.Type type = typeof(T);
341 List<T> list = new List<T>();
343 string typename = typeof(T).Name;
344 foreach (RawEntity raw in mEntities)
346 if (raw.Attributes.ContainsKey("type") && typename == raw.Attributes["type"])
348 object[] args = new object[3];
350 args[1] = raw.Position;
351 args[2] = raw.Attributes;
353 T entity = (T)Activator.CreateInstance(type, args);
354 if (entity != null) list.Add(entity);
355 else throw new RuntimeException("Entity of type " + typename + " not loaded because an entity class can't be found.");
367 public Vector2 CenterCell;
371 public View(Modal data)
373 Debug.Assert(data != null);
377 public void Draw(SpriteBatch spriteBatch)
379 mViewport = spriteBatch.GraphicsDevice.Viewport;
381 // TODO: There is no culling yet, but it runs so fast that it probably won't ever need it.
382 for (int y = 0; y < mData.Metadata.GridHeight; y++)
384 for (int x = 0; x < mData.Metadata.GridWidth; x++)
386 if (mData.IsCellOpen(x, y))
388 spriteBatch.Draw(Map.DefaultTile, GetRectangleFromCoordinates(x, y), Color.White);
392 spriteBatch.Draw(Map.DefaultTile, GetRectangleFromCoordinates(x, y), Color.DarkBlue);
399 /// Get a matrix to transform a point from grid-space to screen coordinates. This
400 /// method uses the viewport to bound the edges of the map such that the camera
401 /// will not show anything outside of the grid.
403 /// <param name="center">The point to put in the center.</param>
404 /// <returns>The transformation matrix.</returns>
405 Matrix GetTransformation(Vector2 center)
407 float halfRatio = PixelsToUnitSquares * 0.5f;
408 Matrix transform = Matrix.CreateTranslation(-center.X, -center.Y, 0.0f);
409 transform *= Matrix.CreateScale(PixelsToUnitSquares);
410 transform *= Matrix.CreateTranslation(mViewport.Width * 0.5f - halfRatio,
411 mViewport.Height * 0.5f - halfRatio, 0.0f);
413 Vector2 topLeft = Vector2.Transform(new Vector2(0.0f, 0.0f), transform);
414 topLeft.X = Math.Max(mViewport.X, topLeft.X);
415 topLeft.Y = Math.Max(mViewport.Y, topLeft.Y);
416 transform *= Matrix.CreateTranslation(-topLeft.X, -topLeft.Y, 0.0f);
418 Vector2 bottomRight = Vector2.Transform(new Vector2((float)mData.Metadata.GridWidth,
419 (float)mData.Metadata.GridHeight), transform);
420 float right = mViewport.X + mViewport.Width;
421 float bottom = mViewport.Y + mViewport.Height;
422 bottomRight.X = Math.Min(right, bottomRight.X) - right;
423 bottomRight.Y = Math.Min(bottom, bottomRight.Y) - bottom;
424 transform *= Matrix.CreateTranslation(-bottomRight.X, -bottomRight.Y, 0.0f);
430 public Point GetPointFromCoordinates(float x, float y)
432 Matrix transform = GetTransformation(CenterCell);
433 Vector2 point = Vector2.Transform(new Vector2(x, y), transform);
435 return new Point((int)point.X, (int)point.Y);
438 public Rectangle GetRectangleFromCoordinates(float x, float y)
440 Matrix transform = GetTransformation(CenterCell);
441 Vector2 point = Vector2.Transform(new Vector2(x, y), transform);
443 return new Rectangle((int)point.X, (int)point.Y, (int)PixelsToUnitSquares, (int)PixelsToUnitSquares);
450 #region Private Variables