Skip to content

Commit

Permalink
Animate Health Bar
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanlangston committed Dec 28, 2023
1 parent 493258f commit c666102
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/ViewModels/AsteroidsViewModel.zig
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub const AsteroidsViewModel = Shared.View.ViewModel.Create(

// Variables
pub var shieldLevel: u8 = MAX_SHIELD;
var nextShieldLevel: u8 = MAX_SHIELD;
pub var victory = false;

pub const screenSize: raylib.Vector2 = raylib.Vector2.init(3200, 1800);
Expand Down Expand Up @@ -60,6 +61,7 @@ pub const AsteroidsViewModel = Shared.View.ViewModel.Create(
var vely: f32 = undefined;
victory = false;
shieldLevel = MAX_SHIELD;
nextShieldLevel = MAX_SHIELD;

shipHeight = (PLAYER_BASE_SIZE / 2) / @tan(std.math.degreesToRadians(
f32,
Expand Down Expand Up @@ -267,13 +269,18 @@ pub const AsteroidsViewModel = Shared.View.ViewModel.Create(
// Update game (one frame)
pub inline fn Update() void {

// Update Shield Level
if (nextShieldLevel != shieldLevel) {
shieldLevel = @max(nextShieldLevel, @as(u8, @intFromFloat(@as(f32, @floatFromInt(shieldLevel)) - raylib.getFrameTime())));
}

// Update Player
switch (player.Update(&shoot, &alien_shoot, screenSize, halfShipHeight)) {
.collide => {
shieldLevel = 0;
nextShieldLevel = 0;
},
.shot => {
shieldLevel -= 5;
nextShieldLevel -= 5;
},
.default => {},
}
Expand All @@ -282,7 +289,7 @@ pub const AsteroidsViewModel = Shared.View.ViewModel.Create(
inline for (0..MAX_ALIENS) |i| {
switch (aliens[i].Update(player, &shoot, &alien_shoot, screenSize)) {
.collide => {
shieldLevel = 0;
nextShieldLevel = 0;
},
.shot => {},
.default => {},
Expand Down Expand Up @@ -332,7 +339,7 @@ pub const AsteroidsViewModel = Shared.View.ViewModel.Create(
}
},
.collide => {
shieldLevel = 0;
nextShieldLevel = 0;
},
}
}
Expand Down Expand Up @@ -367,7 +374,7 @@ pub const AsteroidsViewModel = Shared.View.ViewModel.Create(
}
},
.collide => {
shieldLevel = 0;
nextShieldLevel = 0;
},
}
}
Expand All @@ -379,7 +386,7 @@ pub const AsteroidsViewModel = Shared.View.ViewModel.Create(
destroyedMeteorsCount += 1;
},
.collide => {
shieldLevel = 0;
nextShieldLevel = 0;
},
}
}
Expand Down

0 comments on commit c666102

Please sign in to comment.