]> 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 ed8c39fd1957986f0014a404d44c702ed982bfe3..ddfa9d5457b309f80a9fd8a48bad537fcee7ff06 100644 (file)
@@ -2,11 +2,12 @@
 using System.Collections.Generic;\r
 using System.Linq;\r
 using System.Text;\r
-using Microsoft.Xna.Framework;\r
 using System.IO;\r
-using System.Text.RegularExpressions;\r
 using System.Runtime.Serialization;\r
 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,33 +19,441 @@ namespace CarFire
     /// </summary>\r
     public class Map\r
     {\r
-        #region Types\r
+        // DEBUG: Tilesets not implemented at all.\r
+        public static Texture2D DefaultTile;\r
 \r
-        public class Data\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 string Author;\r
-            public string Name;\r
-            public int MinNumPlayers;\r
-            public int MaxNumPlayers;\r
-            public Point Dimensions;\r
-            public string Tileset;        // TODO: this should be a tilemap object\r
-            public Type Type;\r
-            public char[,] Grid;\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
-        public enum Type\r
+\r
+        #endregion\r
+\r
+\r
+        #region Public Constants\r
+\r
+        public const float PixelsToUnitSquares = 60.0f;\r
+\r
+        #endregion\r
+\r
+\r
+        #region Public Types\r
+\r
+        /// <summary>\r
+        /// The type of a map helps determine how the map is intended to be used.\r
+        /// </summary>\r
+        public enum Mode\r
         {\r
             None,\r
             Campaign,\r
             Battle\r
         }\r
 \r
+        /// <summary>\r
+        /// The container class for map metadata.\r
+        /// </summary>\r
+        public class Metadata\r
+        {\r
+            public string Name;\r
+            public Mode Type;\r
+            public string Author;\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
+\r
+        /// <summary>\r
+        /// Get the name of the map.\r
+        /// </summary>\r
+        public string Name { get { return mData.Metadata.Name; } }\r
+\r
+        /// <summary>\r
+        /// Get the type of the map.\r
+        /// </summary>\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 a set of integers containing each allowable number of players.\r
+        /// </summary>\r
+        public HashSet<int> NumPlayers { get { return mData.Metadata.NumPlayers; } }\r
+\r
+        /// <summary>\r
+        /// Get the width of the map, in grid units.\r
+        /// </summary>\r
+        public int Width { get { return mData.Metadata.GridWidth; } }\r
+\r
+        /// <summary>\r
+        /// Get the height of the map, in grid units.\r
+        /// </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
+        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
+            set { mView.CenterCell = value; }\r
+        }\r
+\r
+        #endregion\r
+\r
+\r
+        #region Public Methods\r
+\r
+        /// <summary>\r
+        /// Construct a map with the provided map data.\r
+        /// </summary>\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
+        }\r
+\r
+\r
+        /// <summary>\r
+        /// Draw a representation of the map to the screen.\r
+        /// </summary>\r
+        /// <param name="spriteBatch">The jeewiz.</param>\r
+        public void Draw(SpriteBatch spriteBatch)\r
+        {\r
+            mView.Draw(spriteBatch);\r
+        }\r
+\r
+\r
+        /// <summary>\r
+        /// Get a point in screen-space from a coordinate in gridspace.\r
+        /// </summary>\r
+        /// <param name="x">X-coordinate.</param>\r
+        /// <param name="y">Y-coordinate.</param>\r
+        /// <returns>Transformed point.</returns>\r
+        public Point GetPointFromCoordinates(float x, float y)\r
+        {\r
+            return mView.GetPointFromCoordinates(x, y);\r
+        }\r
+\r
+        /// <summary>\r
+        /// Get a point in screen-space from a coordinate in gridspace.\r
+        /// </summary>\r
+        /// <param name="point">X,Y-coordinates.</param>\r
+        /// <returns>Transformed point.</returns>\r
+        public Point GetPointFromCoordinates(Vector2 point)\r
+        {\r
+            return mView.GetPointFromCoordinates(point.X, point.Y);\r
+        }\r
+\r
+        /// <summary>\r
+        /// Get a rectangle in screen-space centered around a coordinate in gridspace.\r
+        /// </summary>\r
+        /// <param name="x">X-coordinate.</param>\r
+        /// <param name="y">Y-coordinate.</param>\r
+        /// <returns>Transformed rectangle.</returns>\r
+        public Rectangle GetRectangleFromCoordinates(float x, float y)\r
+        {\r
+            return mView.GetRectangleFromCoordinates(x, y);\r
+        }\r
+\r
+        /// <summary>\r
+        /// Get a rectangle in screen-space centered around a coordinate in gridspace.\r
+        /// </summary>\r
+        /// <param name="point">X,Y-coordinates.</param>\r
+        /// <returns>Transformed rectangle.</returns>\r
+        public Rectangle GetRectangleFromCoordinates(Vector2 point)\r
+        {\r
+            return mView.GetRectangleFromCoordinates(point.X, point.Y);\r
+        }\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-coordinate.</param>\r
+        /// <param name="y">Y-coordinate.</param>\r
+        /// <returns>True if cell can be occupied, false otherwise.</returns>\r
+        public bool IsCellOpen(int x, int y)\r
+        {\r
+            return mData.IsCellOpen(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="point">X,Y-coordinates.</param>\r
+        /// <returns>True if cell can be occupied, false otherwise.</returns>\r
+        public bool IsCellOpen(Point point)\r
+        {\r
+            return mData.IsCellOpen(point.X, point.Y);\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
+        /// <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
+        /// <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
 \r
 \r
-        public Map(Data data)\r
+        #region Private Types\r
+\r
+        class Modal\r
         {\r
-            Console.WriteLine("Read map " + data.Name + " of type " + data.Type + " written by " + data.Author);\r
+            Metadata mMetadata;\r
+            char[,] mGrid;\r
+            List<RawEntity> mEntities;\r
+\r
+            public Modal(Metadata metadata, char[,] grid, List<RawEntity> entities)\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
+\r
+                mMetadata = metadata;\r
+                mGrid = grid;\r
+                mEntities = entities;\r
+\r
+#if DEBUG\r
+                Console.WriteLine("Loaded map {0} of type {1} written by {2}.",\r
+                    metadata.Name,\r
+                    metadata.Type,\r
+                    metadata.Author);\r
+#endif\r
+            }\r
+\r
+\r
+            public Metadata Metadata { get { return mMetadata; } }\r
+            public List<RawEntity> Entities { get { return mEntities; } }\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
+            }\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
+        {\r
+            Modal mData;\r
+\r
+            public Vector2 CenterCell;\r
+            Viewport mViewport;\r
+\r
+\r
+            public View(Modal data)\r
+            {\r
+                Debug.Assert(data != null);\r
+                mData = data;\r
+            }\r
+\r
+            public void Draw(SpriteBatch spriteBatch)\r
+            {\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
+                    }\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
+            Matrix GetTransformation(Vector2 center)\r
+            {\r
+                float halfRatio = PixelsToUnitSquares * 0.5f;\r
+                Matrix transform = Matrix.CreateTranslation(-center.X, -center.Y, 0.0f);\r
+                transform *= Matrix.CreateScale(PixelsToUnitSquares);\r
+                transform *= Matrix.CreateTranslation(mViewport.Width * 0.5f - halfRatio,\r
+                    mViewport.Height * 0.5f - halfRatio, 0.0f);\r
+\r
+                Vector2 topLeft = Vector2.Transform(new Vector2(0.0f, 0.0f), transform);\r
+                topLeft.X = Math.Max(mViewport.X, topLeft.X);\r
+                topLeft.Y = Math.Max(mViewport.Y, topLeft.Y);\r
+                transform *= Matrix.CreateTranslation(-topLeft.X, -topLeft.Y, 0.0f);\r
+\r
+                Vector2 bottomRight = Vector2.Transform(new Vector2((float)mData.Metadata.GridWidth,\r
+                    (float)mData.Metadata.GridHeight), transform);\r
+                float right = mViewport.X + mViewport.Width;\r
+                float bottom = mViewport.Y + mViewport.Height;\r
+                bottomRight.X = Math.Min(right, bottomRight.X) - right;\r
+                bottomRight.Y = Math.Min(bottom, bottomRight.Y) - bottom;\r
+                transform *= Matrix.CreateTranslation(-bottomRight.X, -bottomRight.Y, 0.0f);\r
+\r
+                return transform;\r
+            }\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
         }\r
+\r
+        #endregion\r
+\r
+\r
+        #region Private Variables\r
+\r
+        Modal mData;\r
+        View mView;\r
+\r
+        #endregion\r
     }\r
 }\r
This page took 0.032629 seconds and 4 git commands to generate.