2 using System.Collections.Generic;
6 using System.Runtime.Serialization;
7 using System.Diagnostics;
8 using Microsoft.Xna.Framework;
9 using Microsoft.Xna.Framework.Graphics;
14 /// A map object represents the map or virtual world where players and other
15 /// game entities exist. The map consists of a grid where each grid space can
16 /// contain static scenery and/or game entities which can move and interact
17 /// with other game entities.
21 // DEBUG: Tilesets not implemented at all.
22 public static Texture2D DefaultTile;
24 #region Public Constants
26 public const float PixelsToUnitSquares = 64.0f;
34 /// The type of a map helps determine how the map is intended to be used.
44 /// The container class for map metadata.
51 public HashSet<int> NumPlayers = new HashSet<int>();
52 public string Tileset;
54 public int GridHeight;
60 #region Public Attributes
63 /// Get the name of the map.
65 public string Name { get { return mData.Metadata.Name; } }
68 /// Get the type of the map.
70 //public Type Type { get { return mData.mMetadata.Type; } }
73 /// Get the author of the map.
75 public string Author { get { return mData.Metadata.Author; } }
78 /// Get a set of integers containing each allowable number of players.
80 public HashSet<int> NumPlayers { get { return mData.Metadata.NumPlayers; } }
83 /// Get the width of the map, in grid units.
85 public int Width { get { return mData.Metadata.GridWidth; } }
88 /// Get the height of the map, in grid units.
90 public int Height { get { return mData.Metadata.GridHeight; } }
92 // TODO: This should return whatever object we end up using for tilesets.
93 public string Tileset { get { return mData.Metadata.Tileset; } }
96 public Vector2 CenterCell
98 get { return mView.CenterCell; }
99 set { mView.CenterCell = value; }
105 #region Public Methods
108 /// Construct a map with the provided map data.
110 /// <param name="data">Map data.</param>
111 public Map(Metadata metadata, char[,] grid, Dictionary<char, Dictionary<string, string>> entities)
113 mData = new Modal(metadata, grid, entities);
114 mView = new View(mData);
119 /// Draw a representation of the map to the screen.
121 /// <param name="spriteBatch">The jeewiz.</param>
122 public void Draw(SpriteBatch spriteBatch)
124 mView.Draw(spriteBatch);
129 /// Get a point in screen-space from a coordinate in gridspace.
131 /// <param name="x">X-coordinate.</param>
132 /// <param name="y">Y-coordinate.</param>
133 /// <returns>Transformed point.</returns>
134 public Point GetPointFromCoordinates(float x, float y)
136 return mView.GetPointFromCoordinates(x, y);
140 /// Get a point in screen-space from a coordinate in gridspace.
142 /// <param name="point">X,Y-coordinates.</param>
143 /// <returns>Transformed point.</returns>
144 public Point GetPointFromCoordinates(Vector2 point)
146 return mView.GetPointFromCoordinates(point.X, point.Y);
150 /// Get a rectangle in screen-space centered around a coordinate in gridspace.
152 /// <param name="x">X-coordinate.</param>
153 /// <param name="y">Y-coordinate.</param>
154 /// <returns>Transformed rectangle.</returns>
155 public Rectangle GetRectangleFromCoordinates(float x, float y)
157 return mView.GetRectangleFromCoordinates(x, y);
161 /// Get a rectangle in screen-space centered around a coordinate in gridspace.
163 /// <param name="point">X,Y-coordinates.</param>
164 /// <returns>Transformed rectangle.</returns>
165 public Rectangle GetRectangleFromCoordinates(Vector2 point)
167 return mView.GetRectangleFromCoordinates(point.X, point.Y);
172 /// Determine whether or not a cell can be occupied by a game entity.
174 /// <param name="x">X-coordinate.</param>
175 /// <param name="y">Y-coordinate.</param>
176 /// <returns>True if cell can be occupied, false otherwise.</returns>
177 public bool IsCellOpen(int x, int y)
179 return mData.IsCellOpen(x, y);
183 /// Determine whether or not a cell can be occupied by a game entity.
185 /// <param name="x">X,Y-coordinates.</param>
186 /// <returns>True if cell can be occupied, false otherwise.</returns>
187 public bool IsCellOpen(Point point)
189 return mData.IsCellOpen(point.X, point.Y);
194 /// Get the entities loaded from the map file.
196 /// <returns>Dictionary of entities. The keys are the entity
197 /// identifiers and the value is a dictionary of key-value pairs
198 /// associated with that entity.</returns>
199 public Dictionary<char, Dictionary<string, string>> GetEntities()
201 return mData.Entities;
207 #region Private Types
213 Dictionary<char, Dictionary<string, string>> mEntities;
215 public Modal(Metadata metadata, char[,] grid, Dictionary<char, Dictionary<string, string>> entities)
217 Debug.Assert(metadata != null);
218 Debug.Assert(grid != null);
219 Debug.Assert(entities != null);
220 Debug.Assert(metadata.GridWidth * metadata.GridHeight == grid.Length);
222 mMetadata = metadata;
224 mEntities = entities;
227 Console.WriteLine("Loaded map {0} of type {1} written by {2}.",
235 public Metadata Metadata { get { return mMetadata; } }
236 public Dictionary<char, Dictionary<string, string>> Entities { get { return mEntities; } }
239 public bool IsCellOpen(int x, int y)
241 // TODO: Still need to define characters for types of scenery.
242 return mGrid[x, y] == ' ';
250 public Vector2 CenterCell;
254 public View(Modal data)
256 Debug.Assert(data != null);
260 public void Draw(SpriteBatch spriteBatch)
262 mViewport = spriteBatch.GraphicsDevice.Viewport;
264 // TODO: There is no culling yet, but it runs so fast that it probably won't ever need it.
265 for (int y = 0; y < mData.Metadata.GridHeight; y++)
267 for (int x = 0; x < mData.Metadata.GridWidth; x++)
269 if (mData.IsCellOpen(x, y))
271 spriteBatch.Draw(Map.DefaultTile, GetRectangleFromCoordinates(x, y), Color.White);
275 spriteBatch.Draw(Map.DefaultTile, GetRectangleFromCoordinates(x, y), Color.DarkBlue);
282 /// Get a matrix to transform a point from grid-space to screen coordinates. This
283 /// method uses the viewport to bound the edges of the map such that the camera
284 /// will not show anything outside of the grid.
286 /// <param name="center">The point to put in the center.</param>
287 /// <returns>The transformation matrix.</returns>
288 Matrix GetTransformation(Vector2 center)
290 float halfRatio = PixelsToUnitSquares * 0.5f;
291 Matrix transform = Matrix.CreateTranslation(-center.X, -center.Y, 0.0f);
292 transform *= Matrix.CreateScale(PixelsToUnitSquares);
293 transform *= Matrix.CreateTranslation(mViewport.Width * 0.5f - halfRatio,
294 mViewport.Height * 0.5f - halfRatio, 0.0f);
296 Vector2 topLeft = Vector2.Transform(new Vector2(0.0f, 0.0f), transform);
297 topLeft.X = Math.Max(mViewport.X, topLeft.X);
298 topLeft.Y = Math.Max(mViewport.Y, topLeft.Y);
299 transform *= Matrix.CreateTranslation(-topLeft.X, -topLeft.Y, 0.0f);
301 Vector2 bottomRight = Vector2.Transform(new Vector2((float)mData.Metadata.GridWidth,
302 (float)mData.Metadata.GridHeight), transform);
303 float right = mViewport.X + mViewport.Width;
304 float bottom = mViewport.Y + mViewport.Height;
305 bottomRight.X = Math.Min(right, bottomRight.X) - right;
306 bottomRight.Y = Math.Min(bottom, bottomRight.Y) - bottom;
307 transform *= Matrix.CreateTranslation(-bottomRight.X, -bottomRight.Y, 0.0f);
313 public Point GetPointFromCoordinates(float x, float y)
315 Matrix transform = GetTransformation(CenterCell);
316 Vector2 point = Vector2.Transform(new Vector2(x, y), transform);
318 return new Point((int)point.X, (int)point.Y);
321 public Rectangle GetRectangleFromCoordinates(float x, float y)
323 Matrix transform = GetTransformation(CenterCell);
324 Vector2 point = Vector2.Transform(new Vector2(x, y), transform);
326 return new Rectangle((int)point.X, (int)point.Y, (int)PixelsToUnitSquares, (int)PixelsToUnitSquares);
333 #region Private Variables