commit a9824dfff8d77abe9ae2fb41571093d09c1e15ba
parent 248f0a5e5bd5d84234854f7aff8183236b37d90f
Author: brookjeynes <me@brookjeynes.dev>
Date: Fri, 31 Jul 2026 09:36:58 +1000
refactor: small struct optimisations
Signed-off-by: brookjeynes <me@brookjeynes.dev>
Diffstat:
5 files changed, 59 insertions(+), 65 deletions(-)
diff --git a/build.zig b/build.zig
@@ -2,7 +2,7 @@ const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
- const optimize = b.standardOptimizeOption(.{});
+ const optimize: std.builtin.OptimizeMode = .ReleaseSmall;
const raylib = b.dependency("raylib", .{
.optimize = optimize,
diff --git a/src/bullet.zig b/src/bullet.zig
@@ -1,7 +1,8 @@
const raylib = @import("raylib");
const Timer = @import("./timer.zig");
-pub const radius: f16 = 10.0;
+pub const max_bullets: u8 = 255;
+pub const radius: f32 = 10.0;
pub const Bullet = struct {
position: raylib.Vector2,
@@ -9,61 +10,57 @@ pub const Bullet = struct {
color: raylib.Color,
};
-pub fn Pool(comptime upper: usize) type {
- return struct {
- const Self = @This();
+pub const Pool = struct {
+ bullets: [255]Bullet,
+ count: usize = 0,
+ fire_timer: Timer,
- bullets: [upper]Bullet,
- count: usize = 0,
- fire_timer: Timer,
+ pub fn tick(self: *Pool) bool {
+ return self.fire_timer.tick(raylib.GetFrameTime());
+ }
- pub fn tick(self: *Self) bool {
- return self.fire_timer.tick(raylib.GetFrameTime());
- }
+ pub fn spawn(
+ self: *Pool,
+ position: raylib.Vector2,
+ velocity: raylib.Vector2,
+ colour: raylib.Color,
+ ) void {
+ self.bullets[self.count] = .{
+ .position = position,
+ .velocity = velocity,
+ .color = colour,
+ };
+ self.count += 1;
+ }
- pub fn spawn(
- self: *Self,
- position: raylib.Vector2,
- velocity: raylib.Vector2,
- colour: raylib.Color,
- ) void {
- self.bullets[self.count] = .{
- .position = position,
- .velocity = velocity,
- .color = colour,
- };
- self.count += 1;
+ pub fn despawn(self: *Pool, idx: usize) void {
+ for (idx..self.count) |i| {
+ self.bullets[i] = self.bullets[i + 1];
}
- pub fn despawn(self: *Self, idx: usize) void {
- for (idx..self.count) |i| {
- self.bullets[i] = self.bullets[i + 1];
- }
-
- self.count -= 1;
- }
+ self.count -= 1;
+ }
- pub fn render(self: *Self, screen_size: raylib.Vector2) void {
- const delta = raylib.GetFrameTime();
+ pub fn render(self: *Pool, screen_size: raylib.Vector2) void {
+ const delta = raylib.GetFrameTime();
- var i: usize = 0;
- while (i < self.count) {
- var bullet = &self.bullets[i];
- bullet.position.x += bullet.velocity.x * delta;
- bullet.position.y += bullet.velocity.y * delta;
+ var i: usize = 0;
+ while (i < self.count) {
+ var bullet = &self.bullets[i];
+ bullet.position.x += bullet.velocity.x * delta;
+ bullet.position.y += bullet.velocity.y * delta;
- if (bullet.position.x < -radius * 2 or
- bullet.position.x > screen_size.x + radius * 2 or
- bullet.position.y < -radius * 2 or
- bullet.position.y > screen_size.y + radius * 2)
- {
- self.despawn(i);
- continue;
- }
-
- raylib.DrawCircleV(bullet.position, radius, bullet.color);
- i += 1;
+ if (bullet.position.x < -radius * 2 or
+ bullet.position.x > screen_size.x + radius * 2 or
+ bullet.position.y < -radius * 2 or
+ bullet.position.y > screen_size.y + radius * 2)
+ {
+ self.despawn(i);
+ continue;
}
+
+ raylib.DrawCircleV(bullet.position, radius, bullet.color);
+ i += 1;
}
- };
-}
+ }
+};
diff --git a/src/enemy.zig b/src/enemy.zig
@@ -3,18 +3,17 @@ const raymath = @import("raymath");
const bullet = @import("./bullet.zig");
-const max_spawned_bullets = 2000;
-pub const bullet_damage: f16 = 50.0;
+pub const bullet_damage: f32 = 50.0;
pub const Enemy = @This();
-movement_speed: f16 = 40.0,
-bullet_speed: f16 = 100.0,
+movement_speed: f32 = 40.0,
+bullet_speed: f32 = 100.0,
size: raylib.Vector2 = .{ .x = 40, .y = 40 },
direction: raylib.Vector2 = .{},
position: raylib.Vector2 = .{},
-bullet_pool: bullet.Pool(max_spawned_bullets),
+bullet_pool: bullet.Pool,
pub fn handleMovement(self: *Enemy, target: raylib.Vector2) void {
const delta: f32 = raylib.GetFrameTime();
diff --git a/src/main.zig b/src/main.zig
@@ -237,7 +237,7 @@ pub fn main() !void {
}
}
- const health_percent: f16 = @max(0, @min(1, player.health / Player.max_health));
+ const health_percent: f32 = @max(0, @min(1, player.health / Player.max_health));
const damage_percent = 1 - health_percent;
raylib.DrawRectangle(
0,
@@ -285,9 +285,8 @@ fn spawnEnemy(screen: raylib.Vector2, player: Player, difficulty: f32) Enemy {
}
}
- const diff_f16: f16 = @floatCast(difficulty);
- enemy.movement_speed = @min(@as(f16, 160), @as(f16, 40) * diff_f16);
- enemy.bullet_speed = @min(@as(f16, 300), @as(f16, 100) * diff_f16);
+ enemy.movement_speed = @min(160.0, 40.0 * difficulty);
+ enemy.bullet_speed = @min(300.0, 100.0 * difficulty);
enemy.bullet_pool.fire_timer.duration = @max(0.3, 2.0 / difficulty);
return enemy;
diff --git a/src/player.zig b/src/player.zig
@@ -7,25 +7,24 @@ const Timer = @import("./timer.zig");
pub const Player = @This();
-const max_spawned_bullets = 2000;
const bullet_speed: f32 = 300.0;
pub const bullet_spawn_cooldown: f32 = 0.4;
const acceleration: f32 = 10000.0;
const friction: f32 = 10000.0;
const max_delta: f32 = 1.0 / 30.0;
-pub const max_health: f16 = 255;
-const health_regen_amount: f16 = 50;
+pub const max_health: f32 = 255;
+const health_regen_amount: f32 = 50;
-movement_speed: f16 = 140.0,
+movement_speed: f32 = 140.0,
size: raylib.Vector2 = .{ .x = 50, .y = 50 },
position: raylib.Vector2 = .{},
velocity: raylib.Vector2 = .{},
-health: f16 = max_health,
+health: f32 = max_health,
health_regen_timer: Timer = .{
.duration = 5.0,
},
-bullet_pool: bullet.Pool(max_spawned_bullets),
+bullet_pool: bullet.Pool,
pub fn render(self: *Player) void {
raylib.DrawRectangleV(