commit c30d1375472725bf4cf82efa5358b38288cda90d
parent dd245c6dac0f507ac20e48320c590452302498ae
Author: brookjeynes <me@brookjeynes.dev>
Date: Thu, 30 Jul 2026 11:39:31 +1000
feat: add game screen state
Signed-off-by: brookjeynes <me@brookjeynes.dev>
Diffstat:
| M | src/main.zig | | | 164 | ++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------------- |
1 file changed, 108 insertions(+), 56 deletions(-)
diff --git a/src/main.zig b/src/main.zig
@@ -8,11 +8,19 @@ const Enemy = @import("./enemy.zig");
const Player = @import("./player.zig");
const Timer = @import("./timer.zig");
+const Screen = enum(u8) {
+ title,
+ gameplay,
+ upgrade,
+};
+
pub fn main() !void {
const screen_width: f16 = 800.0;
const screen_height: f16 = 1000.0;
const fps: u8 = 60;
+ var current_screen: Screen = .title;
+
raylib.InitWindow(screen_width, screen_height, "1.44mb game jam");
defer raylib.CloseWindow();
@@ -25,75 +33,119 @@ pub fn main() !void {
},
};
- var enemy_spawn_time: Timer = .{ .duration = 1 };
+ var enemy_spawn_time: Timer = .{ .duration = 0.7 };
var enemies: [255]Enemy = undefined;
var enemy_count: u8 = 0;
- while (!raylib.WindowShouldClose()) {
- const delta = raylib.GetFrameTime();
-
- player.handleMovement();
- for (0..enemy_count) |i| {
- enemies[i].handleMovement(player.position);
- }
-
- if (enemy_spawn_time.tick(delta)) {
- enemies[enemy_count] = spawnEnemy(
- .{
- .x = screen_width,
- .y = screen_height,
- },
- player,
- );
- enemy_count += 1;
- }
-
- raylib.BeginDrawing();
- defer raylib.EndDrawing();
-
- raylib.ClearBackground(raylib.RAYWHITE);
+ const button_size: raylib.Vector2 = .{ .x = 100, .y = 70 };
+ const button_position: raylib.Vector2 = .{
+ .x = (screen_width / 2) - (button_size.x / 2),
+ .y = (screen_height / 2) - (button_size.y / 2),
+ };
+ const button_bounds: raylib.Rectangle = .{
+ .x = button_position.x,
+ .y = button_position.y,
+ .width = button_size.x,
+ .height = button_size.y,
+ };
- player.render();
+ while (!raylib.WindowShouldClose()) {
+ switch (current_screen) {
+ .title => {
+ const mouse_point = raylib.GetMousePosition();
- var enemy_idx: usize = 0;
- while (enemy_idx < enemy_count) {
- const enemy = &enemies[enemy_idx];
+ raylib.BeginDrawing();
+ defer raylib.EndDrawing();
- for (0..player.bullet_pool.count) |bullet_idx| {
- const bullet = player.bullet_pool.bullets[bullet_idx];
+ raylib.ClearBackground(raylib.RAYWHITE);
- const is_collision = raylib.CheckCollisionCircleRec(
- .{
- .x = bullet.position.x,
- .y = bullet.position.y,
- },
- Bullet.radius,
- .{
- .x = enemy.position.x,
- .y = enemy.position.y,
- .width = enemy.size.x,
- .height = enemy.size.y,
- },
+ raylib.DrawRectangleV(
+ button_position,
+ button_size,
+ raylib.BLACK,
+ );
+ raylib.DrawText(
+ "start",
+ @intFromFloat(button_position.x + (button_size.x / 2)),
+ @intFromFloat(button_position.y + (button_size.y / 2)),
+ 18,
+ raylib.WHITE,
);
- if (is_collision) {
- player.bullet_pool.despawn(bullet_idx);
-
- for (enemy_idx..enemy_count) |i| {
- enemies[i] = enemies[i + 1];
- }
- if (enemy_count != 0) {
- enemy_count -= 1;
+ if (raylib.CheckCollisionPointRec(mouse_point, button_bounds)) {
+ if (raylib.IsMouseButtonReleased(raylib.MOUSE_BUTTON_LEFT)) {
+ current_screen = .gameplay;
}
- continue;
}
- }
+ },
+ .upgrade => {},
+ .gameplay => {
+ const delta = raylib.GetFrameTime();
+
+ player.handleMovement();
+ for (0..enemy_count) |i| {
+ enemies[i].handleMovement(player.position);
+ }
- enemy_idx += 1;
- }
+ if (enemy_spawn_time.tick(delta)) {
+ enemies[enemy_count] = spawnEnemy(
+ .{
+ .x = screen_width,
+ .y = screen_height,
+ },
+ player,
+ );
+ enemy_count += 1;
+ }
- for (0..enemy_count) |idx| {
- enemies[idx].render();
+ raylib.BeginDrawing();
+ defer raylib.EndDrawing();
+
+ raylib.ClearBackground(raylib.RAYWHITE);
+
+ player.render();
+
+ var enemy_idx: usize = 0;
+ while (enemy_idx < enemy_count) {
+ const enemy = &enemies[enemy_idx];
+
+ for (0..player.bullet_pool.count) |bullet_idx| {
+ const bullet = player.bullet_pool.bullets[bullet_idx];
+
+ const is_collision = raylib.CheckCollisionCircleRec(
+ .{
+ .x = bullet.position.x,
+ .y = bullet.position.y,
+ },
+ Bullet.radius,
+ .{
+ .x = enemy.position.x,
+ .y = enemy.position.y,
+ .width = enemy.size.x,
+ .height = enemy.size.y,
+ },
+ );
+
+ if (is_collision) {
+ player.bullet_pool.despawn(bullet_idx);
+
+ for (enemy_idx..enemy_count) |i| {
+ enemies[i] = enemies[i + 1];
+ }
+ if (enemy_count != 0) {
+ enemy_count -= 1;
+ }
+ continue;
+ }
+ }
+
+ enemy_idx += 1;
+ }
+
+ for (0..enemy_count) |idx| {
+ enemies[idx].render();
+ }
+ },
}
}
}