]> Dogcows Code - chaz/carfire/blobdiff - CarFire/CarFire/CarFire/Map.cs
New Map APIs:
[chaz/carfire] / CarFire / CarFire / CarFire / Map.cs
index 6727133bc79c1ee72471e67d8f30c0fd2fa7349e..2440f121ac6014a16cfdff81984e1348081afe44 100644 (file)
@@ -48,7 +48,7 @@ namespace CarFire
 \r
         #region Public Constants\r
 \r
-        public const float PixelsToUnitSquares = 60.0f;\r
+        public const float PixelsToUnitSquares = 64.0f;\r
 \r
         #endregion\r
 \r
@@ -92,7 +92,7 @@ namespace CarFire
         #endregion\r
 \r
 \r
-        #region Public Attributes\r
+        #region Public Properties\r
 \r
         /// <summary>\r
         /// Get the name of the map.\r
@@ -156,6 +156,12 @@ namespace CarFire
             set { mView.CenterCell = value; }\r
         }\r
 \r
+        public float Zoom\r
+        {\r
+            get { return mView.Zoom; }\r
+            set { mView.Zoom = value; }\r
+        }\r
+\r
         #endregion\r
 \r
 \r
@@ -326,6 +332,16 @@ namespace CarFire
             mData.ClearCell(point.X, point.Y);\r
         }\r
 \r
+\r
+        /// <summary>\r
+        /// Reset the map to the state it was at right after loading.\r
+        /// </summary>\r
+        public void Reset()\r
+        {\r
+            mData.Reset();\r
+            mView.Reset();\r
+        }\r
+\r
         #endregion\r
 \r
 \r
@@ -333,12 +349,11 @@ namespace CarFire
 \r
         class Modal\r
         {\r
-            Metadata mMetadata;\r
-            char[,] mGrid;\r
-            char mDefaultTile;\r
-            List<RawEntity> mEntities;\r
-            Point[] mPlayerPositions;\r
-            bool[,] mBooleanGrid;\r
+            public Metadata Metadata { get { return mMetadata; } }\r
+            public List<RawEntity> Entities { get { return mEntities; } }\r
+            public Point[] PlayerPositions { get { return mPlayerPositions; } }\r
+            public bool[,] Grid { get { return mBooleanGrid; } }\r
+\r
 \r
             public Modal(Metadata metadata, char[,] grid, char defaultTile,\r
                 List<RawEntity> entities, Point[] playerPositions)\r
@@ -349,19 +364,12 @@ namespace CarFire
                 Debug.Assert(metadata.GridWidth * metadata.GridHeight == grid.Length);\r
 \r
                 mMetadata = metadata;\r
-                mGrid = grid;\r
+                mCleanGrid = grid;\r
                 mDefaultTile = defaultTile;\r
                 mEntities = entities;\r
                 mPlayerPositions = playerPositions;\r
 \r
-                mBooleanGrid = new bool[mMetadata.GridWidth, mMetadata.GridHeight];\r
-                for (int x = 0; x < mMetadata.GridWidth; x++)\r
-                {\r
-                    for (int y = 0; y < mMetadata.GridHeight; y++)\r
-                    {\r
-                        mBooleanGrid[x, y] = IsCellOpen(x, y);\r
-                    }\r
-                }\r
+                Reset();\r
 \r
 #if DEBUG\r
                 Console.WriteLine("Loaded map {0} of type {1} written by {2}.",\r
@@ -372,22 +380,35 @@ namespace CarFire
             }\r
 \r
 \r
-            public Metadata Metadata { get { return mMetadata; } }\r
-            public List<RawEntity> Entities { get { return mEntities; } }\r
-            public Point[] PlayerPositions { get { return mPlayerPositions; } }\r
-            public bool[,] Grid { get { return mBooleanGrid; } }\r
+            public void Reset()\r
+            {\r
+                mGrid = (char[,])mCleanGrid.Clone();\r
+\r
+                mBooleanGrid = new bool[mMetadata.GridWidth, mMetadata.GridHeight];\r
+                for (int x = 0; x < mMetadata.GridWidth; x++)\r
+                {\r
+                    for (int y = 0; y < mMetadata.GridHeight; y++)\r
+                    {\r
+                        mBooleanGrid[x, y] = IsCellOpen(x, y);\r
+                    }\r
+                }\r
+            }\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
+                if (IsOnMap(x, y)) return mGrid[x, y] == ' ';\r
+                return false;\r
             }\r
 \r
             public void SetCell(int x, int y, char tile)\r
             {\r
-                mGrid[x, y] = tile;\r
-                mBooleanGrid[x, y] = IsCellOpen(x, y);\r
+                if (IsOnMap(x, y))\r
+                {\r
+                    mGrid[x, y] = tile;\r
+                    mBooleanGrid[x, y] = IsCellOpen(x, y);\r
+                }\r
             }\r
 \r
             public void ClearCell(int x, int y)\r
@@ -395,6 +416,11 @@ namespace CarFire
                 SetCell(x, y, mDefaultTile);\r
             }\r
 \r
+            public bool IsOnMap(int x, int y)\r
+            {\r
+                return 0 <= x && x < Metadata.GridWidth && 0 <= y && y < Metadata.GridHeight;\r
+            }\r
+\r
 \r
             public List<object> GetAllEntities()\r
             {\r
@@ -457,22 +483,39 @@ namespace CarFire
 \r
                 return list;\r
             }\r
+\r
+\r
+            Metadata mMetadata;\r
+            char[,] mGrid;\r
+            char[,] mCleanGrid;\r
+            bool[,] mBooleanGrid;\r
+            char mDefaultTile;\r
+            List<RawEntity> mEntities;\r
+            Point[] mPlayerPositions;\r
         }\r
 \r
         class View\r
         {\r
-            Modal mData;\r
-\r
             public Vector2 CenterCell;\r
-            Viewport mViewport;\r
+            public float Zoom;\r
 \r
 \r
             public View(Modal data)\r
             {\r
                 Debug.Assert(data != null);\r
                 mData = data;\r
+\r
+                Reset();\r
             }\r
 \r
+\r
+            public void Reset()\r
+            {\r
+                CenterCell = Vector2.Zero;\r
+                Zoom = PixelsToUnitSquares;\r
+            }\r
+\r
+\r
             public void Draw(SpriteBatch spriteBatch)\r
             {\r
                 mViewport = spriteBatch.GraphicsDevice.Viewport;\r
@@ -494,18 +537,30 @@ namespace CarFire
                 }\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
+\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)Math.Round(point.X, 0), (int)Math.Round(point.Y, 0), (int)Math.Round(Zoom, 0), (int)Math.Round(Zoom, 0));\r
+            }\r
+\r
+\r
+\r
             Matrix GetTransformation(Vector2 center)\r
             {\r
-                float halfRatio = PixelsToUnitSquares * 0.5f;\r
+                float halfRatio = Zoom * 0.5f;\r
                 Matrix transform = Matrix.CreateTranslation(-center.X, -center.Y, 0.0f);\r
-                transform *= Matrix.CreateScale(PixelsToUnitSquares);\r
+                transform *= Matrix.CreateScale(Zoom);\r
                 transform *= Matrix.CreateTranslation(mViewport.Width * 0.5f - halfRatio,\r
                     mViewport.Height * 0.5f - halfRatio, 0.0f);\r
 \r
@@ -526,21 +581,8 @@ namespace CarFire
             }\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
+            Modal mData;\r
+            Viewport mViewport;\r
         }\r
 \r
         #endregion\r
This page took 0.026558 seconds and 4 git commands to generate.