Skip to content

Commit

Permalink
fix: proper game loop
Browse files Browse the repository at this point in the history
game can be replayed now
  • Loading branch information
mdwmage committed Jun 7, 2024
1 parent 23efbd6 commit 55a6293
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
8 changes: 8 additions & 0 deletions src/entities.zig
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ pub const Player = struct {
},
Tile.end => {
level.complete = true;
for (map.tiles, 0..) |column, i| {
for (column, 0..) |tile, j| {
if (tile == Tile.start) {
player.x = j;
player.y = i;
}
}
}
},
}
// Check player position based on enemies
Expand Down
1 change: 1 addition & 0 deletions src/game.zig
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,6 @@ pub fn loadGame(path: [:0]const u8, allocator: std.mem.Allocator) !screen.Level
}
}
game.score = 0;
game.moves = 0;
return game;
}
4 changes: 2 additions & 2 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const screenWidth = 800;

// Types

const Scenes = enum {
pub const Scenes = enum {
MainMenu,
Level,
};
Expand Down Expand Up @@ -63,7 +63,7 @@ pub fn main() !void {
}
},
else => {
level.tick();
currentScene = level.tick();
},
}
}
Expand Down
10 changes: 9 additions & 1 deletion src/screen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//

const ui = @import("ui.zig");
const Scene = @import("main.zig").Scenes;
const entities = @import("entities.zig");
const melodie = @import("melodie.zig");
const raylib = @import("raylib");
Expand All @@ -28,9 +29,10 @@ pub const Level = struct {
beat: melodie.Beat,
song: raylib.Sound,
score: f32,
moves: u32,
complete: bool,

pub fn tick(self: *Level) void {
pub fn tick(self: *Level) Scene {
if (self.complete) {
const returnButton = ui.Button{
.x = 250,
Expand All @@ -44,6 +46,11 @@ pub const Level = struct {
raylib.clearBackground(raylib.Color.black);
raylib.drawText(raylib.textFormat("Victory! Score: %.1f", .{self.score}), 100, 250, 60, raylib.Color.gold);
returnButton.render();

if (returnButton.clicked()) {
self.complete = false;
return Scene.MainMenu;
}
} else {
// Music
if (!raylib.isSoundPlaying(self.song)) {
Expand Down Expand Up @@ -73,6 +80,7 @@ pub const Level = struct {

self.player.checkTile(&self.map, self);
}
return Scene.Level;
}
};

Expand Down

0 comments on commit 55a6293

Please sign in to comment.