Browse Source

add a little screen-shake

master
Colin McMillen 4 years ago
parent
commit
8f79bb8680
  1. 14
      Shared/Camera.cs
  2. 1
      Shared/Player.cs
  3. 6
      Shared/World.cs

14
Shared/Camera.cs

@ -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;
}
} }
} }

1
Shared/Player.cs

@ -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;
} }

6
Shared/World.cs

@ -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.

Loading…
Cancel
Save