From 1368c1af3d7a4a12b0b0577dbe3edbfd254e2d04 Mon Sep 17 00:00:00 2001 From: Charles Date: Mon, 19 Apr 2010 04:28:51 +0000 Subject: [PATCH] New Map APIs: 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 | 9 ++ CarFire/CarFire/CarFire/Map.cs | 144 +++++++++++++++++++---------- CarFire/CarFire/CarFire/XnaGame.cs | 21 +---- 3 files changed, 103 insertions(+), 71 deletions(-) diff --git a/CarFire/CarFire/CarFire/Display.cs b/CarFire/CarFire/CarFire/Display.cs index ab4a2c3..fc24895 100644 --- a/CarFire/CarFire/CarFire/Display.cs +++ b/CarFire/CarFire/CarFire/Display.cs @@ -1,5 +1,9 @@ #undef SINGLE_TEST +// Define INGAME_ZOOM to allow zooming in and out with +// the PageUp and PageDown keys. +#define INGAME_ZOOM + using System; using System.Collections.Generic; using System.Linq; @@ -169,6 +173,11 @@ namespace CarFire } } + +#if INGAME_ZOOM + if (Keyboard.GetState().IsKeyDown(Keys.PageUp)) mMap.Zoom = mMap.Zoom + 0.5f; + if (Keyboard.GetState().IsKeyDown(Keys.PageDown)) mMap.Zoom = mMap.Zoom - 0.5f; +#endif } /// diff --git a/CarFire/CarFire/CarFire/Map.cs b/CarFire/CarFire/CarFire/Map.cs index 6727133..2440f12 100644 --- a/CarFire/CarFire/CarFire/Map.cs +++ b/CarFire/CarFire/CarFire/Map.cs @@ -48,7 +48,7 @@ namespace CarFire #region Public Constants - public const float PixelsToUnitSquares = 60.0f; + public const float PixelsToUnitSquares = 64.0f; #endregion @@ -92,7 +92,7 @@ namespace CarFire #endregion - #region Public Attributes + #region Public Properties /// /// Get the name of the map. @@ -156,6 +156,12 @@ namespace CarFire set { mView.CenterCell = value; } } + public float Zoom + { + get { return mView.Zoom; } + set { mView.Zoom = value; } + } + #endregion @@ -326,6 +332,16 @@ namespace CarFire mData.ClearCell(point.X, point.Y); } + + /// + /// Reset the map to the state it was at right after loading. + /// + public void Reset() + { + mData.Reset(); + mView.Reset(); + } + #endregion @@ -333,12 +349,11 @@ namespace CarFire class Modal { - Metadata mMetadata; - char[,] mGrid; - char mDefaultTile; - List mEntities; - Point[] mPlayerPositions; - bool[,] mBooleanGrid; + public Metadata Metadata { get { return mMetadata; } } + public List Entities { get { return mEntities; } } + public Point[] PlayerPositions { get { return mPlayerPositions; } } + public bool[,] Grid { get { return mBooleanGrid; } } + public Modal(Metadata metadata, char[,] grid, char defaultTile, List entities, Point[] playerPositions) @@ -349,19 +364,12 @@ namespace CarFire Debug.Assert(metadata.GridWidth * metadata.GridHeight == grid.Length); mMetadata = metadata; - mGrid = grid; + mCleanGrid = grid; mDefaultTile = defaultTile; mEntities = entities; mPlayerPositions = playerPositions; - mBooleanGrid = new bool[mMetadata.GridWidth, mMetadata.GridHeight]; - for (int x = 0; x < mMetadata.GridWidth; x++) - { - for (int y = 0; y < mMetadata.GridHeight; y++) - { - mBooleanGrid[x, y] = IsCellOpen(x, y); - } - } + Reset(); #if DEBUG Console.WriteLine("Loaded map {0} of type {1} written by {2}.", @@ -372,22 +380,35 @@ namespace CarFire } - public Metadata Metadata { get { return mMetadata; } } - public List Entities { get { return mEntities; } } - public Point[] PlayerPositions { get { return mPlayerPositions; } } - public bool[,] Grid { get { return mBooleanGrid; } } + public void Reset() + { + mGrid = (char[,])mCleanGrid.Clone(); + + mBooleanGrid = new bool[mMetadata.GridWidth, mMetadata.GridHeight]; + for (int x = 0; x < mMetadata.GridWidth; x++) + { + for (int y = 0; y < mMetadata.GridHeight; y++) + { + mBooleanGrid[x, y] = IsCellOpen(x, y); + } + } + } public bool IsCellOpen(int x, int y) { // TODO: Still need to define characters for types of scenery. - return mGrid[x, y] == ' '; + if (IsOnMap(x, y)) return mGrid[x, y] == ' '; + return false; } public void SetCell(int x, int y, char tile) { - mGrid[x, y] = tile; - mBooleanGrid[x, y] = IsCellOpen(x, y); + if (IsOnMap(x, y)) + { + mGrid[x, y] = tile; + mBooleanGrid[x, y] = IsCellOpen(x, y); + } } public void ClearCell(int x, int y) @@ -395,6 +416,11 @@ namespace CarFire SetCell(x, y, mDefaultTile); } + public bool IsOnMap(int x, int y) + { + return 0 <= x && x < Metadata.GridWidth && 0 <= y && y < Metadata.GridHeight; + } + public List GetAllEntities() { @@ -457,22 +483,39 @@ namespace CarFire return list; } + + + Metadata mMetadata; + char[,] mGrid; + char[,] mCleanGrid; + bool[,] mBooleanGrid; + char mDefaultTile; + List mEntities; + Point[] mPlayerPositions; } class View { - Modal mData; - public Vector2 CenterCell; - Viewport mViewport; + public float Zoom; public View(Modal data) { Debug.Assert(data != null); mData = data; + + Reset(); } + + public void Reset() + { + CenterCell = Vector2.Zero; + Zoom = PixelsToUnitSquares; + } + + public void Draw(SpriteBatch spriteBatch) { mViewport = spriteBatch.GraphicsDevice.Viewport; @@ -494,18 +537,30 @@ namespace CarFire } } - /// - /// Get a matrix to transform a point from grid-space to screen coordinates. This - /// method uses the viewport to bound the edges of the map such that the camera - /// will not show anything outside of the grid. - /// - /// The point to put in the center. - /// The transformation matrix. + + public Point GetPointFromCoordinates(float x, float y) + { + Matrix transform = GetTransformation(CenterCell); + Vector2 point = Vector2.Transform(new Vector2(x, y), transform); + + return new Point((int)point.X, (int)point.Y); + } + + public Rectangle GetRectangleFromCoordinates(float x, float y) + { + Matrix transform = GetTransformation(CenterCell); + Vector2 point = Vector2.Transform(new Vector2(x, y), transform); + + 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)); + } + + + Matrix GetTransformation(Vector2 center) { - float halfRatio = PixelsToUnitSquares * 0.5f; + float halfRatio = Zoom * 0.5f; Matrix transform = Matrix.CreateTranslation(-center.X, -center.Y, 0.0f); - transform *= Matrix.CreateScale(PixelsToUnitSquares); + transform *= Matrix.CreateScale(Zoom); transform *= Matrix.CreateTranslation(mViewport.Width * 0.5f - halfRatio, mViewport.Height * 0.5f - halfRatio, 0.0f); @@ -526,21 +581,8 @@ namespace CarFire } - public Point GetPointFromCoordinates(float x, float y) - { - Matrix transform = GetTransformation(CenterCell); - Vector2 point = Vector2.Transform(new Vector2(x, y), transform); - - return new Point((int)point.X, (int)point.Y); - } - - public Rectangle GetRectangleFromCoordinates(float x, float y) - { - Matrix transform = GetTransformation(CenterCell); - Vector2 point = Vector2.Transform(new Vector2(x, y), transform); - - return new Rectangle((int)point.X, (int)point.Y, (int)PixelsToUnitSquares, (int)PixelsToUnitSquares); - } + Modal mData; + Viewport mViewport; } #endregion diff --git a/CarFire/CarFire/CarFire/XnaGame.cs b/CarFire/CarFire/CarFire/XnaGame.cs index 56f7479..b314317 100644 --- a/CarFire/CarFire/CarFire/XnaGame.cs +++ b/CarFire/CarFire/CarFire/XnaGame.cs @@ -1,7 +1,3 @@ - -// DEBUG: for map testing -//#define MAP_TESTING - using System; using System.Collections.Generic; using System.Linq; @@ -29,10 +25,6 @@ namespace CarFire IScreenManager screenManager; IDeterministicGame deterministicGame; -#if MAP_TESTING - Map map; -#endif - public XnaGame() { graphics = new GraphicsDeviceManager(this); @@ -64,6 +56,7 @@ namespace CarFire base.Initialize(); } + /// /// LoadContent will be called once per game and is the place to load /// all of your content. @@ -75,13 +68,6 @@ namespace CarFire screenManager.LoadContent(Content, graphics); deterministicGame.LoadContent(Content); - -#if MAP_TESTING - map = Content.Load("Maps/stable"); - Map.DefaultTile = Content.Load("default"); - map.CenterCell = new Vector2(2, 4); - List entities = map.GetAllEntities(); -#endif } /// @@ -112,13 +98,8 @@ namespace CarFire /// Provides a snapshot of timing values. protected override void Draw(GameTime gameTime) { - GraphicsDevice.Clear(Color.Red); - spriteBatch.Begin(); networkGame.Draw(gameTime, spriteBatch); -#if MAP_TESTING - map.Draw(spriteBatch); -#endif spriteBatch.End(); base.Draw(gameTime); -- 2.43.0