]> Dogcows Code - chaz/carfire/blobdiff - CarFire/CarFire/CarFire/Map.cs
git-svn-id: https://bd85.net/svn/cs3505_group@83 92bb83a3-7c8f-8a45-bc97-515c4e399668
[chaz/carfire] / CarFire / CarFire / CarFire / Map.cs
index b61f2bbcb1eef2f8e0e556efdcff645b3daf5d63..ddfa9d5457b309f80a9fd8a48bad537fcee7ff06 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
@@ -21,9 +22,33 @@ namespace CarFire
         // 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
+        public const float PixelsToUnitSquares = 60.0f;\r
 \r
         #endregion\r
 \r
@@ -33,7 +58,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,7 +71,7 @@ namespace CarFire
         public class Metadata\r
         {\r
             public string Name;\r
-            public Type Type;\r
+            public Mode Type;\r
             public string Author;\r
             public HashSet<int> NumPlayers = new HashSet<int>();\r
             public string Tileset;\r
@@ -54,6 +79,16 @@ namespace CarFire
             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
@@ -67,7 +102,7 @@ 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
@@ -92,7 +127,16 @@ namespace CarFire
         // TODO: This should return whatever object we end up using for tilesets.\r
         public string Tileset { get { return mData.Metadata.Tileset; } }\r
 \r
+        /// <summary>\r
+        /// Get a list of the raw entity containers loaded with the map.\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
+        /// </summary>\r
         public Vector2 CenterCell\r
         {\r
             get { return mView.CenterCell; }\r
@@ -107,8 +151,10 @@ 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, List<RawEntity> entities)\r
         {\r
             mData = new Modal(metadata, grid, entities);\r
             mView = new View(mData);\r
@@ -182,7 +228,7 @@ namespace CarFire
         /// <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
@@ -191,14 +237,24 @@ namespace CarFire
 \r
 \r
         /// <summary>\r
-        /// Get the entities loaded from the map file.\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<object> 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
-        /// <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
+        /// <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.Entities;\r
+            return mData.GetEntities<T>();\r
         }\r
 \r
         #endregion\r
@@ -210,9 +266,9 @@ namespace CarFire
         {\r
             Metadata mMetadata;\r
             char[,] mGrid;\r
-            Dictionary<char, Dictionary<string, string>> mEntities;\r
+            List<RawEntity> mEntities;\r
 \r
-            public Modal(Metadata metadata, char[,] grid, Dictionary<char, Dictionary<string, string>> entities)\r
+            public Modal(Metadata metadata, char[,] grid, List<RawEntity> entities)\r
             {\r
                 Debug.Assert(metadata != null);\r
                 Debug.Assert(grid != null);\r
@@ -233,7 +289,7 @@ namespace CarFire
 \r
 \r
             public Metadata Metadata { get { return mMetadata; } }\r
-            public Dictionary<char, Dictionary<string, string>> Entities { get { return mEntities; } }\r
+            public List<RawEntity> Entities { get { return mEntities; } }\r
 \r
 \r
             public bool IsCellOpen(int x, int y)\r
@@ -241,6 +297,69 @@ namespace CarFire
                 // TODO: Still need to define characters for types of scenery.\r
                 return mGrid[x, y] == ' ';\r
             }\r
+\r
+\r
+            public List<object> GetAllEntities()\r
+            {\r
+                List<object> list = new List<object>();\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[3];\r
+                        args[0] = raw.Id;\r
+                        args[1] = raw.Position;\r
+                        args[2] = raw.Attributes;\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
+                        }\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[3];\r
+                        args[0] = raw.Id;\r
+                        args[1] = raw.Position;\r
+                        args[2] = raw.Attributes;\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
+                    }\r
+                }\r
+\r
+                return list;\r
+            }\r
         }\r
 \r
         class View\r
This page took 0.034463 seconds and 4 git commands to generate.