diff --git a/allegro/allegro.zig b/allegro/allegro.zig index 5fabd77..224bfff 100644 --- a/allegro/allegro.zig +++ b/allegro/allegro.zig @@ -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{}); } + 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 { return getBitmapHeight(self); } diff --git a/src/assets/images/pie_charts_24.png b/src/assets/images/pie_charts_24.png new file mode 100644 index 0000000..75fa6c2 Binary files /dev/null and b/src/assets/images/pie_charts_24.png differ diff --git a/src/game/game.zig b/src/game/game.zig index fd32e33..cc40398 100644 --- a/src/game/game.zig +++ b/src/game/game.zig @@ -19,6 +19,7 @@ pub const Game = struct { level: Level, isOver: bool = false, starsCollected: usize = 0, + starsTotal: usize, playerPosition: engine.PointF, playerFallingAnimation: Animation, @@ -39,6 +40,7 @@ pub const Game = struct { const playerPosition = level.character.float(); return Game{ .level = level, + .starsTotal = level.collectables.countValue(.Star), .playerPosition = playerPosition, .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), diff --git a/src/game/tile_map.zig b/src/game/tile_map.zig index 170f741..8ca37fc 100644 --- a/src/game/tile_map.zig +++ b/src/game/tile_map.zig @@ -20,8 +20,9 @@ pub fn TileMap(comptime Value: type) type { pub fn countValue(self: Column, value: Value) usize { var count: usize = 0; - while (self.values.valueIterator()) |v| { - if (value == v) { + var values = self.values.valueIterator(); + while (values.next()) |v| { + if (value == v.*) { count += 1; } } @@ -81,6 +82,7 @@ pub fn TileMap(comptime Value: type) type { while (columns.next()) |c| { count += c.countValue(value); } + return count; } pub fn ensureColumn(self: *Self, x: i64) !*Column { diff --git a/src/game_scene.zig b/src/game_scene.zig index d12143d..ad85c63 100644 --- a/src/game_scene.zig +++ b/src/game_scene.zig @@ -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))); } - 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) { const textColor = allegro.mapRgb(0x48, 0x91, 0x00); @@ -188,8 +193,8 @@ pub const GameScene = struct { } 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.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.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, .Left, "Tiles: ({d}, {d}) -> ({d}, {d})", .{ tileBounds.min.x, tileBounds.min.y, tileBounds.max.x, tileBounds.max.y }); // self.game.debugHighlightTile(self.game.playerPosition); } } diff --git a/src/main.zig b/src/main.zig index d2801cc..c0153c7 100644 --- a/src/main.zig +++ b/src/main.zig @@ -45,12 +45,14 @@ pub fn main() !void { 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("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("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.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, "pie_charts_24", 24, 24); 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_grass_32", 32, 64); diff --git a/src/renderer.zig b/src/renderer.zig index 9a1ecfe..e15d6e0 100644 --- a/src/renderer.zig +++ b/src/renderer.zig @@ -45,12 +45,6 @@ pub const Renderer = struct { 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 { if (self.sprites.getFrame(spriteName, frame)) |sprite| { sprite.drawScaledUniform(x, y, self.viewport.scale); @@ -62,11 +56,30 @@ pub const Renderer = struct { 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 { const screen = self.viewport.viewToScreen(x, y); 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 { const text = std.fmt.bufPrintZ(&self.textFormattingBuffer, fmt, args) catch { return;