]> Dogcows Code - chaz/carfire/blobdiff - CarFire/CarFire/CarFire/Map.cs
New map method Map.GetStartingPositionForPlayer to, uh, get the starting positions...
[chaz/carfire] / CarFire / CarFire / CarFire / Map.cs
index c277a7aa4590633359fd50a7d7ef9349f0ec0eff..f3d59d4a705a6e2e341a0177cf4e87f23fb46f7f 100644 (file)
@@ -22,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 = 8.0f;\r
+        public const float PixelsToUnitSquares = 60.0f;\r
 \r
         #endregion\r
 \r
@@ -34,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
@@ -47,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
@@ -78,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
@@ -127,10 +151,12 @@ 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, List<RawEntity> 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, Point[] playerPositions)\r
         {\r
-            mData = new Modal(metadata, grid, entities);\r
+            mData = new Modal(metadata, grid, entities, playerPositions);\r
             mView = new View(mData);\r
         }\r
 \r
@@ -210,6 +236,19 @@ namespace CarFire
         }\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
@@ -241,8 +280,9 @@ namespace CarFire
             Metadata mMetadata;\r
             char[,] mGrid;\r
             List<RawEntity> mEntities;\r
+            Point[] mPlayerPositions;\r
 \r
-            public Modal(Metadata metadata, char[,] grid, List<RawEntity> entities)\r
+            public Modal(Metadata metadata, char[,] grid, List<RawEntity> entities, Point[] playerPositions)\r
             {\r
                 Debug.Assert(metadata != null);\r
                 Debug.Assert(grid != null);\r
@@ -252,6 +292,7 @@ namespace CarFire
                 mMetadata = metadata;\r
                 mGrid = grid;\r
                 mEntities = entities;\r
+                mPlayerPositions = playerPositions;\r
 \r
 #if DEBUG\r
                 Console.WriteLine("Loaded map {0} of type {1} written by {2}.",\r
@@ -264,6 +305,7 @@ namespace CarFire
 \r
             public Metadata Metadata { get { return mMetadata; } }\r
             public List<RawEntity> Entities { get { return mEntities; } }\r
+            public Point[] PlayerPositions { get { return mPlayerPositions; } }\r
 \r
 \r
             public bool IsCellOpen(int x, int y)\r
@@ -288,9 +330,19 @@ namespace CarFire
                         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
+                        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
@@ -318,7 +370,7 @@ namespace CarFire
 \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
+                        else throw new RuntimeException("Entity of type " + typename + " not loaded because an entity class can't be found.");\r
                     }\r
                 }\r
 \r
This page took 0.032196 seconds and 4 git commands to generate.