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 #region Public Constants
24 public const float PixelsToUnitSquares = 64.0f;
32 /// The type of a map helps determine how the map is intended to be used.
42 /// The container class for map metadata.
49 public HashSet<int> NumPlayers = new HashSet<int>();
50 public string Tileset;
52 public int GridHeight;
56 /// The container class for information about an entity defined in the map.
58 public class RawEntity
61 public Point Position;
62 public Dictionary<string, string> Attributes = new Dictionary<string, string>();
68 #region Public Properties
71 /// Get the name of the map.
73 public string Name { get { return mData.Metadata.Name; } }
76 /// Get the type of the map.
78 public Mode Type { get { return mData.Metadata.Type; } }
81 /// Get the author of the map.
83 public string Author { get { return mData.Metadata.Author; } }
86 /// Get a set of integers containing each allowable number of players.
88 public HashSet<int> NumPlayers { get { return mData.Metadata.NumPlayers; } }
91 /// Get the width of the map, in grid units.
93 public int Width { get { return mData.Metadata.GridWidth; } }
96 /// Get the height of the map, in grid units.
98 public int Height { get { return mData.Metadata.GridHeight; } }
101 /// Get the name of the tileset.
103 public string Tileset { get { return mData.Metadata.Tileset; } }
106 /// Get the current grid of open cells. On the grid, true means
107 /// the cell is open (i.e. an entity can occupy that cell), and false
108 /// means the cell is closed. Note that, just like Map.IsCellOpen,
109 /// only static scenery is considered; the grid cannot tell you
110 /// whether or not an entity is already occupying the cell.
112 public bool[,] Grid { get { return mData.Grid; } }
115 /// Get a list of the raw entity containers loaded with the map. If you
116 /// want to get the actual entity objects, use Map.GetEntities and
117 /// Map.GetAllEntities instead.
119 public List<RawEntity> RawEntities { get { return mData.Entities; } }
123 /// Get and set the coordinate of the grid cell that should be in
124 /// the center of the screen when the map is drawn. Setting this
125 /// will change the viewport of the map and will effect the return
126 /// values of Map.GetPointFromCoordinates and Map.GetRectangleFromCoordinates.
128 public Vector2 CenterCell
130 get { return mView.CenterCell; }
131 set { mView.CenterCell = value; }
135 /// Get and set the tilemap with its associated texture and tile
136 /// character to coordinate mappings. This effects what the map looks
137 /// like when it is drawn.
139 public static Tilemap Tilemap;
142 /// Get and set the zoom of the map view. The default zoom is
143 /// Map.PixelsToUnitSquares.
147 get { return mView.Zoom; }
148 set { mView.Zoom = value; }
154 #region Public Methods
157 /// Construct a map with the provided map data.
159 /// <param name="metadata">The metadata.</param>
160 /// <param name="grid">The grid.</param>
161 /// <param name="entities">The entities.</param>
162 public Map(Metadata metadata, char[,] grid, char defaultTile,
163 List<RawEntity> entities, Point[] playerPositions)
165 mData = new Model(metadata, grid, defaultTile, entities, playerPositions);
166 mView = new View(mData);
171 /// Draw a representation of the map to the screen.
173 /// <param name="spriteBatch">The jeewiz.</param>
174 public void Draw(SpriteBatch spriteBatch)
176 mView.Draw(spriteBatch);
181 /// Get a point in screen-space from a coordinate in gridspace.
183 /// <param name="x">X-coordinate.</param>
184 /// <param name="y">Y-coordinate.</param>
185 /// <returns>Transformed point.</returns>
186 public Point GetPointFromCoordinates(float x, float y)
188 return mView.GetPointFromCoordinates(x, y);
192 /// Get a point in screen-space from a coordinate in gridspace.
194 /// <param name="point">X,Y-coordinates.</param>
195 /// <returns>Transformed point.</returns>
196 public Point GetPointFromCoordinates(Vector2 point)
198 return mView.GetPointFromCoordinates(point.X, point.Y);
202 /// Get a rectangle in screen-space centered around a coordinate in gridspace.
204 /// <param name="x">X-coordinate.</param>
205 /// <param name="y">Y-coordinate.</param>
206 /// <returns>Transformed rectangle.</returns>
207 public Rectangle GetRectangleFromCoordinates(float x, float y)
209 return mView.GetRectangleFromCoordinates(x, y);
213 /// Get a rectangle in screen-space centered around a coordinate in gridspace.
215 /// <param name="point">X,Y-coordinates.</param>
216 /// <returns>Transformed rectangle.</returns>
217 public Rectangle GetRectangleFromCoordinates(Vector2 point)
219 return mView.GetRectangleFromCoordinates(point.X, point.Y);
224 /// Determine whether or not a cell can be occupied by a game entity.
226 /// <param name="x">X-coordinate.</param>
227 /// <param name="y">Y-coordinate.</param>
228 /// <returns>True if cell can be occupied, false otherwise.</returns>
229 public bool IsCellOpen(int x, int y)
231 return mData.IsCellOpen(x, y);
235 /// created by Brady for AI precalculations
237 /// <param name="x">X-coordinate.</param>
238 /// <param name="y">Y-coordinate.</param>
239 public bool IsWall(int x, int y)
241 return mData.IsWall(x, y);
245 /// Determine whether or not a cell can be occupied by a game entity.
247 /// <param name="point">X,Y-coordinates.</param>
248 /// <returns>True if cell can be occupied, false otherwise.</returns>
249 public bool IsCellOpen(Point point)
251 return mData.IsCellOpen(point.X, point.Y);
256 /// Get the starting position of a player.
258 /// <param name="playerNumber">The number of the player (i.e. 1-4).
259 /// This number must be a valid player number.</param>
260 /// <returns>The starting position of the player.</returns>
261 public Point GetStartingPositionForPlayer(int playerNumber)
263 Debug.Assert(1 <= playerNumber && playerNumber <= NumPlayers.Max());
264 return mData.PlayerPositions[playerNumber];
269 /// Get all the entities loaded from the map file. Exceptions could be
270 /// thrown if there are entities without associated classes.
272 /// <param name="game">The game reference to be passed to entities.</param>
273 /// <returns>List of entity objects loaded.</returns>
274 public List<IEntity> GetAllEntities(Game game)
276 return mData.GetAllEntities(game);
280 /// Get the entities of a certain type loaded from the map file. Exceptions
281 /// could be thrown if there are entities without associated classes.
283 /// <param name="game">The game reference to be passed to entities.</param>
284 /// <typeparam name="T">Type of the entity you want a list of.</typeparam>
285 /// <returns>List of entity objects loaded.</returns>
286 public List<T> GetEntities<T>(Game game)
288 return mData.GetEntities<T>(game);
293 /// Set the tile of a cell.
295 /// <param name="x">X-coordinate.</param>
296 /// <param name="y">Y-coordinate.</param>
297 /// <param name="tile">The character representing the tile.</param>
298 public void SetCell(int x, int y, char tile)
300 mData.SetCell(x, y, tile);
304 /// Set the tile of a cell.
306 /// <param name="point">X,Y-coordinates.</param>
307 /// <param name="tile">The character representing the tile.</param>
308 public void SetCell(Point point, char tile)
310 mData.SetCell(point.X, point.Y, tile);
315 /// Clear a cell to the default tile.
317 /// <param name="x">X-coordinate.</param>
318 /// <param name="y">Y-coordinate.</param>
319 public void ClearCell(int x, int y)
321 mData.ClearCell(x, y);
325 /// Clear a cell to the default tile.
327 /// <param name="point">X,Y-coordinates.</param>
328 public void ClearCell(Point point)
330 mData.ClearCell(point.X, point.Y);
335 /// Reset the map to the state it was at right after loading.
346 #region Private Types
350 public Metadata Metadata { get { return mMetadata; } }
351 public List<RawEntity> Entities { get { return mEntities; } }
352 public Point[] PlayerPositions { get { return mPlayerPositions; } }
353 public bool[,] Grid { get { return mBooleanGrid; } }
356 public Model(Metadata metadata, char[,] grid, char defaultTile,
357 List<RawEntity> entities, Point[] playerPositions)
359 Debug.Assert(metadata != null);
360 Debug.Assert(grid != null);
361 Debug.Assert(entities != null);
362 Debug.Assert(metadata.GridWidth * metadata.GridHeight == grid.Length);
363 Debug.Assert(Tilemap != null);
365 mMetadata = metadata;
367 mDefaultTile = defaultTile;
368 mEntities = entities;
369 mPlayerPositions = playerPositions;
374 Console.WriteLine("Loaded map {0} of type {1} written by {2}.",
384 mGrid = (char[,])mCleanGrid.Clone();
386 mBooleanGrid = new bool[mMetadata.GridWidth, mMetadata.GridHeight];
387 for (int x = 0; x < mMetadata.GridWidth; x++)
389 for (int y = 0; y < mMetadata.GridHeight; y++)
391 mBooleanGrid[x, y] = IsCellOpen(x, y);
397 public bool IsCellOpen(int x, int y)
399 if (!IsOnMap(x, y)) return false;
400 return (Tilemap.GetTileFlags(mGrid[x, y]) & TileFlags.Open) == TileFlags.Open;
403 //created by Brady for AI precalculations
404 public bool IsWall(int x, int y)
406 if (!IsOnMap(x, y)) return false;
407 return (Tilemap.GetTileFlags(mGrid[x, y]) & TileFlags.Wall) == TileFlags.Wall;
410 public void SetCell(int x, int y, char tile)
415 mBooleanGrid[x, y] = IsCellOpen(x, y);
419 public char GetCell(int x, int y)
421 if (IsOnMap(x, y)) return mGrid[x, y];
425 public void ClearCell(int x, int y)
427 SetCell(x, y, mDefaultTile);
430 public bool IsOnMap(int x, int y)
432 return 0 <= x && x < Metadata.GridWidth && 0 <= y && y < Metadata.GridHeight;
436 public List<IEntity> GetAllEntities(Game game)
438 List<IEntity> list = new List<IEntity>();
440 foreach (RawEntity raw in mEntities)
442 if (raw.Attributes.ContainsKey("type"))
444 string typename = raw.Attributes["type"];
446 object[] args = new object[4];
448 args[1] = raw.Position;
449 args[2] = raw.Attributes;
454 IEntity entity = (IEntity)Activator.CreateInstance(System.Type.GetType("CarFire." + typename), args);
457 entity.LoadContent(game.ContentManager);
460 else throw new Exception();
462 #pragma warning disable 0168
463 catch (System.Exception ex)
464 #pragma warning restore 0168
466 throw new Exception("Entity of type " + typename + " not loaded because an entity class can't be found.");
471 Console.WriteLine("Ignoring entity with identifier " + raw.Id + " since it has no type key.");
478 public List<T> GetEntities<T>(Game game)
480 System.Type type = typeof(T);
481 List<T> list = new List<T>();
483 string typename = typeof(T).Name;
484 foreach (RawEntity raw in mEntities)
486 if (raw.Attributes.ContainsKey("type") && typename == raw.Attributes["type"])
488 object[] args = new object[4];
490 args[1] = raw.Position;
491 args[2] = raw.Attributes;
494 T entity = (T)Activator.CreateInstance(type, args);
497 ((IEntity)entity).LoadContent(game.ContentManager);
500 else throw new Exception("Entity of type " + typename + " not loaded because an entity class can't be found.");
511 bool[,] mBooleanGrid;
513 List<RawEntity> mEntities;
514 Point[] mPlayerPositions;
519 public Vector2 CenterCell;
523 public View(Model data)
525 Debug.Assert(data != null);
534 CenterCell = Vector2.Zero;
535 Zoom = PixelsToUnitSquares;
539 public void Draw(SpriteBatch spriteBatch)
541 if (Tilemap == null) throw new Exception("Cannot draw map without first setting the tilemap.");
542 mViewport = spriteBatch.GraphicsDevice.Viewport;
544 for (int y = 0; y < mData.Metadata.GridHeight; y++)
546 for (int x = 0; x < mData.Metadata.GridWidth; x++)
548 Tilemap.Draw(spriteBatch, mData.GetCell(x, y), GetRectangleFromCoordinates(x, y));
554 public Point GetPointFromCoordinates(float x, float y)
556 Matrix transform = GetTransformation(CenterCell);
557 Vector2 point = Vector2.Transform(new Vector2(x, y), transform);
559 return new Point((int)point.X, (int)point.Y);
562 public Rectangle GetRectangleFromCoordinates(float x, float y)
564 Matrix transform = GetTransformation(CenterCell);
565 Vector2 point = Vector2.Transform(new Vector2(x, y), transform);
567 return new Rectangle((int)Math.Round(point.X, 0), (int)Math.Round(point.Y, 0), (int)Math.Round(Zoom, 0), (int)Math.Round(Zoom, 0));
572 Matrix GetTransformation(Vector2 center)
574 float halfRatio = Zoom * 0.5f;
575 Matrix transform = Matrix.CreateTranslation(-center.X, -center.Y, 0.0f);
576 transform *= Matrix.CreateScale(Zoom);
577 transform *= Matrix.CreateTranslation(mViewport.Width * 0.5f - halfRatio,
578 mViewport.Height * 0.5f - halfRatio, 0.0f);
580 Vector2 topLeft = Vector2.Transform(new Vector2(0.0f, 0.0f), transform);
581 topLeft.X = Math.Max(mViewport.X, topLeft.X);
582 topLeft.Y = Math.Max(mViewport.Y, topLeft.Y);
583 transform *= Matrix.CreateTranslation(-topLeft.X, -topLeft.Y, 0.0f);
585 Vector2 bottomRight = Vector2.Transform(new Vector2((float)mData.Metadata.GridWidth,
586 (float)mData.Metadata.GridHeight), transform);
587 float right = mViewport.X + mViewport.Width;
588 float bottom = mViewport.Y + mViewport.Height;
589 bottomRight.X = Math.Min(right, bottomRight.X) - right;
590 bottomRight.Y = Math.Min(bottom, bottomRight.Y) - bottom;
591 transform *= Matrix.CreateTranslation(-bottomRight.X, -bottomRight.Y, 0.0f);
604 #region Private Variables