From bbd8a562097d6e7d42b3792d54e5f6e74b0c6d85 Mon Sep 17 00:00:00 2001 From: Sander Schobers Date: Mon, 5 Jun 2023 04:47:44 +0200 Subject: [PATCH] Added percentage of stars collected in level + mini pie chart. --- allegro/allegro.zig | 8 ++++++++ src/assets/images/pie_charts_24.png | Bin 0 -> 2175 bytes src/game/game.zig | 2 ++ src/game/tile_map.zig | 6 ++++-- src/game_scene.zig | 11 ++++++++--- src/main.zig | 2 ++ src/renderer.zig | 25 +++++++++++++++++++------ 7 files changed, 43 insertions(+), 11 deletions(-) create mode 100644 src/assets/images/pie_charts_24.png 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 0000000000000000000000000000000000000000..75fa6c29e2b80546f7df4ce8e55344ff4940193a GIT binary patch literal 2175 zcmbVOdr(t%8cle~02a~32=Y*rafL#psYOtTh=9S8RSH3@KoCk;6(g@A4->$JVdaq# zhk_DJ*$_Zepe$7oih))_fk1dalG5;Oc!d}O*LINxq^ngn-?>=Uq=noyjyL>QyZ3mY`v2nK1m1YG1Mt^b_&=orEH5C zy-ffNAQk+>?vSJZ zBfpqnemAoGnq?%9JL{J}_-f+8dCyi_WR=wo0j zE4-{I@6|Q}JmWBZG#W}Dw_4x<^D(E5DhBMT5p7bb)O535`F;1HfUZ5|%ME2Nd~_PS zsU&)|KzlD%%MtGLBJ@q1y20wTRI1x@xU{uUkGd0XkZp!Pu%=(md!71V@wL72z|EN&;tTjyPs~htM~$yde1QGQ<9=OIv)V92oGbsgAKZ=qP@Qxe_7>au8)<<%mdloxw99Lq+ z4G!sHtVximYq8K=_|nk+rdO;K-_rC401ym@LAdj2@Dpgr9DqHV%eOjxOvgm;P{;2A zW_l$tfcyuNJ@kdeG;p18Bd+a|dsI+4yD62J|Hx)x3gsZ=7;cI(VO3Htme<5VPR&-R&&O0zVt~ds3Bb|f1 z+g3BW&Y{}FLQ+ChZ8ub}VU+heF8Z|$tY0H_HeMB%x zv_2~o+O9Y2=ysZja!A4%O}DD;mU4a;nr^TDTG20TPk(admCiqDiv0v5>bSg;@-S^T zIXv$vU#BE2a188rjeW%O?!Pc8F< z^-J18IO$6JFfMC_p2UD$L;RN>rv?Jog+&P&P}4LVJ9*Hrg&=pt{Iq?|T~rlwI4;lA!ZPzHRrY18H z+PKo(J^5>ak%=B1@rZ_=BNjb zWH=RBtJ9sYif%5Pi@UcVMBgP#&8k+-mSZ8NH7Ob5yGBJp#b4UYQl6A73Wd-zG4zRU z5`DGQAvYq&bN*rxjm2Ya!X(q=7yNSrHbwMRhc7$-4oSjH|%Jb@{yi zE&|GY+6R|y=M)O=itR9d@L4@-_9GY?vVjJTikD7u(<;#>I`YqBuv#jUZM!t1MD&3*duz$1k-ZC@)_c20&vZDb-wnK7fCz@}pTuWUpE6rWsJac@4Wn z|~ zO;Tus`dz-Re>(Ah` F{{h*QNfQ77 literal 0 HcmV?d00001 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;