]> 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 93dc5ef6ba181c1f678298bf79015a0c62d6c118..504a68ce7f9dca706131c80835b2c67c3b1fcce1 100644 (file)
@@ -19,33 +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 Exceptions\r
-\r
-        /// <summary>\r
-        /// This exception is thrown during the loading of a map if any\r
-        /// part of the map file is inconsistent with the expected format\r
-        /// and order.\r
-        /// </summary>\r
-        public class RuntimeException : System.ApplicationException\r
-        {\r
-            public RuntimeException() { }\r
-\r
-            public RuntimeException(string message) :\r
-                base(message) { }\r
-\r
-            public RuntimeException(string message, System.Exception inner) :\r
-                base(message, inner) { }\r
-\r
-            protected RuntimeException(SerializationInfo info, StreamingContext context) :\r
-                base(info, context) { }\r
-        }\r
-\r
-        #endregion\r
-\r
-\r
         #region Public Constants\r
 \r
         public const float PixelsToUnitSquares = 64.0f;\r
@@ -73,6 +46,7 @@ namespace CarFire
             public string Name;\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
@@ -109,6 +83,11 @@ namespace CarFire
         /// </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
@@ -124,7 +103,9 @@ 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
@@ -156,6 +137,15 @@ 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
@@ -249,6 +239,16 @@ 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
@@ -277,21 +277,23 @@ namespace CarFire
         /// 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<object> GetAllEntities()\r
+        public List<IEntity> GetAllEntities(Game game)\r
         {\r
-            return mData.GetAllEntities();\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>()\r
+        public List<T> GetEntities<T>(Game game)\r
         {\r
-            return mData.GetEntities<T>();\r
+            return mData.GetEntities<T>(game);\r
         }\r
 \r
 \r
@@ -366,6 +368,7 @@ namespace CarFire
                 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
                 mCleanGrid = grid;\r
@@ -401,9 +404,15 @@ namespace CarFire
 \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
+                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
@@ -415,6 +424,12 @@ namespace CarFire
                 }\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
@@ -426,9 +441,9 @@ namespace CarFire
             }\r
 \r
 \r
-            public List<object> GetAllEntities()\r
+            public List<IEntity> GetAllEntities(Game game)\r
             {\r
-                List<object> list = new List<object>();\r
+                List<IEntity> list = new List<IEntity>();\r
 \r
                 foreach (RawEntity raw in mEntities)\r
                 {\r
@@ -436,23 +451,27 @@ namespace CarFire
                     {\r
                         string typename = raw.Attributes["type"];\r
 \r
-                        object[] args = new object[3];\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
-\r
-                            object entity = Activator.CreateInstance(System.Type.GetType("CarFire." + typename), args);\r
-                            if (entity != null) list.Add(entity);\r
-                            else throw new RuntimeException();\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 RuntimeException("Entity of type " + typename + " not loaded because an entity class can't be found.");\r
+                            throw new Exception("Entity of type " + typename + " not loaded because an entity class can't be found.");\r
                         }\r
                     }\r
                     else\r
@@ -464,7 +483,7 @@ namespace CarFire
                 return list;\r
             }\r
 \r
-            public List<T> GetEntities<T>()\r
+            public List<T> GetEntities<T>(Game game)\r
             {\r
                 System.Type type = typeof(T);\r
                 List<T> list = new List<T>();\r
@@ -474,14 +493,19 @@ namespace CarFire
                 {\r
                     if (raw.Attributes.ContainsKey("type") && typename == raw.Attributes["type"])\r
                     {\r
-                        object[] args = new object[3];\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) list.Add(entity);\r
-                        else throw new RuntimeException("Entity of type " + typename + " not loaded because an entity class can't be found.");\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
@@ -522,21 +546,14 @@ namespace CarFire
 \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
This page took 0.024603 seconds and 4 git commands to generate.