Added percentage of stars collected in level + mini pie chart.

This commit is contained in:
Sander Schobers 2023-06-05 04:47:44 +02:00
parent 7f2719df43
commit bbd8a56209
7 changed files with 43 additions and 11 deletions

View File

@ -53,6 +53,14 @@ pub const Bitmap = struct {
drawTintedScaledBitmap(self, tint, 0, 0, @intToFloat(f32, self.width()), @intToFloat(f32, self.height()), x, y, w, h, DrawFlags{}); drawTintedScaledBitmap(self, tint, 0, 0, @intToFloat(f32, self.width()), @intToFloat(f32, self.height()), x, y, w, h, DrawFlags{});
} }
pub fn drawTintedScaledUniform(self: Bitmap, tint: Color, x: f32, y: f32, s: f32) void {
const sourceW = @intToFloat(f32, self.width());
const sourceH = @intToFloat(f32, self.height());
const scaledW = s * sourceW;
const scaledH = s * sourceH;
drawTintedScaledBitmap(self, tint, 0, 0, sourceW, sourceH, x, y, scaledW, scaledH, DrawFlags{});
}
pub fn height(self: Bitmap) i32 { pub fn height(self: Bitmap) i32 {
return getBitmapHeight(self); return getBitmapHeight(self);
} }

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 KiB

View File

@ -19,6 +19,7 @@ pub const Game = struct {
level: Level, level: Level,
isOver: bool = false, isOver: bool = false,
starsCollected: usize = 0, starsCollected: usize = 0,
starsTotal: usize,
playerPosition: engine.PointF, playerPosition: engine.PointF,
playerFallingAnimation: Animation, playerFallingAnimation: Animation,
@ -39,6 +40,7 @@ pub const Game = struct {
const playerPosition = level.character.float(); const playerPosition = level.character.float();
return Game{ return Game{
.level = level, .level = level,
.starsTotal = level.collectables.countValue(.Star),
.playerPosition = playerPosition, .playerPosition = playerPosition,
.playerFallingAnimation = Animation.initPartialLoop(renderer.sprites.get("character_lion_48").?, 0.2, 8, 12), .playerFallingAnimation = Animation.initPartialLoop(renderer.sprites.get("character_lion_48").?, 0.2, 8, 12),
.playerIdleAnimation = Animation.initPartialLoop(renderer.sprites.get("character_lion_48").?, 0.25, 0, 4), .playerIdleAnimation = Animation.initPartialLoop(renderer.sprites.get("character_lion_48").?, 0.25, 0, 4),

View File

@ -20,8 +20,9 @@ pub fn TileMap(comptime Value: type) type {
pub fn countValue(self: Column, value: Value) usize { pub fn countValue(self: Column, value: Value) usize {
var count: usize = 0; var count: usize = 0;
while (self.values.valueIterator()) |v| { var values = self.values.valueIterator();
if (value == v) { while (values.next()) |v| {
if (value == v.*) {
count += 1; count += 1;
} }
} }
@ -81,6 +82,7 @@ pub fn TileMap(comptime Value: type) type {
while (columns.next()) |c| { while (columns.next()) |c| {
count += c.countValue(value); count += c.countValue(value);
} }
return count;
} }
pub fn ensureColumn(self: *Self, x: i64) !*Column { pub fn ensureColumn(self: *Self, x: i64) !*Column {

View File

@ -174,7 +174,12 @@ pub const GameScene = struct {
self.game.drawSpriteFrameP("character_lion_48", self.game.playerIdleAnimation.current + playerDirectionFrameOffset, self.game.playerPosition.add(engine.PointF.init(-0.25, -0.25))); self.game.drawSpriteFrameP("character_lion_48", self.game.playerIdleAnimation.current + playerDirectionFrameOffset, self.game.playerPosition.add(engine.PointF.init(-0.25, -0.25)));
} }
ctx.renderer.printTextV("default", ctx.palette.background.text, 0.01, 0.01, Renderer.TextAlignment.Left, "Stars collected: {d}", .{self.game.starsCollected}); ctx.renderer.printTextV("default", ctx.palette.background.text, 0.01, 0.01, .Left, "Stars collected: {d}", .{self.game.starsCollected});
const percentage = self.game.starsCollected * 100 / self.game.starsTotal;
ctx.renderer.printTextV("default", ctx.palette.background.text, 0.96, 0.01, .Right, "{d} %", .{percentage});
if (percentage > 0) {
ctx.renderer.drawTintedSpriteFrameV("pie_charts_24", percentage - 1, ctx.palette.background.text, 0.97, 0.01);
}
if (self.game.isOver) { if (self.game.isOver) {
const textColor = allegro.mapRgb(0x48, 0x91, 0x00); const textColor = allegro.mapRgb(0x48, 0x91, 0x00);
@ -188,8 +193,8 @@ pub const GameScene = struct {
} }
if (ctx.showDebug) { if (ctx.showDebug) {
ctx.renderer.printTextV("debug", ctx.palette.background.text, 0.01, 0.1, Renderer.TextAlignment.Left, "Character: ({d:.2}, {d:.2})", .{ self.game.playerPosition.x, self.game.playerPosition.y }); ctx.renderer.printTextV("debug", ctx.palette.background.text, 0.01, 0.1, .Left, "Character: ({d:.2}, {d:.2})", .{ self.game.playerPosition.x, self.game.playerPosition.y });
ctx.renderer.printTextV("debug", ctx.palette.background.text, 0.01, 0.15, Renderer.TextAlignment.Left, "Tiles: ({d}, {d}) -> ({d}, {d})", .{ tileBounds.min.x, tileBounds.min.y, tileBounds.max.x, tileBounds.max.y }); ctx.renderer.printTextV("debug", ctx.palette.background.text, 0.01, 0.15, .Left, "Tiles: ({d}, {d}) -> ({d}, {d})", .{ tileBounds.min.x, tileBounds.min.y, tileBounds.max.x, tileBounds.max.y });
// self.game.debugHighlightTile(self.game.playerPosition); // self.game.debugHighlightTile(self.game.playerPosition);
} }
} }

View File

@ -45,12 +45,14 @@ pub fn main() !void {
allegro.setNewBitmapFlags(allegro.NewBitmapFlags{ .MIN_LINEAR = false, .MAG_LINEAR = false }); allegro.setNewBitmapFlags(allegro.NewBitmapFlags{ .MIN_LINEAR = false, .MAG_LINEAR = false });
try renderer.textures.addFromFile("character_lion_48", paths.AssetsDir ++ "/images/character_lion_48.png"); try renderer.textures.addFromFile("character_lion_48", paths.AssetsDir ++ "/images/character_lion_48.png");
try renderer.textures.addFromFile("item_star_32", paths.AssetsDir ++ "/images/item_star_32.png"); try renderer.textures.addFromFile("item_star_32", paths.AssetsDir ++ "/images/item_star_32.png");
try renderer.textures.addFromFile("pie_charts_24", paths.AssetsDir ++ "/images/pie_charts_24.png");
try renderer.textures.addFromFile("text_balloons", paths.AssetsDir ++ "/images/text_balloons.png"); try renderer.textures.addFromFile("text_balloons", paths.AssetsDir ++ "/images/text_balloons.png");
try renderer.textures.addFromFile("tiles_dirt_32", paths.AssetsDir ++ "/images/tiles_dirt_32.png"); try renderer.textures.addFromFile("tiles_dirt_32", paths.AssetsDir ++ "/images/tiles_dirt_32.png");
try renderer.textures.addFromFile("tiles_grass_32", paths.AssetsDir ++ "/images/tiles_grass_32.png"); try renderer.textures.addFromFile("tiles_grass_32", paths.AssetsDir ++ "/images/tiles_grass_32.png");
try renderer.sprites.addFromTextures(renderer.textures, "character_lion_48", 48, 48); try renderer.sprites.addFromTextures(renderer.textures, "character_lion_48", 48, 48);
try renderer.sprites.addFromTextures(renderer.textures, "item_star_32", 32, 32); try renderer.sprites.addFromTextures(renderer.textures, "item_star_32", 32, 32);
try renderer.sprites.addFromTextures(renderer.textures, "pie_charts_24", 24, 24);
try renderer.sprites.addFromTextures(renderer.textures, "text_balloons", 128, 96); try renderer.sprites.addFromTextures(renderer.textures, "text_balloons", 128, 96);
try renderer.sprites.addFromTextures(renderer.textures, "tiles_dirt_32", 32, 32); try renderer.sprites.addFromTextures(renderer.textures, "tiles_dirt_32", 32, 32);
try renderer.sprites.addFromTextures(renderer.textures, "tiles_grass_32", 32, 64); try renderer.sprites.addFromTextures(renderer.textures, "tiles_grass_32", 32, 64);

View File

@ -45,12 +45,6 @@ pub const Renderer = struct {
self.display.destroy(); self.display.destroy();
} }
pub fn drawText(self: Renderer, fontName: []const u8, color: allegro.Color, x: f32, y: f32, alignment: TextAlignment, text: [*:0]const u8) void {
if (self.fonts.get(fontName)) |font| {
font.draw(color, x, y, alignment.toDrawTextFlags(), text);
}
}
pub fn drawSpriteFrame(self: Renderer, spriteName: []const u8, frame: usize, x: f32, y: f32) void { pub fn drawSpriteFrame(self: Renderer, spriteName: []const u8, frame: usize, x: f32, y: f32) void {
if (self.sprites.getFrame(spriteName, frame)) |sprite| { if (self.sprites.getFrame(spriteName, frame)) |sprite| {
sprite.drawScaledUniform(x, y, self.viewport.scale); sprite.drawScaledUniform(x, y, self.viewport.scale);
@ -62,11 +56,30 @@ pub const Renderer = struct {
self.drawSpriteFrame(spriteName, frame, screen.x, screen.y); self.drawSpriteFrame(spriteName, frame, screen.x, screen.y);
} }
pub fn drawText(self: Renderer, fontName: []const u8, color: allegro.Color, x: f32, y: f32, alignment: TextAlignment, text: [*:0]const u8) void {
if (self.fonts.get(fontName)) |font| {
font.draw(color, x, y, alignment.toDrawTextFlags(), text);
}
}
pub fn drawTextV(self: Renderer, fontName: []const u8, color: allegro.Color, x: f32, y: f32, alignment: TextAlignment, text: [*:0]const u8) void { pub fn drawTextV(self: Renderer, fontName: []const u8, color: allegro.Color, x: f32, y: f32, alignment: TextAlignment, text: [*:0]const u8) void {
const screen = self.viewport.viewToScreen(x, y); const screen = self.viewport.viewToScreen(x, y);
self.drawText(fontName, color, screen.x, screen.y, alignment, text); self.drawText(fontName, color, screen.x, screen.y, alignment, text);
} }
pub fn drawTintedSpriteFrame(self: Renderer, spriteName: []const u8, frame: usize, tint: allegro.Color, x: f32, y: f32) void {
if (self.sprites.getFrame(spriteName, frame)) |sprite| {
sprite.drawTintedScaledUniform(tint, x, y, self.viewport.scale);
}
}
pub fn drawTintedSpriteFrameV(self: Renderer, spriteName: []const u8, frame: usize, tint: allegro.Color, x: f32, y: f32) void {
const screen = self.viewport.viewToScreen(x, y);
if (self.sprites.getFrame(spriteName, frame)) |sprite| {
sprite.drawTintedScaledUniform(tint, screen.x, screen.y, self.viewport.scale);
}
}
pub fn printText(self: *Renderer, font: []const u8, color: allegro.Color, x: f32, y: f32, alignment: TextAlignment, comptime fmt: []const u8, args: anytype) void { pub fn printText(self: *Renderer, font: []const u8, color: allegro.Color, x: f32, y: f32, alignment: TextAlignment, comptime fmt: []const u8, args: anytype) void {
const text = std.fmt.bufPrintZ(&self.textFormattingBuffer, fmt, args) catch { const text = std.fmt.bufPrintZ(&self.textFormattingBuffer, fmt, args) catch {
return; return;