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; } }
97 /// Get and set the coordinate of the grid cell that should be in
98 /// the center of the screen when the map is drawn.
100 public Vector2 CenterCell
102 get { return mView.CenterCell; }
103 set { mView.CenterCell = value; }
109 #region Public Methods
112 /// Construct a map with the provided map data.
114 /// <param name="data">Map data.</param>
115 public Map(Metadata metadata, char[,] grid, Dictionary<char, Dictionary<string, string>> entities)
117 mData = new Modal(metadata, grid, entities);
118 mView = new View(mData);
123 /// Draw a representation of the map to the screen.
125 /// <param name="spriteBatch">The jeewiz.</param>
126 public void Draw(SpriteBatch spriteBatch)
128 mView.Draw(spriteBatch);
133 /// Get a point in screen-space from a coordinate in gridspace.
135 /// <param name="x">X-coordinate.</param>
136 /// <param name="y">Y-coordinate.</param>
137 /// <returns>Transformed point.</returns>
138 public Point GetPointFromCoordinates(float x, float y)
140 return mView.GetPointFromCoordinates(x, y);
144 /// Get a point in screen-space from a coordinate in gridspace.
146 /// <param name="point">X,Y-coordinates.</param>
147 /// <returns>Transformed point.</returns>
148 public Point GetPointFromCoordinates(Vector2 point)
150 return mView.GetPointFromCoordinates(point.X, point.Y);
154 /// Get a rectangle in screen-space centered around a coordinate in gridspace.
156 /// <param name="x">X-coordinate.</param>
157 /// <param name="y">Y-coordinate.</param>
158 /// <returns>Transformed rectangle.</returns>
159 public Rectangle GetRectangleFromCoordinates(float x, float y)
161 return mView.GetRectangleFromCoordinates(x, y);
165 /// Get a rectangle in screen-space centered around a coordinate in gridspace.
167 /// <param name="point">X,Y-coordinates.</param>
168 /// <returns>Transformed rectangle.</returns>
169 public Rectangle GetRectangleFromCoordinates(Vector2 point)
171 return mView.GetRectangleFromCoordinates(point.X, point.Y);
176 /// Determine whether or not a cell can be occupied by a game entity.
178 /// <param name="x">X-coordinate.</param>
179 /// <param name="y">Y-coordinate.</param>
180 /// <returns>True if cell can be occupied, false otherwise.</returns>
181 public bool IsCellOpen(int x, int y)
183 return mData.IsCellOpen(x, y);
187 /// Determine whether or not a cell can be occupied by a game entity.
189 /// <param name="x">X,Y-coordinates.</param>
190 /// <returns>True if cell can be occupied, false otherwise.</returns>
191 public bool IsCellOpen(Point point)
193 return mData.IsCellOpen(point.X, point.Y);
198 /// Get the entities loaded from the map file.
200 /// <returns>Dictionary of entities. The keys are the entity
201 /// identifiers and the value is a dictionary of key-value pairs
202 /// associated with that entity.</returns>
203 public Dictionary<char, Dictionary<string, string>> GetEntities()
205 return mData.Entities;
211 #region Private Types
217 Dictionary<char, Dictionary<string, string>> mEntities;
219 public Modal(Metadata metadata, char[,] grid, Dictionary<char, Dictionary<string, string>> entities)
221 Debug.Assert(metadata != null);
222 Debug.Assert(grid != null);
223 Debug.Assert(entities != null);
224 Debug.Assert(metadata.GridWidth * metadata.GridHeight == grid.Length);
226 mMetadata = metadata;
228 mEntities = entities;
231 Console.WriteLine("Loaded map {0} of type {1} written by {2}.",
239 public Metadata Metadata { get { return mMetadata; } }
240 public Dictionary<char, Dictionary<string, string>> Entities { get { return mEntities; } }
243 public bool IsCellOpen(int x, int y)
245 // TODO: Still need to define characters for types of scenery.
246 return mGrid[x, y] == ' ';
254 public Vector2 CenterCell;
258 public View(Modal data)
260 Debug.Assert(data != null);
264 public void Draw(SpriteBatch spriteBatch)
266 mViewport = spriteBatch.GraphicsDevice.Viewport;
268 // TODO: There is no culling yet, but it runs so fast that it probably won't ever need it.
269 for (int y = 0; y < mData.Metadata.GridHeight; y++)
271 for (int x = 0; x < mData.Metadata.GridWidth; x++)
273 if (mData.IsCellOpen(x, y))
275 spriteBatch.Draw(Map.DefaultTile, GetRectangleFromCoordinates(x, y), Color.White);
279 spriteBatch.Draw(Map.DefaultTile, GetRectangleFromCoordinates(x, y), Color.DarkBlue);
286 /// Get a matrix to transform a point from grid-space to screen coordinates. This
287 /// method uses the viewport to bound the edges of the map such that the camera
288 /// will not show anything outside of the grid.
290 /// <param name="center">The point to put in the center.</param>
291 /// <returns>The transformation matrix.</returns>
292 Matrix GetTransformation(Vector2 center)
294 float halfRatio = PixelsToUnitSquares * 0.5f;
295 Matrix transform = Matrix.CreateTranslation(-center.X, -center.Y, 0.0f);
296 transform *= Matrix.CreateScale(PixelsToUnitSquares);
297 transform *= Matrix.CreateTranslation(mViewport.Width * 0.5f - halfRatio,
298 mViewport.Height * 0.5f - halfRatio, 0.0f);
300 Vector2 topLeft = Vector2.Transform(new Vector2(0.0f, 0.0f), transform);
301 topLeft.X = Math.Max(mViewport.X, topLeft.X);
302 topLeft.Y = Math.Max(mViewport.Y, topLeft.Y);
303 transform *= Matrix.CreateTranslation(-topLeft.X, -topLeft.Y, 0.0f);
305 Vector2 bottomRight = Vector2.Transform(new Vector2((float)mData.Metadata.GridWidth,
306 (float)mData.Metadata.GridHeight), transform);
307 float right = mViewport.X + mViewport.Width;
308 float bottom = mViewport.Y + mViewport.Height;
309 bottomRight.X = Math.Min(right, bottomRight.X) - right;
310 bottomRight.Y = Math.Min(bottom, bottomRight.Y) - bottom;
311 transform *= Matrix.CreateTranslation(-bottomRight.X, -bottomRight.Y, 0.0f);
317 public Point GetPointFromCoordinates(float x, float y)
319 Matrix transform = GetTransformation(CenterCell);
320 Vector2 point = Vector2.Transform(new Vector2(x, y), transform);
322 return new Point((int)point.X, (int)point.Y);
325 public Rectangle GetRectangleFromCoordinates(float x, float y)
327 Matrix transform = GetTransformation(CenterCell);
328 Vector2 point = Vector2.Transform(new Vector2(x, y), transform);
330 return new Rectangle((int)point.X, (int)point.Y, (int)PixelsToUnitSquares, (int)PixelsToUnitSquares);
337 #region Private Variables