Skip to content

Commit

Permalink
Score Counter
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanlangston committed Dec 28, 2023
1 parent c666102 commit 417e02c
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 26 deletions.
44 changes: 43 additions & 1 deletion src/Helpers.zig
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,54 @@ pub const Helpers = struct {
screenWidth: f32,
positionY: f32,
) void {
const TitleTextSize = raylib.measureText(text, fontSize);
const TitleTextSize = raylib.measureText(text, @intFromFloat(fontSize));
raylib.drawText(
text,
@divFloor((@as(i32, @intFromFloat(screenWidth)) - TitleTextSize), 2),
@intFromFloat(positionY),
@intFromFloat(fontSize),
color,
);
}
pub inline fn DrawTextWithFontRightAligned(
text: [:0]const u8,
color: raylib.Color,
font: raylib.Font,
fontSize: f32,
screenWidth: f32,
positionY: f32,
) void {
const TitleTextSize = raylib.measureTextEx(
font,
text,
fontSize,
@floatFromInt(font.glyphPadding),
);
raylib.drawTextEx(
font,
text,
raylib.Vector2.init(
screenWidth - TitleTextSize.x,
positionY,
),
TitleTextSize.y,
@floatFromInt(font.glyphPadding),
color,
);
}
pub inline fn DrawTextRightAligned(
text: [:0]const u8,
color: raylib.Color,
fontSize: f32,
screenWidth: f32,
positionY: f32,
) void {
const TitleTextSize = raylib.measureText(text, @intFromFloat(fontSize));
raylib.drawText(
text,
@as(i32, @intFromFloat(screenWidth)) - TitleTextSize,
@intFromFloat(positionY),
@intFromFloat(fontSize),
color,
);
}
Expand Down
37 changes: 18 additions & 19 deletions src/ViewModels/AsteroidsViewModel.zig
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ 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 All @@ -45,9 +44,10 @@ pub const AsteroidsViewModel = Shared.View.ViewModel.Create(
pub var aliens: [MAX_ALIENS]Alien = undefined;
pub var alien_shoot: [ALIENS_MAX_SHOOTS]Shoot = undefined;

var midMeteorsCount: i32 = 0;
var smallMeteorsCount: i32 = 0;
var destroyedMeteorsCount: i32 = 0;
var midMeteorsCount: u8 = 0;
var smallMeteorsCount: u16 = 0;

pub var score: u64 = 0;

pub var starScape: Starscape = undefined;

Expand All @@ -59,7 +59,6 @@ pub const AsteroidsViewModel = Shared.View.ViewModel.Create(
var posy: f32 = undefined;
var velx: f32 = undefined;
var vely: f32 = undefined;
victory = false;
shieldLevel = MAX_SHIELD;
nextShieldLevel = MAX_SHIELD;

Expand Down Expand Up @@ -88,7 +87,7 @@ pub const AsteroidsViewModel = Shared.View.ViewModel.Create(
.color = Shared.Color.Gray.Light,
};

destroyedMeteorsCount = 0;
score = 0;

// Initialization shoot
for (0..PLAYER_MAX_SHOOTS) |i| {
Expand Down Expand Up @@ -272,12 +271,14 @@ pub const AsteroidsViewModel = Shared.View.ViewModel.Create(
// Update Shield Level
if (nextShieldLevel != shieldLevel) {
shieldLevel = @max(nextShieldLevel, @as(u8, @intFromFloat(@as(f32, @floatFromInt(shieldLevel)) - raylib.getFrameTime())));
} else if (shieldLevel > MAX_SHIELD) {
shieldLevel = 0;
}

// Update Player
switch (player.Update(&shoot, &alien_shoot, screenSize, halfShipHeight)) {
.collide => {
nextShieldLevel = 0;
shieldLevel = 0;
},
.shot => {
nextShieldLevel -= 5;
Expand All @@ -289,9 +290,11 @@ pub const AsteroidsViewModel = Shared.View.ViewModel.Create(
inline for (0..MAX_ALIENS) |i| {
switch (aliens[i].Update(player, &shoot, &alien_shoot, screenSize)) {
.collide => {
nextShieldLevel = 0;
shieldLevel = 0;
},
.shot => {
score += 8;
},
.shot => {},
.default => {},
}
}
Expand All @@ -314,7 +317,7 @@ pub const AsteroidsViewModel = Shared.View.ViewModel.Create(
switch (bigMeteors[i].Update(player, &shoot, &aliens, &alien_shoot, screenSize)) {
.default => {},
.shot => |shot| {
destroyedMeteorsCount += 1;
score += 4;

for (0..2) |_| {
mediumMeteors[@intCast(midMeteorsCount)].position = raylib.Vector2.init(
Expand All @@ -339,7 +342,7 @@ pub const AsteroidsViewModel = Shared.View.ViewModel.Create(
}
},
.collide => {
nextShieldLevel = 0;
shieldLevel = 0;
},
}
}
Expand All @@ -349,7 +352,7 @@ pub const AsteroidsViewModel = Shared.View.ViewModel.Create(
switch (mediumMeteors[i].Update(player, &shoot, &aliens, &alien_shoot, screenSize)) {
.default => {},
.shot => |shot| {
destroyedMeteorsCount += 1;
score += 2;

for (0..2) |_| {
smallMeteors[@intCast(smallMeteorsCount)].position = raylib.Vector2.init(
Expand All @@ -374,7 +377,7 @@ pub const AsteroidsViewModel = Shared.View.ViewModel.Create(
}
},
.collide => {
nextShieldLevel = 0;
shieldLevel = 0;
},
}
}
Expand All @@ -383,17 +386,13 @@ pub const AsteroidsViewModel = Shared.View.ViewModel.Create(
switch (smallMeteors[i].Update(player, &shoot, &aliens, &alien_shoot, screenSize)) {
.default => {},
.shot => {
destroyedMeteorsCount += 1;
score += 1;
},
.collide => {
nextShieldLevel = 0;
shieldLevel = 0;
},
}
}

if (destroyedMeteorsCount == MAX_BIG_METEORS + MAX_MEDIUM_METEORS + MAX_SMALL_METEORS) {
victory = true;
}
}
},
.{
Expand Down
13 changes: 7 additions & 6 deletions src/Views/AsteroidsView.zig
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ fn DrawWithCamera() Shared.View.Views {
const result = camera.Draw(Shared.View.Views, &DrawFunction);

// Draw Health Bar
const onePixelScaled: f32 = 0.0025 * @as(f32, @floatFromInt(raylib.getScreenWidth()));
const screenWidth: f32 = @floatFromInt(raylib.getScreenWidth());
const onePixelScaled: f32 = 0.0025 * screenWidth;
const healthBarWidth = onePixelScaled * 100;
raylib.drawRectangleRounded(
raylib.Rectangle.init(
Expand Down Expand Up @@ -116,12 +117,12 @@ fn DrawWithCamera() Shared.View.Views {
Shared.Color.Gray.Dark,
);

if (vm.victory) Shared.Helpers.DrawTextCentered(
"VICTORY",
Shared.Helpers.DrawTextRightAligned(
std.fmt.allocPrintZ(Shared.GetAllocator(), "Score: {}", .{vm.score}) catch "Score Unknown!",
Shared.Color.Blue.Light,
40,
vm.screenSize.x,
vm.screenSize.y / 2,
onePixelScaled * 10,
screenWidth - (5 * onePixelScaled),
5,
);

if (Shared.Input.Start_Pressed()) {
Expand Down

0 comments on commit 417e02c

Please sign in to comment.