From 79905b35469c750f1a58d9b34dd5691e8f31aaa6 Mon Sep 17 00:00:00 2001 From: Rokas Puzonas Date: Sun, 18 Feb 2024 20:24:25 +0200 Subject: [PATCH] add pause menu --- src/main-scene.zig | 50 ++++++++++++++++++++++++++++++++++++++++---- src/player-input.zig | 12 +++++++++++ 2 files changed, 58 insertions(+), 4 deletions(-) diff --git a/src/main-scene.zig b/src/main-scene.zig index 171fd92..c145c6b 100644 --- a/src/main-scene.zig +++ b/src/main-scene.zig @@ -202,6 +202,7 @@ player: Player, rope: Rope, ui: UI, should_close: bool = false, +paused: bool = false, camera: rl.Camera2D, @@ -505,6 +506,41 @@ fn tickUI(self: *Self) !void { if (self.ui.button("Exit?", content.left(), content.top() + 120, content.width, 30)) { self.should_close = true; } + } else if (self.paused) { + const modal_size = rl.Vector2{ .x = 200, .y = 200 }; + const modal = screen_box.box( + screen_box.center() - modal_size.x/2, + screen_box.middle() - modal_size.y/2, + modal_size.x, + modal_size.y + ); + const content = modal.margin(10); + + rl.DrawRectangleRec(modal.rect(), rl.RAYWHITE); + UI.drawTextAligned( + font, + "Paused", + content.center_top().add(.{ .x = 0, .y = 30 }), + 30, + rl.BLACK + ); + + if (self.ui.button("Continue?", content.left(), content.top() + 70, content.width, 30)) { + self.togglePaused(); + } + + if (self.ui.button("Exit?", content.left(), content.top() + 120, content.width, 30)) { + self.should_close = true; + } + } +} + +fn togglePaused(self: *Self) void { + self.paused = !self.paused; + if (self.paused) { + self.player.input.deactivate(); + } else { + self.player.input.activate(); } } @@ -521,11 +557,17 @@ pub fn tick(self: *Self) !void { var enemies = &self.enemies; var rope = &self.rope; - self.tickPlayer(); - try self.tickEnemies(); + if (self.player.input.isPausePressed()) { + self.togglePaused(); + } - if (self.player.alive()) { - self.timePassed += rl.GetFrameTime(); + if (!self.paused) { + self.tickPlayer(); + try self.tickEnemies(); + + if (self.player.alive()) { + self.timePassed += rl.GetFrameTime(); + } } const deathBallPosition = rope.nodePositions.get(rope.nodePositions.len-1); diff --git a/src/player-input.zig b/src/player-input.zig index 5e30074..76bdc51 100644 --- a/src/player-input.zig +++ b/src/player-input.zig @@ -104,6 +104,18 @@ pub fn getHandPosition(self: *@This()) rl.Vector2 { return hand_position; } +pub fn isPausePressed(self: *@This()) bool { + if (rl.IsKeyPressed(rl.KeyboardKey.KEY_ESCAPE)) { + return true; + } + + if (rl.IsGamepadAvailable(self.gamepad) and rl.IsGamepadButtonPressed(self.gamepad, .GAMEPAD_BUTTON_MIDDLE_RIGHT)) { + return true; + } + + return false; +} + pub fn activate(self: *@This()) void { if (self.active) return; rl.DisableCursor();