Description:
Add zooming, scrolling, and proper sprite layering (changes from last night).
Commit status:
[Not Reviewed]
References:
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r4:d36395dd0ac1 -

@@ -0,0 +1,60
1 using System;
2 using Microsoft.Xna.Framework;
3 using Microsoft.Xna.Framework.Graphics;
4
5 namespace isometricparkfna
6 {
7 public class Camera
8 {
9
10 public Vector2 position;
11
12 float _zoom;
13
14
15 public static int world_width { get; set; }
16 public static int world_height { get; set; }
17 public static Vector2 display_offset { get; set; }
18
19 public float zoom
20 {
21 get { return _zoom; }
22 set { _zoom = value; if (zoom < 0.1f) _zoom = 0.1f; } // Negative zoom will flip image
23 }
24
25
26 public Camera()
27 {
28 this.zoom = 1.0f;
29 position = Vector2.Zero;
30 }
31
32 public void Move(Vector2 change)
33 {
34 this.position += change;
35 }
36
37 public Matrix get_transformation(GraphicsDevice graphicsDevice)
38 {
39 Viewport viewPort = graphicsDevice.Viewport;
40
41
42 Matrix transformation = Matrix.CreateTranslation(new Vector3(-this.position.X, -this.position.Y, 0)) *
43 Matrix.CreateScale(new Vector3(this.zoom, this.zoom, 1)) *
44 Matrix.CreateTranslation(new Vector3(viewPort.Width * 0.5f, viewPort.Height * 0.5f, 0));
45
46
47 return transformation;
48 }
49
50 public Vector2 world_to_screen(Vector2 worldPosition)
51 {
52 return worldPosition - this.position + Camera.display_offset;
53 }
54
55 public Vector2 screen_to_world(Vector2 screenPosition)
56 {
57 return screenPosition + this.position - Camera.display_offset;
58 }
59 }
60 }
@@ -18,3 +18,4
18 /Users/alys/repos/isometric-park-fna/FNA/obj/Debug/FNA.csproj.CoreCompileInputs.cache
18 /Users/alys/repos/isometric-park-fna/FNA/obj/Debug/FNA.csproj.CoreCompileInputs.cache
19 /Users/alys/repos/isometric-park-fna/FNA/obj/Debug/FNA.dll
19 /Users/alys/repos/isometric-park-fna/FNA/obj/Debug/FNA.dll
20 /Users/alys/repos/isometric-park-fna/FNA/obj/Debug/FNA.pdb
20 /Users/alys/repos/isometric-park-fna/FNA/obj/Debug/FNA.pdb
21 /Users/alys/repos/isometric-park-fna/FNA/obj/Debug/FNA.csprojAssemblyReference.cache
1 NO CONTENT: modified file, binary diff hidden
NO CONTENT: modified file, binary diff hidden
@@ -10,6 +10,7
10 using isometricparkfna;
10 using isometricparkfna;
11 using System.Diagnostics;
11 using System.Diagnostics;
12
12
13
13 struct Entity
14 struct Entity
14 {
15 {
15 public Vector2 loc;
16 public Vector2 loc;
@@ -44,10 +45,10
44 }
45 }
45 }
46 }
46
47
47 static class Camera
48 //static class Camera
48 {
49 //{
49 static public Vector2 Location = Vector2.Zero;
50 // static public Vector2 Location = Vector2.Zero;
50 }
51 //}
51
52
52 class FNAGame : Game
53 class FNAGame : Game
53 {
54 {
@@ -61,6 +62,10
61 private Song music;
62 private Song music;
62 private SpriteFont font;
63 private SpriteFont font;
63
64
65 private Camera camera = new Camera();
66
67 Random random_generator = new Random();
68
64 int frameRate = 0;
69 int frameRate = 0;
65 int frameCounter = 0;
70 int frameCounter = 0;
66 TimeSpan elapsedTime = TimeSpan.Zero;
71 TimeSpan elapsedTime = TimeSpan.Zero;
@@ -87,11 +92,13
87
92
88 //new tile stuff
93 //new tile stuff
89 TileMap myMap = new TileMap();
94 TileMap myMap = new TileMap();
90 int squaresAcross = 250;
95 int squaresAcross = 25;
91 int squaresDown = 250;
96 int squaresDown = 25;
92 int baseOffsetX = -14;
97 int baseOffsetX = -14;
93 int baseOffsetY = -14;
98 int baseOffsetY = -14;
94
99
100 GraphicsDevice device;
101
95
102
96 private static void Main(string[] args)
103 private static void Main(string[] args)
97 {
104 {
@@ -148,6 +155,8
148
155
149 private FNAGame()
156 private FNAGame()
150 {
157 {
158 //this.device = new GraphicsDevice(GraphicsAdapter.DefaultAdapter, GraphicsProfile.)
159
151 GraphicsDeviceManager gdm = new GraphicsDeviceManager(this)
160 GraphicsDeviceManager gdm = new GraphicsDeviceManager(this)
152 {
161 {
153
162
@@ -261,6 +270,37
261 }
270 }
262
271
263
272
273 if (keyboardCur.IsKeyDown(Keys.Down) && keyboardPrev.IsKeyUp(Keys.Down))
274 {
275 this.camera.Move(new Vector2(0, -2));
276 }
277 else if (keyboardCur.IsKeyDown(Keys.Up) && keyboardPrev.IsKeyUp(Keys.Up))
278 {
279 this.camera.Move(new Vector2(0, 2));
280
281 }
282 else if (keyboardCur.IsKeyDown(Keys.Left) && keyboardPrev.IsKeyUp(Keys.Left))
283 {
284 this.camera.Move(new Vector2(-2, 0));
285
286 }
287 else if (keyboardCur.IsKeyDown(Keys.Right) && keyboardPrev.IsKeyUp(Keys.Right))
288 {
289 this.camera.Move(new Vector2(2, 0));
290
291 }
292 else if (keyboardCur.IsKeyDown(Keys.Subtract) && keyboardPrev.IsKeyUp(Keys.Subtract))
293 {
294 this.camera.zoom -= 0.25f;
295
296 }
297 else if (keyboardCur.IsKeyDown(Keys.Add) && keyboardPrev.IsKeyUp(Keys.Add))
298 {
299 this.camera.zoom += 0.25f;
300
301 }
302
303
264 if (keyboardCur.IsKeyDown(Keys.Space) && keyboardPrev.IsKeyUp(Keys.Space))
304 if (keyboardCur.IsKeyDown(Keys.Space) && keyboardPrev.IsKeyUp(Keys.Space))
265 {
305 {
266 sound.Play(volume, pitch, pan);
306 sound.Play(volume, pitch, pan);
@@ -295,20 +335,36
295
335
296 protected void drawTileAt(int x, int y, int tileIndex, int height)
336 protected void drawTileAt(int x, int y, int tileIndex, int height)
297 {
337 {
298 Vector2 squareOffset = new Vector2(Camera.Location.X % 32,
338 Vector2 firstSquare = Vector2.Zero;
299 Camera.Location.Y % 32);
339 Vector2 squareOffset = Vector2.Zero;
300
340
301 int offsetX = (int)squareOffset.X;
341 int offsetX = (int)squareOffset.X;
302 int offsetY = (int)squareOffset.Y;
342 int offsetY = (int)squareOffset.Y;
343 int firstX = (int)firstSquare.X;
344 int firstY = (int)firstSquare.Y;
345 int rowOffset = 0;
346 float maxdepth = ((this.squaresAcross + 1) + ((this.squaresDown + 1) * Tile.TileWidth)) * 10;
347
348
349 int mapx = (firstX + x);
350 int mapy = (firstY + y);
351 float depthOffset = 0.7f - ((mapx + (mapy * Tile.TileWidth)) / maxdepth);
352
353 if ((firstY + y) % 2 == 1)
354 rowOffset = Tile.OddRowXOffset;
303
355
304 batch.Draw(
356 batch.Draw(
305 Tile.TileSetTexture,
357 Tile.TileSetTexture,
306 new Rectangle(
358 new Rectangle(
307 (x * Tile.TileStepX) - offsetX + 0 + baseOffsetX,
359 (x * Tile.TileStepX) - offsetX + rowOffset + baseOffsetX,
308 (y * Tile.TileStepY) - offsetY + baseOffsetY,
360 (y * Tile.TileStepY) - offsetY + baseOffsetY,
309 Tile.TileWidth, Tile.TileHeight*height),
361 Tile.TileWidth, Tile.TileHeight*height),
310 Tile.GetExtendedSourceRectangle(tileIndex, height),
362 Tile.GetExtendedSourceRectangle(tileIndex, height),
311 Color.White);
363 Color.White,
364 0.0f,
365 Vector2.Zero,
366 SpriteEffects.None,
367 depthOffset);
312 }
368 }
313
369
314 protected override void Draw(GameTime gameTime)
370 protected override void Draw(GameTime gameTime)
@@ -324,16 +380,20
324 Stopwatch stopWatch = new Stopwatch();
380 Stopwatch stopWatch = new Stopwatch();
325 stopWatch.Start();
381 stopWatch.Start();
326 GraphicsDevice.Clear(Color.CornflowerBlue);
382 GraphicsDevice.Clear(Color.CornflowerBlue);
327 batch.Begin();
383 batch.Begin(SpriteSortMode.BackToFront,
384 BlendState.AlphaBlend,
385 null,
386 null,
387 null,
388 null,
389 camera.get_transformation(GraphicsDevice));
328
390
329 //New tile stuff
391 //New tile stuff
330 Vector2 firstSquare = new Vector2(Camera.Location.X / 32,
392 Vector2 firstSquare = Vector2.Zero;
331 Camera.Location.Y / 32);
332 int firstX = (int)firstSquare.X;
393 int firstX = (int)firstSquare.X;
333 int firstY = (int)firstSquare.Y;
394 int firstY = (int)firstSquare.Y;
334
395
335 Vector2 squareOffset = new Vector2(Camera.Location.X % 32,
396 Vector2 squareOffset = Vector2.Zero;
336 Camera.Location.Y % 32);
337
397
338 int offsetX = (int)squareOffset.X;
398 int offsetX = (int)squareOffset.X;
339 int offsetY = (int)squareOffset.Y;
399 int offsetY = (int)squareOffset.Y;
@@ -344,16 +404,21
344 if ((firstY + y) % 2 == 1)
404 if ((firstY + y) % 2 == 1)
345 rowOffset = Tile.OddRowXOffset;
405 rowOffset = Tile.OddRowXOffset;
346
406
347 for (int x = 0; x < this.squaresAcross; x++)
407 for (int x = 0; x < this.squaresAcross; x++) {
348 {
408
409
349 batch.Draw(
410 batch.Draw(
350 Tile.TileSetTexture,
411 Tile.TileSetTexture,
351 new Rectangle(
412 new Rectangle(
352 (x * Tile.TileStepX) - offsetX + rowOffset + baseOffsetX,
413 (x * Tile.TileStepX) - offsetX + rowOffset + baseOffsetX,
353 (y * Tile.TileStepY) - offsetY + baseOffsetY,
414 (y * Tile.TileStepY) - offsetY + baseOffsetY,
354 Tile.TileWidth, Tile.TileHeight),
415 Tile.TileWidth, Tile.TileHeight),
355 Tile.GetSourceRectangle(1),
416 Tile.GetSourceRectangle(1),
356 Color.White);
417 Color.White,
418 0.0f,
419 Vector2.Zero,
420 SpriteEffects.None,
421 0.9f);
357 }
422 }
358 }
423 }
359
424
@@ -371,7 +436,7
371 Vector2 stop = new Vector2(startX + this.squaresAcross* Tile.TileStepX/2,
436 Vector2 stop = new Vector2(startX + this.squaresAcross* Tile.TileStepX/2,
372 this.squaresDown*Tile.TileStepY- baseOffsetY+4);
437 this.squaresDown*Tile.TileStepY- baseOffsetY+4);
373
438
374 Line.drawLine(batch, start, stop, Color.White);
439 Line.drawLine(batch, start, stop, Color.White, 0.8f);
375
440
376 }
441 }
377 //Lines going down and to the left:
442 //Lines going down and to the left:
@@ -384,34 +449,57
384 Vector2 stop_reverse = new Vector2(startX + -(this.squaresAcross * Tile.TileStepX / 2),
449 Vector2 stop_reverse = new Vector2(startX + -(this.squaresAcross * Tile.TileStepX / 2),
385 (this.squaresDown * Tile.TileStepY) - baseOffsetY + 4);
450 (this.squaresDown * Tile.TileStepY) - baseOffsetY + 4);
386
451
387 Line.drawLine(batch, start_reverse, stop_reverse, Color.White);
452 Line.drawLine(batch, start_reverse, stop_reverse, Color.White, 0.8f);
388
453
389 }
454 }
390
455
391 //int startpos = 140;
392 //for (int i = startpos; i < startpos + 4; i++)
393 //{
394 // batch.Draw(
395 // Tile.TileSetTexture,
396 // new Rectangle(
397 // ((i - startpos+2) * 2 * Tile.TileStepX) - offsetX + 0 + baseOffsetX,
398 // (4 * Tile.TileStepY) - offsetY + baseOffsetY,
399 // Tile.TileWidth, Tile.TileHeight),
400 // Tile.GetExtendedSourceRectangle(i, 2),
401 // Color.White);
402 //}
403
404 drawTileAt(4, 4, 140, 3);
456 drawTileAt(4, 4, 140, 3);
405 drawTileAt(6, 4, 141, 3);
457 drawTileAt(6, 4, 141, 3);
406 drawTileAt(8, 4, 142, 2);
458 drawTileAt(8, 4, 142, 2);
407 drawTileAt(10, 4, 142, 3);
459 drawTileAt(10, 4, 142, 3);
460 drawTileAt(0, 0, 22, 1);
408
461
409 batch.DrawString(font, fps, new Vector2(33, 33), Color.Black);
462
410 batch.DrawString(font, fps, new Vector2(32, 32), Color.White);
463 //Warning: creates a flashing effect because tree positions update every 1/60th of a second
411 batch.DrawString(font, this.drawTime.TotalMilliseconds.ToString(), new Vector2(120, 33), Color.Black);
464 //*
412 batch.DrawString(font, this.drawTime.TotalMilliseconds.ToString(), new Vector2(119, 32), Color.White);
465
466
467
468 for (int i = 0; i< 80; i++)
469 {
470 for (int j = 0; j < 50; j += 1)
471 {
472 if (this.random_generator.NextDouble() > 0.75)
473 {
474 drawTileAt(i, j, 142, 2);
475 }
476
477
478 }
479 }//*/
480
413
481
414 batch.End();
482 batch.End();
483
484 batch.Begin(SpriteSortMode.BackToFront,
485 BlendState.AlphaBlend,
486 null,
487 null,
488 null,
489 null);
490
491 batch.DrawString(font, fps, new Vector2(33, 33), Color.Black, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 0.5f);
492 batch.DrawString(font, fps, new Vector2(32, 32), Color.White, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 0.49f);
493 batch.DrawString(font, this.drawTime.TotalMilliseconds.ToString(), new Vector2(120, 33), Color.Black, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 0.5f);
494 batch.DrawString(font, this.drawTime.TotalMilliseconds.ToString(), new Vector2(119, 32), Color.White, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 0.49f);
495
496
497 batch.DrawString(font, camera.position.ToString(), new Vector2(190, 33), Color.Black, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 0.5f);
498 batch.DrawString(font, camera.position.ToString(), new Vector2(189, 32), Color.White, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 0.49f);
499
500 batch.End();
501
502
415 stopWatch.Stop();
503 stopWatch.Stop();
416 this.drawTime = stopWatch.Elapsed;
504 this.drawTime = stopWatch.Elapsed;
417
505
@@ -32,6 +32,7
32 <Compile Include="Tile.cs" />
32 <Compile Include="Tile.cs" />
33 <Compile Include="TileMap.cs" />
33 <Compile Include="TileMap.cs" />
34 <Compile Include="Line.cs" />
34 <Compile Include="Line.cs" />
35 <Compile Include="Camera.cs" />
35 </ItemGroup>
36 </ItemGroup>
36 <ItemGroup>
37 <ItemGroup>
37 <ProjectReference Include="..\FNA\FNA.csproj">
38 <ProjectReference Include="..\FNA\FNA.csproj">
You need to be logged in to leave comments. Login now