]> Dogcows Code - chaz/carfire/commitdiff
New Map APIs:
authorCharles <Charles@92bb83a3-7c8f-8a45-bc97-515c4e399668>
Mon, 19 Apr 2010 04:28:51 +0000 (04:28 +0000)
committerCharles <Charles@92bb83a3-7c8f-8a45-bc97-515c4e399668>
Mon, 19 Apr 2010 04:28:51 +0000 (04:28 +0000)
Map.Reset to remove any modifications to the map after it was loaded.
Map.Zoom to change the zoom of the map in-game... for testing and debugging.
Removed my map-testing junk from the base game class.

git-svn-id: https://bd85.net/svn/cs3505_group@106 92bb83a3-7c8f-8a45-bc97-515c4e399668

CarFire/CarFire/CarFire/Display.cs
CarFire/CarFire/CarFire/Map.cs
CarFire/CarFire/CarFire/XnaGame.cs

index ab4a2c302c80e3603ff33e2fde9ce7563d20704a..fc2489530e8f53caea9de9b88f5f8ff99be9fb78 100644 (file)
@@ -1,5 +1,9 @@
 #undef SINGLE_TEST\r
 \r
+// Define INGAME_ZOOM to allow zooming in and out with\r
+// the PageUp and PageDown keys.\r
+#define INGAME_ZOOM\r
+\r
 using System;\r
 using System.Collections.Generic;\r
 using System.Linq;\r
@@ -169,6 +173,11 @@ namespace CarFire
                 }          \r
 \r
             }\r
+\r
+#if INGAME_ZOOM\r
+            if (Keyboard.GetState().IsKeyDown(Keys.PageUp)) mMap.Zoom = mMap.Zoom + 0.5f;\r
+            if (Keyboard.GetState().IsKeyDown(Keys.PageDown)) mMap.Zoom = mMap.Zoom - 0.5f;\r
+#endif\r
         }\r
 \r
         /// <summary>\r
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
index 56f7479fddb7d6dacbed1eb45fe48a78e07ea3a1..b314317f2747a81f87785f094747f35da4034190 100644 (file)
@@ -1,7 +1,3 @@
-\r
-// DEBUG: for map testing\r
-//#define MAP_TESTING\r
-\r
 using System;\r
 using System.Collections.Generic;\r
 using System.Linq;\r
@@ -29,10 +25,6 @@ namespace CarFire
         IScreenManager screenManager;\r
         IDeterministicGame deterministicGame;\r
 \r
-#if MAP_TESTING\r
-        Map map;\r
-#endif\r
-\r
         public XnaGame()\r
         {\r
             graphics = new GraphicsDeviceManager(this);\r
@@ -64,6 +56,7 @@ namespace CarFire
             base.Initialize();\r
         }\r
 \r
+\r
         /// <summary>\r
         /// LoadContent will be called once per game and is the place to load\r
         /// all of your content.\r
@@ -75,13 +68,6 @@ namespace CarFire
 \r
             screenManager.LoadContent(Content, graphics);\r
             deterministicGame.LoadContent(Content);\r
-\r
-#if MAP_TESTING\r
-            map = Content.Load<Map>("Maps/stable");\r
-            Map.DefaultTile = Content.Load<Texture2D>("default");\r
-            map.CenterCell = new Vector2(2, 4);\r
-            List<object> entities = map.GetAllEntities();\r
-#endif\r
         }\r
 \r
         /// <summary>\r
@@ -112,13 +98,8 @@ namespace CarFire
         /// <param name="gameTime">Provides a snapshot of timing values.</param>\r
         protected override void Draw(GameTime gameTime)\r
         {\r
-            GraphicsDevice.Clear(Color.Red);\r
-\r
             spriteBatch.Begin();\r
             networkGame.Draw(gameTime, spriteBatch);\r
-#if MAP_TESTING\r
-            map.Draw(spriteBatch);\r
-#endif\r
             spriteBatch.End();\r
 \r
             base.Draw(gameTime);\r
This page took 0.037152 seconds and 4 git commands to generate.