+\r
+\r
+ /// <summary>\r
+ /// Determine whether or not a cell can be occupied by a game entity.\r
+ /// </summary>\r
+ /// <param name="x">X-coordinate.</param>\r
+ /// <param name="y">Y-coordinate.</param>\r
+ /// <returns>True if cell can be occupied, false otherwise.</returns>\r
+ public bool IsCellOpen(int x, int y)\r
+ {\r
+ return mData.IsCellOpen(x, y);\r
+ }\r
+\r
+ /// <summary>\r
+ /// Determine whether or not a cell can be occupied by a game entity.\r
+ /// </summary>\r
+ /// <param name="point">X,Y-coordinates.</param>\r
+ /// <returns>True if cell can be occupied, false otherwise.</returns>\r
+ public bool IsCellOpen(Point point)\r
+ {\r
+ return mData.IsCellOpen(point.X, point.Y);\r
+ }\r
+\r
+\r
+ /// <summary>\r
+ /// Get the starting position of a player.\r
+ /// </summary>\r
+ /// <param name="playerNumber">The number of the player (i.e. 1-4).\r
+ /// This number must be a valid player number.</param>\r
+ /// <returns>The starting position of the player.</returns>\r
+ public Point GetStartingPositionForPlayer(int playerNumber)\r
+ {\r
+ Debug.Assert(1 <= playerNumber && playerNumber <= NumPlayers.Max());\r
+ return mData.PlayerPositions[playerNumber];\r
+ }\r
+\r
+\r
+ /// <summary>\r
+ /// Get all the entities loaded from the map file. Exceptions could be\r
+ /// thrown if there are entities without associated classes.\r
+ /// </summary>\r
+ /// <returns>List of entity objects loaded.</returns>\r
+ public List<IEntity> GetAllEntities()\r
+ {\r
+ return mData.GetAllEntities();\r
+ }\r
+\r
+ /// <summary>\r
+ /// Get the entities of a certain type loaded from the map file. Exceptions\r
+ /// could be thrown if there are entities without associated classes.\r
+ /// </summary>\r
+ /// <typeparam name="T">Type of the entity you want a list of.</typeparam>\r
+ /// <returns>List of entity objects loaded.</returns>\r
+ public List<T> GetEntities<T>()\r
+ {\r
+ return mData.GetEntities<T>();\r
+ }\r
+\r
+\r
+ /// <summary>\r
+ /// Set the tile of a cell.\r
+ /// </summary>\r
+ /// <param name="x">X-coordinate.</param>\r
+ /// <param name="y">Y-coordinate.</param>\r
+ /// <param name="tile">The character representing the tile.</param>\r
+ public void SetCell(int x, int y, char tile)\r
+ {\r
+ mData.SetCell(x, y, tile);\r
+ }\r
+\r
+ /// <summary>\r
+ /// Set the tile of a cell.\r
+ /// </summary>\r
+ /// <param name="point">X,Y-coordinates.</param>\r
+ /// <param name="tile">The character representing the tile.</param>\r
+ public void SetCell(Point point, char tile)\r
+ {\r
+ mData.SetCell(point.X, point.Y, tile);\r
+ }\r
+\r
+\r
+ /// <summary>\r
+ /// Clear a cell to the default tile.\r
+ /// </summary>\r
+ /// <param name="x">X-coordinate.</param>\r
+ /// <param name="y">Y-coordinate.</param>\r
+ public void ClearCell(int x, int y)\r
+ {\r
+ mData.ClearCell(x, y);\r
+ }\r
+\r
+ /// <summary>\r
+ /// Clear a cell to the default tile.\r
+ /// </summary>\r
+ /// <param name="point">X,Y-coordinates.</param>\r
+ public void ClearCell(Point point)\r
+ {\r
+ mData.ClearCell(point.X, point.Y);\r
+ }\r
+\r
+\r
+ /// <summary>\r
+ /// Reset the map to the state it was at right after loading.\r
+ /// </summary>\r
+ public void Reset()\r
+ {\r
+ mData.Reset();\r
+ mView.Reset();\r
+ }\r
+\r
+ #endregion\r
+\r
+\r
+ #region Private Types\r
+\r
+ class Model\r
+ {\r
+ public Metadata Metadata { get { return mMetadata; } }\r
+ public List<RawEntity> Entities { get { return mEntities; } }\r
+ public Point[] PlayerPositions { get { return mPlayerPositions; } }\r
+ public bool[,] Grid { get { return mBooleanGrid; } }\r
+\r
+ public Game Game;\r
+\r
+\r
+ public Model(Metadata metadata, char[,] grid, char defaultTile,\r
+ List<RawEntity> entities, Point[] playerPositions)\r
+ {\r
+ Debug.Assert(metadata != null);\r
+ Debug.Assert(grid != null);\r
+ Debug.Assert(entities != null);\r
+ Debug.Assert(metadata.GridWidth * metadata.GridHeight == grid.Length);\r
+\r
+ mMetadata = metadata;\r
+ mCleanGrid = grid;\r
+ mDefaultTile = defaultTile;\r
+ mEntities = entities;\r
+ mPlayerPositions = playerPositions;\r
+\r
+ Reset();\r
+\r
+#if DEBUG\r
+ Console.WriteLine("Loaded map {0} of type {1} written by {2}.",\r
+ metadata.Name,\r
+ metadata.Type,\r
+ metadata.Author);\r
+#endif\r
+ }\r
+\r
+\r
+ public void Reset()\r
+ {\r
+ mGrid = (char[,])mCleanGrid.Clone();\r
+\r
+ mBooleanGrid = new bool[mMetadata.GridWidth, mMetadata.GridHeight];\r
+ for (int x = 0; x < mMetadata.GridWidth; x++)\r
+ {\r
+ for (int y = 0; y < mMetadata.GridHeight; y++)\r
+ {\r
+ mBooleanGrid[x, y] = IsCellOpen(x, y);\r
+ }\r
+ }\r
+ }\r
+\r
+\r
+ public bool IsCellOpen(int x, int y)\r
+ {\r
+ // TODO: Still need to define characters for types of scenery.\r
+ if (IsOnMap(x, y)) return mGrid[x, y] == ' ';\r
+ return false;\r
+ }\r
+\r
+ public void SetCell(int x, int y, char tile)\r
+ {\r
+ if (IsOnMap(x, y))\r
+ {\r
+ mGrid[x, y] = tile;\r
+ mBooleanGrid[x, y] = IsCellOpen(x, y);\r
+ }\r
+ }\r
+\r
+ public void ClearCell(int x, int y)\r
+ {\r
+ SetCell(x, y, mDefaultTile);\r
+ }\r
+\r
+ public bool IsOnMap(int x, int y)\r
+ {\r
+ return 0 <= x && x < Metadata.GridWidth && 0 <= y && y < Metadata.GridHeight;\r
+ }\r
+\r
+\r
+ public List<IEntity> GetAllEntities()\r
+ {\r
+ List<IEntity> list = new List<IEntity>();\r
+\r
+ foreach (RawEntity raw in mEntities)\r
+ {\r
+ if (raw.Attributes.ContainsKey("type"))\r
+ {\r
+ string typename = raw.Attributes["type"];\r
+\r
+ object[] args = new object[4];\r
+ args[0] = raw.Id;\r
+ args[1] = raw.Position;\r
+ args[2] = raw.Attributes;\r
+ args[3] = Game;\r
+\r
+ try\r
+ {\r
+ IEntity entity = (IEntity)Activator.CreateInstance(System.Type.GetType("CarFire." + typename), args);\r
+ if (entity != null)\r
+ {\r
+ entity.LoadContent(Game.ContentManager);\r
+ list.Add(entity);\r
+ }\r
+ else throw new RuntimeException();\r
+ }\r
+#pragma warning disable 0168\r
+ catch (System.Exception ex)\r
+#pragma warning restore 0168\r
+ {\r
+ throw new RuntimeException("Entity of type " + typename + " not loaded because an entity class can't be found.");\r
+ }\r
+ }\r
+ else\r
+ {\r
+ Console.WriteLine("Ignoring entity with identifier " + raw.Id + " since it has no type key.");\r
+ }\r
+ }\r
+\r
+ return list;\r
+ }\r
+\r
+ public List<T> GetEntities<T>()\r
+ {\r
+ System.Type type = typeof(T);\r
+ List<T> list = new List<T>();\r
+\r
+ string typename = typeof(T).Name;\r
+ foreach (RawEntity raw in mEntities)\r
+ {\r
+ if (raw.Attributes.ContainsKey("type") && typename == raw.Attributes["type"])\r
+ {\r
+ object[] args = new object[4];\r
+ args[0] = raw.Id;\r
+ args[1] = raw.Position;\r
+ args[2] = raw.Attributes;\r
+ args[3] = Game;\r
+\r
+ T entity = (T)Activator.CreateInstance(type, args);\r
+ if (entity != null)\r
+ {\r
+ ((IEntity)entity).LoadContent(Game.ContentManager);\r
+ list.Add(entity);\r
+ }\r
+ else throw new RuntimeException("Entity of type " + typename + " not loaded because an entity class can't be found.");\r
+ }\r
+ }\r
+\r
+ return list;\r
+ }\r
+\r
+\r
+ Metadata mMetadata;\r
+ char[,] mGrid;\r
+ char[,] mCleanGrid;\r
+ bool[,] mBooleanGrid;\r
+ char mDefaultTile;\r
+ List<RawEntity> mEntities;\r
+ Point[] mPlayerPositions;\r
+ }\r
+\r
+ class View\r
+ {\r
+ public Vector2 CenterCell;\r
+ public float Zoom;\r
+\r
+\r
+ public View(Model data)\r
+ {\r
+ Debug.Assert(data != null);\r
+ mData = data;\r
+\r
+ Reset();\r
+ }\r
+\r
+\r
+ public void Reset()\r
+ {\r
+ CenterCell = Vector2.Zero;\r
+ Zoom = PixelsToUnitSquares;\r
+ }\r
+\r
+\r
+ public void Draw(SpriteBatch spriteBatch)\r
+ {\r
+ mViewport = spriteBatch.GraphicsDevice.Viewport;\r
+\r
+ // TODO: There is no culling yet, but it runs so fast that it probably won't ever need it.\r
+ for (int y = 0; y < mData.Metadata.GridHeight; y++)\r
+ {\r
+ for (int x = 0; x < mData.Metadata.GridWidth; x++)\r
+ {\r
+ if (mData.IsCellOpen(x, y))\r
+ {\r
+ spriteBatch.Draw(Map.DefaultTile, GetRectangleFromCoordinates(x, y), Color.White);\r
+ }\r
+ else\r
+ {\r
+ spriteBatch.Draw(Map.DefaultTile, GetRectangleFromCoordinates(x, y), Color.DarkBlue);\r
+ }\r
+ }\r
+ }\r
+ }\r
+\r
+\r
+ public Point GetPointFromCoordinates(float x, float y)\r
+ {\r
+ Matrix transform = GetTransformation(CenterCell);\r
+ Vector2 point = Vector2.Transform(new Vector2(x, y), transform);\r
+\r
+ return new Point((int)point.X, (int)point.Y);\r
+ }\r
+\r
+ public Rectangle GetRectangleFromCoordinates(float x, float y)\r
+ {\r
+ Matrix transform = GetTransformation(CenterCell);\r
+ Vector2 point = Vector2.Transform(new Vector2(x, y), transform);\r
+ \r
+ 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));\r
+ }\r
+\r
+\r
+\r
+ Matrix GetTransformation(Vector2 center)\r
+ {\r
+ float halfRatio = Zoom * 0.5f;\r
+ Matrix transform = Matrix.CreateTranslation(-center.X, -center.Y, 0.0f);\r
+ transform *= Matrix.CreateScale(Zoom);\r
+ transform *= Matrix.CreateTranslation(mViewport.Width * 0.5f - halfRatio,\r
+ mViewport.Height * 0.5f - halfRatio, 0.0f);\r
+\r
+ Vector2 topLeft = Vector2.Transform(new Vector2(0.0f, 0.0f), transform);\r
+ topLeft.X = Math.Max(mViewport.X, topLeft.X);\r
+ topLeft.Y = Math.Max(mViewport.Y, topLeft.Y);\r
+ transform *= Matrix.CreateTranslation(-topLeft.X, -topLeft.Y, 0.0f);\r
+\r
+ Vector2 bottomRight = Vector2.Transform(new Vector2((float)mData.Metadata.GridWidth,\r
+ (float)mData.Metadata.GridHeight), transform);\r
+ float right = mViewport.X + mViewport.Width;\r
+ float bottom = mViewport.Y + mViewport.Height;\r
+ bottomRight.X = Math.Min(right, bottomRight.X) - right;\r
+ bottomRight.Y = Math.Min(bottom, bottomRight.Y) - bottom;\r
+ transform *= Matrix.CreateTranslation(-bottomRight.X, -bottomRight.Y, 0.0f);\r
+\r
+ return transform;\r
+ }\r
+\r
+\r
+ Model mData;\r
+ Viewport mViewport;\r
+ }\r
+\r
+ #endregion\r
+\r
+\r
+ #region Private Variables\r
+\r
+ Model mData;\r
+ View mView;\r
+\r
+ #endregion\r