add a little screen-shake

This commit is contained in:
Colin McMillen 2020-03-10 15:04:41 -04:00
parent 37c679b704
commit 8f79bb8680
3 changed files with 19 additions and 2 deletions

View File

@ -13,11 +13,14 @@ namespace SemiColinGames {
public int Top { get => bbox.Top; } public int Top { get => bbox.Top; }
public Point HalfSize { get => new Point(Width / 2, Height / 2); } public Point HalfSize { get => new Point(Width / 2, Height / 2); }
private float shakeTime = 0.0f;
private Random random = new Random();
public Matrix Projection { public Matrix Projection {
get => Matrix.CreateOrthographicOffCenter(Left, Left + Width, Height, 0, -1, 1); 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; int diff = player.X - bbox.Center.X;
if (Math.Abs(diff) > 16) { if (Math.Abs(diff) > 16) {
bbox.Offset((int) (diff * 0.1), 0); bbox.Offset((int) (diff * 0.1), 0);
@ -28,7 +31,16 @@ namespace SemiColinGames {
if (bbox.Right > worldWidth) { if (bbox.Right > worldWidth) {
bbox.Offset(worldWidth - bbox.Right, 0); 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}"); Debug.AddToast($"p: {player.X}, {player.Y} c: {bbox.Center.X}");
} }
public void Shake() {
shakeTime = 0.5f;
}
} }
} }

View File

@ -150,6 +150,7 @@ namespace SemiColinGames {
} }
if (harmedByCollision && invincibilityTime <= 0) { if (harmedByCollision && invincibilityTime <= 0) {
world.ScreenShake();
Health -= 1; Health -= 1;
invincibilityTime = 0.6f; invincibilityTime = 0.6f;
} }

View File

@ -164,7 +164,11 @@ namespace SemiColinGames {
Reset(); Reset();
} }
LinesOfSight.Update(npcs, CollisionTargets); 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. // Draws everything that's behind the player, from back to front.