# HG changeset patch # User alys # Date 2020-12-12 03:31:10 # Node ID accb2e8529fe9d1ae0852338202dc6b66736f0d0 # Parent 8f8c684c5f35c92fb0b2b6bf6433bf4fb8d76459 Fix debouncing and only apply to certain keys. diff --git a/isometric-park-fna/Camera.cs b/isometric-park-fna/Camera.cs --- a/isometric-park-fna/Camera.cs +++ b/isometric-park-fna/Camera.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; @@ -10,6 +11,7 @@ public Vector2 position; float _zoom; + float[] zoom_levels; public static int world_width { get; set; } @@ -29,6 +31,13 @@ position = Vector2.Zero; } + public Camera(float[] zoomLevels) + { + this.zoom = 1.0f; + position = Vector2.Zero; + this.zoom_levels = zoomLevels; + } + public void Move(Vector2 change) { this.position += change; @@ -56,5 +65,41 @@ { return screenPosition + this.position - Camera.display_offset; } + + public float ZoomIn() + { + float next_level; + + foreach (float level in this.zoom_levels) + { + if(level > this.zoom) + { + next_level = level; + this.zoom = next_level; + return next_level; + } + } + + return this.zoom; + + } + + public float ZoomOut() + { + float next_level; + + foreach (float level in this.zoom_levels.Reverse()) + { + if (level < this.zoom) + { + next_level = level; + this.zoom = next_level; + return next_level; + } + } + + return this.zoom; + + } } } diff --git a/isometric-park-fna/FNAGame.cs b/isometric-park-fna/FNAGame.cs --- a/isometric-park-fna/FNAGame.cs +++ b/isometric-park-fna/FNAGame.cs @@ -23,7 +23,7 @@ private Song music; private SpriteFont font; - private Camera camera = new Camera(); + private Camera camera = new Camera(new float[] {0.25f, 0.5f, 1.0f, 2.0f, 4.0f }); Random random_generator = new Random(); @@ -156,34 +156,33 @@ } - if (keyboardCur.IsKeyDown(Keys.Down) && keyboardPrev.IsKeyUp(Keys.Down)) + if (keyboardCur.IsKeyDown(Keys.Down) ) { this.camera.Move(new Vector2(0, -2)); } - else if (keyboardCur.IsKeyDown(Keys.Up) && keyboardPrev.IsKeyUp(Keys.Up)) + else if (keyboardCur.IsKeyDown(Keys.Up) ) { this.camera.Move(new Vector2(0, 2)); } - else if (keyboardCur.IsKeyDown(Keys.Left) && keyboardPrev.IsKeyUp(Keys.Left)) + else if (keyboardCur.IsKeyDown(Keys.Left) ) { this.camera.Move(new Vector2(-2, 0)); } - else if (keyboardCur.IsKeyDown(Keys.Right) && keyboardPrev.IsKeyUp(Keys.Right)) + else if (keyboardCur.IsKeyDown(Keys.Right) ) { this.camera.Move(new Vector2(2, 0)); } else if (keyboardCur.IsKeyDown(Keys.Subtract) && keyboardPrev.IsKeyUp(Keys.Subtract)) { - this.camera.zoom *= 0.75f; - + this.camera.ZoomOut(); } else if (keyboardCur.IsKeyDown(Keys.Add) && keyboardPrev.IsKeyUp(Keys.Add)) { - this.camera.zoom *= 1.25f; + this.camera.ZoomIn(); } @@ -232,6 +231,8 @@ frameCounter = 0; } + this.keyboardPrev = keyboardCur; + base.Update(gameTime); @@ -364,8 +365,7 @@ drawTileAt(0, 0, 22, 1); - //Warning: creates a flashing effect because tree positions update every 1/60th of a second - /* + //* @@ -373,12 +373,19 @@ { for (int j = 0; j < 50; j += 1) { + //Warning: creates a flashing effect because tree positions update every 1/60th of a second + + /* if (this.random_generator.NextDouble() > 0.75) { drawTileAt(i, j, 142, 2); + }//*/ + + if ((i + j) % 3 == 0) + { + drawTileAt(i, j, 142, 2); } - - + } }//*/