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 Constants
27 public const float PixelsToUnitSquares = 8.0f;
35 /// The type of a map helps determine how the map is intended to be used.
45 /// The container class for map metadata.
52 public HashSet<int> NumPlayers = new HashSet<int>();
53 public string Tileset;
55 public int GridHeight;
59 /// The container class for information about an entity defined in the map.
61 public class RawEntity
64 public Point Position;
65 public Dictionary<string, string> Attributes = new Dictionary<string, string>();
71 #region Public Attributes
74 /// Get the name of the map.
76 public string Name { get { return mData.Metadata.Name; } }
79 /// Get the type of the map.
81 //public Type Type { get { return mData.mMetadata.Type; } }
84 /// Get the author of the map.
86 public string Author { get { return mData.Metadata.Author; } }
89 /// Get a set of integers containing each allowable number of players.
91 public HashSet<int> NumPlayers { get { return mData.Metadata.NumPlayers; } }
94 /// Get the width of the map, in grid units.
96 public int Width { get { return mData.Metadata.GridWidth; } }
99 /// Get the height of the map, in grid units.
101 public int Height { get { return mData.Metadata.GridHeight; } }
103 // TODO: This should return whatever object we end up using for tilesets.
104 public string Tileset { get { return mData.Metadata.Tileset; } }
107 /// Get a list of the raw entity containers loaded with the map.
109 public List<RawEntity> RawEntities { get { return mData.Entities; } }
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.
116 public Vector2 CenterCell
118 get { return mView.CenterCell; }
119 set { mView.CenterCell = value; }
125 #region Public Methods
128 /// Construct a map with the provided map data.
130 /// <param name="data">Map data.</param>
131 public Map(Metadata metadata, char[,] grid, List<RawEntity> entities)
133 mData = new Modal(metadata, grid, entities);
134 mView = new View(mData);
139 /// Draw a representation of the map to the screen.
141 /// <param name="spriteBatch">The jeewiz.</param>
142 public void Draw(SpriteBatch spriteBatch)
144 mView.Draw(spriteBatch);
149 /// Get a point in screen-space from a coordinate in gridspace.
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)
156 return mView.GetPointFromCoordinates(x, y);
160 /// Get a point in screen-space from a coordinate in gridspace.
162 /// <param name="point">X,Y-coordinates.</param>
163 /// <returns>Transformed point.</returns>
164 public Point GetPointFromCoordinates(Vector2 point)
166 return mView.GetPointFromCoordinates(point.X, point.Y);
170 /// Get a rectangle in screen-space centered around a coordinate in gridspace.
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)
177 return mView.GetRectangleFromCoordinates(x, y);
181 /// Get a rectangle in screen-space centered around a coordinate in gridspace.
183 /// <param name="point">X,Y-coordinates.</param>
184 /// <returns>Transformed rectangle.</returns>
185 public Rectangle GetRectangleFromCoordinates(Vector2 point)
187 return mView.GetRectangleFromCoordinates(point.X, point.Y);
192 /// Determine whether or not a cell can be occupied by a game entity.
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)
199 return mData.IsCellOpen(x, y);
203 /// Determine whether or not a cell can be occupied by a game entity.
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)
209 return mData.IsCellOpen(point.X, point.Y);
214 /// Get all the entities loaded from the map file. Exceptions could be
215 /// thrown if there are entities without associated classes.
217 /// <returns>List of entity objects loaded.</returns>
218 public List<object> GetAllEntities()
220 return mData.GetAllEntities();
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.
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>()
231 return mData.GetEntities<T>();
237 #region Private Types
243 List<RawEntity> mEntities;
245 public Modal(Metadata metadata, char[,] grid, List<RawEntity> entities)
247 Debug.Assert(metadata != null);
248 Debug.Assert(grid != null);
249 Debug.Assert(entities != null);
250 Debug.Assert(metadata.GridWidth * metadata.GridHeight == grid.Length);
252 mMetadata = metadata;
254 mEntities = entities;
257 Console.WriteLine("Loaded map {0} of type {1} written by {2}.",
265 public Metadata Metadata { get { return mMetadata; } }
266 public List<RawEntity> Entities { get { return mEntities; } }
269 public bool IsCellOpen(int x, int y)
271 // TODO: Still need to define characters for types of scenery.
272 return mGrid[x, y] == ' ';
276 public List<object> GetAllEntities()
278 List<object> list = new List<object>();
280 foreach (RawEntity raw in mEntities)
282 if (raw.Attributes.ContainsKey("type"))
284 string typename = raw.Attributes["type"];
286 object[] args = new object[3];
288 args[1] = raw.Position;
289 args[2] = raw.Attributes;
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.");
297 Console.WriteLine("Ignoring entity with identifier " + raw.Id + " since it has no type key.");
304 public List<T> GetEntities<T>()
306 System.Type type = typeof(T);
307 List<T> list = new List<T>();
309 string typename = typeof(T).Name;
310 foreach (RawEntity raw in mEntities)
312 if (raw.Attributes.ContainsKey("type") && typename == raw.Attributes["type"])
314 object[] args = new object[3];
316 args[1] = raw.Position;
317 args[2] = raw.Attributes;
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.");
333 public Vector2 CenterCell;
337 public View(Modal data)
339 Debug.Assert(data != null);
343 public void Draw(SpriteBatch spriteBatch)
345 mViewport = spriteBatch.GraphicsDevice.Viewport;
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++)
350 for (int x = 0; x < mData.Metadata.GridWidth; x++)
352 if (mData.IsCellOpen(x, y))
354 spriteBatch.Draw(Map.DefaultTile, GetRectangleFromCoordinates(x, y), Color.White);
358 spriteBatch.Draw(Map.DefaultTile, GetRectangleFromCoordinates(x, y), Color.DarkBlue);
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.
369 /// <param name="center">The point to put in the center.</param>
370 /// <returns>The transformation matrix.</returns>
371 Matrix GetTransformation(Vector2 center)
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);
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);
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);
396 public Point GetPointFromCoordinates(float x, float y)
398 Matrix transform = GetTransformation(CenterCell);
399 Vector2 point = Vector2.Transform(new Vector2(x, y), transform);
401 return new Point((int)point.X, (int)point.Y);
404 public Rectangle GetRectangleFromCoordinates(float x, float y)
406 Matrix transform = GetTransformation(CenterCell);
407 Vector2 point = Vector2.Transform(new Vector2(x, y), transform);
409 return new Rectangle((int)point.X, (int)point.Y, (int)PixelsToUnitSquares, (int)PixelsToUnitSquares);
416 #region Private Variables