From 8f79bb86808c9376623201c0e68f472be9bb9325 Mon Sep 17 00:00:00 2001 From: Colin McMillen Date: Tue, 10 Mar 2020 15:04:41 -0400 Subject: [PATCH] add a little screen-shake --- Shared/Camera.cs | 14 +++++++++++++- Shared/Player.cs | 1 + Shared/World.cs | 6 +++++- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/Shared/Camera.cs b/Shared/Camera.cs index 839cb42..7b76727 100644 --- a/Shared/Camera.cs +++ b/Shared/Camera.cs @@ -13,11 +13,14 @@ namespace SemiColinGames { public int Top { get => bbox.Top; } public Point HalfSize { get => new Point(Width / 2, Height / 2); } + private float shakeTime = 0.0f; + private Random random = new Random(); + public Matrix Projection { get => Matrix.CreateOrthographicOffCenter(Left, Left + Width, Height, 0, -1, 1); } - public void Update(Point player, int worldWidth) { + public void Update(float modelTime, Point player, int worldWidth) { int diff = player.X - bbox.Center.X; if (Math.Abs(diff) > 16) { bbox.Offset((int) (diff * 0.1), 0); @@ -28,7 +31,16 @@ namespace SemiColinGames { if (bbox.Right > worldWidth) { bbox.Offset(worldWidth - bbox.Right, 0); } + if (shakeTime > 0) { + shakeTime -= modelTime; + int x = random.Next(-4, 5); + bbox.Offset(x, 0); + } Debug.AddToast($"p: {player.X}, {player.Y} c: {bbox.Center.X}"); } + + public void Shake() { + shakeTime = 0.5f; + } } } diff --git a/Shared/Player.cs b/Shared/Player.cs index 007f661..3cce7b9 100644 --- a/Shared/Player.cs +++ b/Shared/Player.cs @@ -150,6 +150,7 @@ namespace SemiColinGames { } if (harmedByCollision && invincibilityTime <= 0) { + world.ScreenShake(); Health -= 1; invincibilityTime = 0.6f; } diff --git a/Shared/World.cs b/Shared/World.cs index 9c15b8f..ce31d7b 100644 --- a/Shared/World.cs +++ b/Shared/World.cs @@ -164,7 +164,11 @@ namespace SemiColinGames { Reset(); } LinesOfSight.Update(npcs, CollisionTargets); - Camera.Update(Player.Position, Width); + Camera.Update(modelTime, Player.Position, Width); + } + + public void ScreenShake() { + Camera.Shake(); } // Draws everything that's behind the player, from back to front.