From 76dbdc6913f664566f225f753e574c9ef4b9b840 Mon Sep 17 00:00:00 2001 From: Colin McMillen Date: Wed, 29 Jan 2020 18:01:41 -0500 Subject: [PATCH] Bound camera by right edge of World. Fixes #32. GitOrigin-RevId: e193c89509be035d5f6e899cd8c33ff2534a777f --- Shared/Camera.cs | 5 ++++- Shared/SneakGame.cs | 2 +- Shared/World.cs | 22 ++++++++++++++-------- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/Shared/Camera.cs b/Shared/Camera.cs index 92a9ba3..d5d7836 100644 --- a/Shared/Camera.cs +++ b/Shared/Camera.cs @@ -11,7 +11,7 @@ namespace SemiColinGames { public int Height { get => bbox.Height; } public int Left { get => bbox.Left; } - public void Update(Point player) { + public void Update(Point player, int worldWidth) { int diff = player.X - bbox.Center.X; if (Math.Abs(diff) > 16) { bbox.Offset((int) (diff * 0.1), 0); @@ -19,6 +19,9 @@ namespace SemiColinGames { if (bbox.Left < 0) { bbox.Offset(-bbox.Left, 0); } + if (bbox.Right > worldWidth) { + bbox.Offset(worldWidth - bbox.Right, 0); + } Debug.AddToast($"p: {player.X}, {player.Y} c: {bbox.Center.X}"); } } diff --git a/Shared/SneakGame.cs b/Shared/SneakGame.cs index de89bfc..ceea94e 100644 --- a/Shared/SneakGame.cs +++ b/Shared/SneakGame.cs @@ -93,7 +93,7 @@ namespace SemiColinGames { float modelTime = (float) gameTime.ElapsedGameTime.TotalSeconds; Clock.AddModelTime(modelTime); player.Update(modelTime, input, world.CollisionTargets); - camera.Update(player.Position); + camera.Update(player.Position, world.Width); } base.Update(gameTime); diff --git a/Shared/World.cs b/Shared/World.cs index ce948e7..baa65c0 100644 --- a/Shared/World.cs +++ b/Shared/World.cs @@ -85,8 +85,14 @@ namespace SemiColinGames { readonly Tile[] tiles; readonly Aabb[] collisionTargets; - public int Width { get; private set; } - public int Height { get; private set; } + // Size of World in terms of tile grid. + private readonly int tileWidth; + private readonly int tileHeight; + + // Size of World in pixels. + public int Width { + get { return tileWidth * TileSize; } + } readonly string worldString = @" @@ -109,11 +115,11 @@ namespace SemiColinGames { public World(Texture2D texture) { var tilesList = new List(); string[] worldDesc = worldString.Split('\n'); - Width = worldDesc.AsQueryable().Max(a => a.Length); - Height = worldDesc.Length; - Debug.WriteLine("world size: {0}x{1}", Width, Height); - for (int i = 0; i < Width; i++) { - for (int j = 0; j < Height; j++) { + tileWidth = worldDesc.AsQueryable().Max(a => a.Length); + tileHeight = worldDesc.Length; + Debug.WriteLine("world size: {0}x{1}", tileWidth, tileHeight); + for (int i = 0; i < tileWidth; i++) { + for (int j = 0; j < tileHeight; j++) { Terrain terrain = Terrain.Empty; if (i < worldDesc[j].Length) { switch (worldDesc[j][i]) { @@ -172,7 +178,7 @@ namespace SemiColinGames { // Add a final synthetic collisionTarget on the right side of the world. collisionTargets[tiles.Length + 1] = new Aabb( - new Vector2(Width * TileSize + 1, 0), new Vector2(1, float.MaxValue)); + new Vector2(Width + 1, 0), new Vector2(1, float.MaxValue)); } public void Draw(SpriteBatch spriteBatch, Camera camera) {