Skip to content

Commit

Permalink
Shake camera when collision occurs. Highlight red if damage is taken.
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanlangston committed Jan 3, 2024
1 parent 39f78d7 commit ed11df0
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 11 deletions.
8 changes: 3 additions & 5 deletions src/Camera.zig
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,12 @@ pub const Camera = struct {
};
}

pub fn initScaledTargetCamera(targetScreenSize: raylib.Vector2, scaleFactor: f32, target: raylib.Vector2) Camera {
const screenWidth: f32 = @as(f32, @floatFromInt(raylib.getScreenWidth()));
const screenHeight: f32 = @as(f32, @floatFromInt(raylib.getScreenHeight()));
const zoomScale: f32 = (screenWidth / targetScreenSize.x) * scaleFactor;
pub fn initScaledTargetCamera(targetScreenSize: raylib.Vector2, currentScreenSize: raylib.Vector2, scaleFactor: f32, target: raylib.Vector2) Camera {
const zoomScale: f32 = (currentScreenSize.x / targetScreenSize.x) * scaleFactor;
return Camera{
.camera2D = raylib.Camera2D{
.target = target,
.offset = raylib.Vector2.init(screenWidth / 2, screenHeight / 2),
.offset = raylib.Vector2.init(currentScreenSize.x / 2, currentScreenSize.y / 2),
.zoom = zoomScale,
.rotation = 0,
},
Expand Down
4 changes: 4 additions & 0 deletions src/Models/Player.zig
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ pub const Player = struct {
rotation: f32,
collider: raylib.Vector3,
color: raylib.Color,
frame: f32,
status: PlayerStatusType,

const PLAYER_SPEED: f32 = 5;

Expand All @@ -38,6 +40,8 @@ pub const Player = struct {
0,
0,
),
.frame = 0,
.status = .default,
.acceleration = 0,
.rotation = 0,
.collider = raylib.Vector3.init(
Expand Down
18 changes: 17 additions & 1 deletion src/ViewModels/AsteroidsViewModel.zig
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,22 @@ pub const AsteroidsViewModel = Shared.View.ViewModel.Create(
// Update Player
switch (player.Update(&shoot, &aliens, &alien_shoot, screenSize, halfShipHeight)) {
.collide => {
player.status = .collide;
player.frame = 0;
nextShieldLevel -= 1;
},
.shot => {
player.status = .shot;
nextShieldLevel -= 5;
},
.default => {},
.default => {
if (player.frame > 0.25) {
player.status = .default;
player.frame = 0;
} else if (player.status != .default) {
player.frame += raylib.getFrameTime();
}
},
}

// Update Aliens
Expand Down Expand Up @@ -230,6 +240,8 @@ pub const AsteroidsViewModel = Shared.View.ViewModel.Create(
NewAlien();
},
.collide => {
player.status = .collide;

if (bigMeteors[i].position.x > player.collider.x - halfShipHeight) {
player.rotation = std.math.radiansToDegrees(f32, std.math.pi * 2 - std.math.degreesToRadians(f32, player.rotation));
} else if (bigMeteors[i].position.x < halfShipHeight) {
Expand Down Expand Up @@ -305,6 +317,8 @@ pub const AsteroidsViewModel = Shared.View.ViewModel.Create(
NewAlien();
},
.collide => {
player.status = .collide;

if (mediumMeteors[i].position.x > player.collider.x) {
player.rotation = std.math.radiansToDegrees(f32, std.math.pi * 2 - std.math.degreesToRadians(f32, player.rotation));
} else if (mediumMeteors[i].position.x < player.collider.x) {
Expand Down Expand Up @@ -369,6 +383,8 @@ pub const AsteroidsViewModel = Shared.View.ViewModel.Create(
NewAlien();
},
.collide => {
player.status = .collide;

if (smallMeteors[i].position.x > player.collider.x) {
player.rotation = std.math.radiansToDegrees(f32, std.math.pi * 2 - std.math.degreesToRadians(f32, player.rotation));
} else if (smallMeteors[i].position.x < player.collider.x) {
Expand Down
24 changes: 19 additions & 5 deletions src/Views/AsteroidsView.zig
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,32 @@ fn DrawWithCamera() Shared.View.Views {

vm.Update();

const screenWidth: f32 = @floatFromInt(raylib.getScreenWidth());
const screenHeight: f32 = @floatFromInt(raylib.getScreenHeight());
const screenSize = raylib.Vector2.init(screenWidth, screenHeight);

const shakeAmount = screenWidth / 400;
const target = if (vm.player.status == .collide) raylib.Vector2.init(
vm.player.position.x - (if (Shared.Random.Get().boolean()) shakeAmount else -shakeAmount),
vm.player.position.y - vm.shipHeight - (if (Shared.Random.Get().boolean()) shakeAmount else -shakeAmount),
) else raylib.Vector2.init(
vm.player.position.x,
vm.player.position.y - vm.shipHeight,
);
const camera = Shared.Camera.initScaledTargetCamera(
vm.screenSize,
screenSize,
3.5,
raylib.Vector2.init(
vm.player.position.x,
vm.player.position.y - vm.shipHeight,
),
target,
);
const result = camera.Draw(Shared.View.Views, &DrawFunction);

// Flash screen if player hurt
if (vm.player.status != .default) {
raylib.drawRectangleV(raylib.Vector2.init(0, 0), screenSize, Shared.Color.Red.Base.alpha(0.1));
}

// Draw Health Bar
const screenWidth: f32 = @floatFromInt(raylib.getScreenWidth());
const onePixelScaled: f32 = 0.0025 * screenWidth;
const healthBarWidth = onePixelScaled * 100;
raylib.drawRectangleRounded(
Expand Down

0 comments on commit ed11df0

Please sign in to comment.