From e0d53853502f5c56eccf8511370e4d9efba2be86 Mon Sep 17 00:00:00 2001 From: Colin McMillen Date: Tue, 24 Mar 2020 14:41:07 -0400 Subject: [PATCH] camera now tracks player y-position too --- Shared/Camera.cs | 17 +++++++++++------ Shared/Player.cs | 9 ++++++--- Shared/World.cs | 2 +- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/Shared/Camera.cs b/Shared/Camera.cs index b6171fa..4ed5bb9 100644 --- a/Shared/Camera.cs +++ b/Shared/Camera.cs @@ -18,13 +18,18 @@ namespace SemiColinGames { public Matrix Projection { - get => Matrix.CreateOrthographicOffCenter(Left, Left + Width, Height, 0, -1, 1); + get => Matrix.CreateOrthographicOffCenter(Left, Left + Width, Height + Top, Top, -1, 1); } - public void Update(float modelTime, Vector2 player, int worldWidth) { - float diff = player.X - bbox.Center.X; - if (Math.Abs(diff) > 16) { - bbox.Offset((int) (diff * 0.1), 0); + public void Update(float modelTime, Player player, int worldWidth) { + Vector2 pos = player.Position; + float xDiff = pos.X - bbox.Center.X; + float yDiff = pos.Y - bbox.Center.Y; + if (Math.Abs(xDiff) > 16) { + bbox.Offset((int) (xDiff * 0.1), 0); + } + if (Math.Abs(yDiff) > 16 && player.StandingOnGround) { + bbox.Offset(0, (int) (yDiff * 0.1)); } if (bbox.Left < 0) { bbox.Offset(-bbox.Left, 0); @@ -37,7 +42,7 @@ namespace SemiColinGames { int x = random.Next(-4, 5); bbox.Offset(x, 0); } - Debug.AddToast($"p: {player.X}, {player.Y} c: {bbox.Center.X}"); + Debug.AddToast($"p: {pos.X}, {pos.Y} c: {bbox.Center.X}"); } public void Shake() { diff --git a/Shared/Player.cs b/Shared/Player.cs index d6417c6..985624d 100644 --- a/Shared/Player.cs +++ b/Shared/Player.cs @@ -44,8 +44,11 @@ namespace SemiColinGames { this.position = position; Facing = facing; Health = MaxHealth; + StandingOnGround = false; } + public bool StandingOnGround { get; private set; } + public int MaxHealth { get; private set; } = 3; public int Health { get; private set; } @@ -129,12 +132,12 @@ namespace SemiColinGames { } } - bool standingOnGround = false; + StandingOnGround = false; AABB groundIntersect = BoxOffset(position, 1); foreach (var box in candidates) { if (groundIntersect.Intersect(box) != null) { Debug.AddRect(box, Color.Cyan); - standingOnGround = true; + StandingOnGround = true; if (box.Tile?.IsHazard ?? false) { Debug.AddRect(box, Color.Red); harmedByCollision = true; @@ -142,7 +145,7 @@ namespace SemiColinGames { } } - if (standingOnGround) { + if (StandingOnGround) { jumps = 1; ySpeed = -0.0001f; Debug.AddRect(Box(position), Color.Cyan); diff --git a/Shared/World.cs b/Shared/World.cs index e6f53b7..4c73071 100644 --- a/Shared/World.cs +++ b/Shared/World.cs @@ -162,7 +162,7 @@ namespace SemiColinGames { Reset(); } LinesOfSight.Update(npcs, CollisionTargets); - Camera.Update(modelTime, Player.Position, Width); + Camera.Update(modelTime, Player, Width); } public void ScreenShake() {