]> Dogcows Code - chaz/carfire/blobdiff - CarFire/CarFire/CarFire/Map.cs
New IEntity properties: Coordinates (set), Identifier. Loadable entities should...
[chaz/carfire] / CarFire / CarFire / CarFire / Map.cs
index 95a8c2c9b75253b17cc4debd169d312ca3cf43f5..504a68ce7f9dca706131c80835b2c67c3b1fcce1 100644 (file)
@@ -7,6 +7,7 @@ using System.Runtime.Serialization;
 using System.Diagnostics;\r
 using Microsoft.Xna.Framework;\r
 using Microsoft.Xna.Framework.Graphics;\r
+using System.Reflection;\r
 \r
 namespace CarFire\r
 {\r
@@ -18,9 +19,6 @@ namespace CarFire
     /// </summary>\r
     public class Map\r
     {\r
-        // DEBUG: Tilesets not implemented at all.\r
-        public static Texture2D DefaultTile;\r
-\r
         #region Public Constants\r
 \r
         public const float PixelsToUnitSquares = 64.0f;\r
@@ -33,7 +31,7 @@ namespace CarFire
         /// <summary>\r
         /// The type of a map helps determine how the map is intended to be used.\r
         /// </summary>\r
-        public enum Type\r
+        public enum Mode\r
         {\r
             None,\r
             Campaign,\r
@@ -46,18 +44,29 @@ namespace CarFire
         public class Metadata\r
         {\r
             public string Name;\r
-            public Type Type;\r
+            public Mode Type;\r
             public string Author;\r
+            public string Next;\r
             public HashSet<int> NumPlayers = new HashSet<int>();\r
             public string Tileset;\r
             public int GridWidth;\r
             public int GridHeight;\r
         }\r
 \r
+        /// <summary>\r
+        /// The container class for information about an entity defined in the map.\r
+        /// </summary>\r
+        public class RawEntity\r
+        {\r
+            public char Id;\r
+            public Point Position;\r
+            public Dictionary<string, string> Attributes = new Dictionary<string, string>();\r
+        }\r
+\r
         #endregion\r
 \r
 \r
-        #region Public Attributes\r
+        #region Public Properties\r
 \r
         /// <summary>\r
         /// Get the name of the map.\r
@@ -67,13 +76,18 @@ namespace CarFire
         /// <summary>\r
         /// Get the type of the map.\r
         /// </summary>\r
-        //public Type Type { get { return mData.mMetadata.Type; } }\r
+        public Mode Type { get { return mData.Metadata.Type; } }\r
 \r
         /// <summary>\r
         /// Get the author of the map.\r
         /// </summary>\r
         public string Author { get { return mData.Metadata.Author; } }\r
 \r
+        /// <summary>\r
+        /// Get the name of the next map to load after this one.\r
+        /// </summary>\r
+        public string Next { get { return mData.Metadata.Next; } }\r
+\r
         /// <summary>\r
         /// Get a set of integers containing each allowable number of players.\r
         /// </summary>\r
@@ -89,13 +103,33 @@ namespace CarFire
         /// </summary>\r
         public int Height { get { return mData.Metadata.GridHeight; } }\r
 \r
-        // TODO: This should return whatever object we end up using for tilesets.\r
+        /// <summary>\r
+        /// Get the name of the tileset.\r
+        /// </summary>\r
         public string Tileset { get { return mData.Metadata.Tileset; } }\r
 \r
+        /// <summary>\r
+        /// Get the current grid of open cells.  On the grid, true means\r
+        /// the cell is open (i.e. an entity can occupy that cell), and false\r
+        /// means the cell is closed.  Note that, just like Map.IsCellOpen,\r
+        /// only static scenery is considered; the grid cannot tell you\r
+        /// whether or not an entity is already occupying the cell.\r
+        /// </summary>\r
+        public bool[,] Grid { get { return mData.Grid; } }\r
+\r
+        /// <summary>\r
+        /// Get a list of the raw entity containers loaded with the map.  If you\r
+        /// want to get the actual entity objects, use Map.GetEntities and\r
+        /// Map.GetAllEntities instead.\r
+        /// </summary>\r
+        public List<RawEntity> RawEntities { get { return mData.Entities; } }\r
+\r
 \r
         /// <summary>\r
         /// Get and set the coordinate of the grid cell that should be in\r
-        /// the center of the screen when the map is drawn.\r
+        /// the center of the screen when the map is drawn.  Setting this\r
+        /// will change the viewport of the map and will effect the return\r
+        /// values of Map.GetPointFromCoordinates and Map.GetRectangleFromCoordinates.\r
         /// </summary>\r
         public Vector2 CenterCell\r
         {\r
@@ -103,6 +137,25 @@ namespace CarFire
             set { mView.CenterCell = value; }\r
         }\r
 \r
+        /// <summary>\r
+        /// Get and set the tilemap with its associated texture and tile\r
+        /// character to coordinate mappings.  This effects what the map looks\r
+        /// like when it is drawn.  You will need to reset any map instances\r
+        /// after setting a new tilemap.  You should also set a tilemap before\r
+        /// instantiating any maps.\r
+        /// </summary>\r
+        public static Tilemap Tilemap;\r
+\r
+        /// <summary>\r
+        /// Get and set the zoom of the map view.  The default zoom is\r
+        /// Map.PixelsToUnitSquares.\r
+        /// </summary>\r
+        public float Zoom\r
+        {\r
+            get { return mView.Zoom; }\r
+            set { mView.Zoom = value; }\r
+        }\r
+\r
         #endregion\r
 \r
 \r
@@ -111,10 +164,13 @@ namespace CarFire
         /// <summary>\r
         /// Construct a map with the provided map data.\r
         /// </summary>\r
-        /// <param name="data">Map data.</param>\r
-        public Map(Metadata metadata, char[,] grid, Dictionary<char, Dictionary<string, string>> entities)\r
+        /// <param name="metadata">The metadata.</param>\r
+        /// <param name="grid">The grid.</param>\r
+        /// <param name="entities">The entities.</param>\r
+        public Map(Metadata metadata, char[,] grid, char defaultTile,\r
+            List<RawEntity> entities, Point[] playerPositions)\r
         {\r
-            mData = new Modal(metadata, grid, entities);\r
+            mData = new Model(metadata, grid, defaultTile, entities, playerPositions);\r
             mView = new View(mData);\r
         }\r
 \r
@@ -183,10 +239,20 @@ namespace CarFire
             return mData.IsCellOpen(x, y);\r
         }\r
 \r
+        /// <summary>\r
+        /// created by Brady for AI precalculations\r
+        /// </summary>\r
+        /// <param name="x">X-coordinate.</param>\r
+        /// <param name="y">Y-coordinate.</param>\r
+        public bool IsWall(int x, int y)\r
+        {\r
+            return mData.IsWall(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="x">X,Y-coordinates.</param>\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
@@ -195,14 +261,91 @@ namespace CarFire
 \r
 \r
         /// <summary>\r
-        /// Get the entities loaded from the map file.\r
+        /// Get the starting position of a player.\r
         /// </summary>\r
-        /// <returns>Dictionary of entities.  The keys are the entity\r
-        /// identifiers and the value is a dictionary of key-value pairs\r
-        /// associated with that entity.</returns>\r
-        public Dictionary<char, Dictionary<string, string>> GetEntities()\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
-            return mData.Entities;\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
+        /// <param name="game">The game reference to be passed to entities.</param>\r
+        /// <returns>List of entity objects loaded.</returns>\r
+        public List<IEntity> GetAllEntities(Game game)\r
+        {\r
+            return mData.GetAllEntities(game);\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
+        /// <param name="game">The game reference to be passed to entities.</param>\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>(Game game)\r
+        {\r
+            return mData.GetEntities<T>(game);\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
@@ -210,22 +353,30 @@ namespace CarFire
 \r
         #region Private Types\r
 \r
-        class Modal\r
+        class Model\r
         {\r
-            Metadata mMetadata;\r
-            char[,] mGrid;\r
-            Dictionary<char, Dictionary<string, string>> mEntities;\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
 \r
-            public Modal(Metadata metadata, char[,] grid, Dictionary<char, Dictionary<string, string>> entities)\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
+                Debug.Assert(Tilemap != null);\r
 \r
                 mMetadata = metadata;\r
-                mGrid = grid;\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
@@ -236,64 +387,201 @@ namespace CarFire
             }\r
 \r
 \r
-            public Metadata Metadata { get { return mMetadata; } }\r
-            public Dictionary<char, Dictionary<string, string>> Entities { get { return mEntities; } }\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
-                return mGrid[x, y] == ' ';\r
+                if (!IsOnMap(x, y)) return false;\r
+                return (Tilemap.GetTileFlags(mGrid[x, y]) & TileFlags.Open) == TileFlags.Open;\r
+            }\r
+\r
+            //created by Brady for AI precalculations\r
+            public bool IsWall(int x, int y)\r
+            {\r
+                if (!IsOnMap(x, y)) return false;\r
+                return (Tilemap.GetTileFlags(mGrid[x, y]) & TileFlags.Wall) == TileFlags.Wall;\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 char GetCell(int x, int y)\r
+            {\r
+                if (IsOnMap(x, y)) return mGrid[x, y];\r
+                return mDefaultTile;\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(Game game)\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 Exception();\r
+                        }\r
+#pragma warning disable 0168\r
+                        catch (System.Exception ex)\r
+#pragma warning restore 0168\r
+                        {\r
+                            throw new Exception("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>(Game game)\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 Exception("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
-            Modal mData;\r
-\r
             public Vector2 CenterCell;\r
-            Viewport mViewport;\r
+            public float Zoom;\r
 \r
 \r
-            public View(Modal data)\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
+                if (Tilemap == null) throw new Exception("Cannot draw map without first setting the tilemap.");\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
+                        Tilemap.Draw(spriteBatch, mData.GetCell(x, y), GetRectangleFromCoordinates(x, y));\r
                     }\r
                 }\r
             }\r
 \r
-            /// <summary>\r
-            /// Get a matrix to transform a point from grid-space to screen coordinates.  This\r
-            /// method uses the viewport to bound the edges of the map such that the camera\r
-            /// will not show anything outside of the grid.\r
-            /// </summary>\r
-            /// <param name="center">The point to put in the center.</param>\r
-            /// <returns>The transformation matrix.</returns>\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 = PixelsToUnitSquares * 0.5f;\r
+                float halfRatio = Zoom * 0.5f;\r
                 Matrix transform = Matrix.CreateTranslation(-center.X, -center.Y, 0.0f);\r
-                transform *= Matrix.CreateScale(PixelsToUnitSquares);\r
+                transform *= Matrix.CreateScale(Zoom);\r
                 transform *= Matrix.CreateTranslation(mViewport.Width * 0.5f - halfRatio,\r
                     mViewport.Height * 0.5f - halfRatio, 0.0f);\r
 \r
@@ -314,21 +602,8 @@ namespace CarFire
             }\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)point.X, (int)point.Y, (int)PixelsToUnitSquares, (int)PixelsToUnitSquares);\r
-            }\r
+            Model mData;\r
+            Viewport mViewport;\r
         }\r
 \r
         #endregion\r
@@ -336,7 +611,7 @@ namespace CarFire
 \r
         #region Private Variables\r
 \r
-        Modal mData;\r
+        Model mData;\r
         View mView;\r
 \r
         #endregion\r
This page took 0.032694 seconds and 4 git commands to generate.