]> Dogcows Code - chaz/carfire/blobdiff - CarFire/CarFire/CarFire/Map.cs
Entity loading implemented.
[chaz/carfire] / CarFire / CarFire / CarFire / Map.cs
index 95a8c2c9b75253b17cc4debd169d312ca3cf43f5..c277a7aa4590633359fd50a7d7ef9349f0ec0eff 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
@@ -23,7 +24,7 @@ namespace CarFire
 \r
         #region Public Constants\r
 \r
-        public const float PixelsToUnitSquares = 64.0f;\r
+        public const float PixelsToUnitSquares = 8.0f;\r
 \r
         #endregion\r
 \r
@@ -54,6 +55,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
@@ -92,6 +103,11 @@ 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
@@ -112,7 +128,7 @@ namespace CarFire
         /// 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
+        public Map(Metadata metadata, char[,] grid, List<RawEntity> entities)\r
         {\r
             mData = new Modal(metadata, grid, entities);\r
             mView = new View(mData);\r
@@ -186,7 +202,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
@@ -195,14 +211,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>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
+        /// <returns>List of entity objects loaded.</returns>\r
+        public List<object> GetAllEntities()\r
         {\r
-            return mData.Entities;\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
         #endregion\r
@@ -214,9 +240,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
@@ -237,7 +263,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
@@ -245,6 +271,59 @@ 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
+                        object entity = Activator.CreateInstance(System.Type.GetType("CarFire." + typename), args);\r
+                        if (entity != null) list.Add(entity);\r
+                        else Console.WriteLine("Entity of type " + typename + " not loaded because an entity class can't be found.");\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 Console.WriteLine("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.021445 seconds and 4 git commands to generate.