]> Dogcows Code - chaz/carfire/blob - CarFire/CarFire/CarFire/Map.cs
Map drawing implemented.
[chaz/carfire] / CarFire / CarFire / CarFire / Map.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.IO;
6 using System.Runtime.Serialization;
7 using System.Diagnostics;
8 using Microsoft.Xna.Framework;
9 using Microsoft.Xna.Framework.Graphics;
10
11 namespace CarFire
12 {
13 /// <summary>
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.
18 /// </summary>
19 public class Map
20 {
21 // DEBUG: Tilesets not implemented at all.
22 public static Texture2D DefaultTile;
23
24 #region Public Constants
25
26 public const float PixelsToUnitSquares = 64.0f;
27
28 #endregion
29
30
31 #region Public Types
32
33 /// <summary>
34 /// The type of a map helps determine how the map is intended to be used.
35 /// </summary>
36 public enum Type
37 {
38 None,
39 Campaign,
40 Battle
41 }
42
43 /// <summary>
44 /// The container class for map metadata.
45 /// </summary>
46 public class Metadata
47 {
48 public string Name;
49 public Type Type;
50 public string Author;
51 public HashSet<int> NumPlayers = new HashSet<int>();
52 public string Tileset;
53 public int GridWidth;
54 public int GridHeight;
55 }
56
57 #endregion
58
59
60 #region Public Attributes
61
62 /// <summary>
63 /// Get the name of the map.
64 /// </summary>
65 public string Name { get { return mData.Metadata.Name; } }
66
67 /// <summary>
68 /// Get the type of the map.
69 /// </summary>
70 //public Type Type { get { return mData.mMetadata.Type; } }
71
72 /// <summary>
73 /// Get the author of the map.
74 /// </summary>
75 public string Author { get { return mData.Metadata.Author; } }
76
77 /// <summary>
78 /// Get a set of integers containing each allowable number of players.
79 /// </summary>
80 public HashSet<int> NumPlayers { get { return mData.Metadata.NumPlayers; } }
81
82 /// <summary>
83 /// Get the width of the map, in grid units.
84 /// </summary>
85 public int Width { get { return mData.Metadata.GridWidth; } }
86
87 /// <summary>
88 /// Get the height of the map, in grid units.
89 /// </summary>
90 public int Height { get { return mData.Metadata.GridHeight; } }
91
92 // TODO: This should return whatever object we end up using for tilesets.
93 public string Tileset { get { return mData.Metadata.Tileset; } }
94
95
96 public Vector2 CenterCell
97 {
98 get { return mView.CenterCell; }
99 set { mView.CenterCell = value; }
100 }
101
102 #endregion
103
104
105 #region Public Methods
106
107 /// <summary>
108 /// Construct a map with the provided map data.
109 /// </summary>
110 /// <param name="data">Map data.</param>
111 public Map(Metadata metadata, char[,] grid, Dictionary<char, Dictionary<string, string>> entities)
112 {
113 mData = new Modal(metadata, grid, entities);
114 mView = new View(mData);
115 }
116
117
118 /// <summary>
119 /// Draw a representation of the map to the screen.
120 /// </summary>
121 /// <param name="spriteBatch">The jeewiz.</param>
122 public void Draw(SpriteBatch spriteBatch)
123 {
124 mView.Draw(spriteBatch);
125 }
126
127
128 /// <summary>
129 /// Get a point in screen-space from a coordinate in gridspace.
130 /// </summary>
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)
135 {
136 return mView.GetPointFromCoordinates(x, y);
137 }
138
139 /// <summary>
140 /// Get a point in screen-space from a coordinate in gridspace.
141 /// </summary>
142 /// <param name="point">X,Y-coordinates.</param>
143 /// <returns>Transformed point.</returns>
144 public Point GetPointFromCoordinates(Vector2 point)
145 {
146 return mView.GetPointFromCoordinates(point.X, point.Y);
147 }
148
149 /// <summary>
150 /// Get a rectangle in screen-space centered around a coordinate in gridspace.
151 /// </summary>
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)
156 {
157 return mView.GetRectangleFromCoordinates(x, y);
158 }
159
160 /// <summary>
161 /// Get a rectangle in screen-space centered around a coordinate in gridspace.
162 /// </summary>
163 /// <param name="point">X,Y-coordinates.</param>
164 /// <returns>Transformed rectangle.</returns>
165 public Rectangle GetRectangleFromCoordinates(Vector2 point)
166 {
167 return mView.GetRectangleFromCoordinates(point.X, point.Y);
168 }
169
170
171 /// <summary>
172 /// Determine whether or not a cell can be occupied by a game entity.
173 /// </summary>
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)
178 {
179 return mData.IsCellOpen(x, y);
180 }
181
182 /// <summary>
183 /// Determine whether or not a cell can be occupied by a game entity.
184 /// </summary>
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)
188 {
189 return mData.IsCellOpen(point.X, point.Y);
190 }
191
192
193 /// <summary>
194 /// Get the entities loaded from the map file.
195 /// </summary>
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()
200 {
201 return mData.Entities;
202 }
203
204 #endregion
205
206
207 #region Private Types
208
209 class Modal
210 {
211 Metadata mMetadata;
212 char[,] mGrid;
213 Dictionary<char, Dictionary<string, string>> mEntities;
214
215 public Modal(Metadata metadata, char[,] grid, Dictionary<char, Dictionary<string, string>> entities)
216 {
217 Debug.Assert(metadata != null);
218 Debug.Assert(grid != null);
219 Debug.Assert(entities != null);
220 Debug.Assert(metadata.GridWidth * metadata.GridHeight == grid.Length);
221
222 mMetadata = metadata;
223 mGrid = grid;
224 mEntities = entities;
225
226 #if DEBUG
227 Console.WriteLine("Loaded map {0} of type {1} written by {2}.",
228 metadata.Name,
229 metadata.Type,
230 metadata.Author);
231 #endif
232 }
233
234
235 public Metadata Metadata { get { return mMetadata; } }
236 public Dictionary<char, Dictionary<string, string>> Entities { get { return mEntities; } }
237
238
239 public bool IsCellOpen(int x, int y)
240 {
241 // TODO: Still need to define characters for types of scenery.
242 return mGrid[x, y] == ' ';
243 }
244 }
245
246 class View
247 {
248 Modal mData;
249
250 public Vector2 CenterCell;
251 Viewport mViewport;
252
253
254 public View(Modal data)
255 {
256 Debug.Assert(data != null);
257 mData = data;
258 }
259
260 public void Draw(SpriteBatch spriteBatch)
261 {
262 mViewport = spriteBatch.GraphicsDevice.Viewport;
263
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++)
266 {
267 for (int x = 0; x < mData.Metadata.GridWidth; x++)
268 {
269 if (mData.IsCellOpen(x, y))
270 {
271 spriteBatch.Draw(Map.DefaultTile, GetRectangleFromCoordinates(x, y), Color.White);
272 }
273 else
274 {
275 spriteBatch.Draw(Map.DefaultTile, GetRectangleFromCoordinates(x, y), Color.DarkBlue);
276 }
277 }
278 }
279 }
280
281 /// <summary>
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.
285 /// </summary>
286 /// <param name="center">The point to put in the center.</param>
287 /// <returns>The transformation matrix.</returns>
288 Matrix GetTransformation(Vector2 center)
289 {
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);
295
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);
300
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);
308
309 return transform;
310 }
311
312
313 public Point GetPointFromCoordinates(float x, float y)
314 {
315 Matrix transform = GetTransformation(CenterCell);
316 Vector2 point = Vector2.Transform(new Vector2(x, y), transform);
317
318 return new Point((int)point.X, (int)point.Y);
319 }
320
321 public Rectangle GetRectangleFromCoordinates(float x, float y)
322 {
323 Matrix transform = GetTransformation(CenterCell);
324 Vector2 point = Vector2.Transform(new Vector2(x, y), transform);
325
326 return new Rectangle((int)point.X, (int)point.Y, (int)PixelsToUnitSquares, (int)PixelsToUnitSquares);
327 }
328 }
329
330 #endregion
331
332
333 #region Private Variables
334
335 Modal mData;
336 View mView;
337
338 #endregion
339 }
340 }
This page took 0.052498 seconds and 5 git commands to generate.