From 186f8b66f6ef2ae417b7b0cd596dd4f815da577e Mon Sep 17 00:00:00 2001 From: Jared Miller Date: Mon, 15 Dec 2025 14:13:18 -0500 Subject: [PATCH] Allow holding keys to adjust entity counts --- src/sandbox_main.zig | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/src/sandbox_main.zig b/src/sandbox_main.zig index 1938d52..b9c2b2a 100644 --- a/src/sandbox_main.zig +++ b/src/sandbox_main.zig @@ -225,20 +225,46 @@ pub fn main() !void { } } +const REPEAT_DELAY: f32 = 0.4; // initial delay before repeat +const REPEAT_RATE: f32 = 0.05; // repeat interval + +var add_timer: f32 = 0; +var sub_timer: f32 = 0; + fn handleInput(entities: *sandbox.Entities, rng: *std.Random, paused: *bool) void { + const dt = rl.getFrameTime(); const shift = rl.isKeyDown(.left_shift) or rl.isKeyDown(.right_shift); const add_count: usize = if (shift) 10000 else 1000; + const add_held = rl.isKeyDown(.equal) or rl.isKeyDown(.kp_add); + const sub_held = rl.isKeyDown(.minus) or rl.isKeyDown(.kp_subtract); + // add entities: = or + if (rl.isKeyPressed(.equal) or rl.isKeyPressed(.kp_add)) { - for (0..add_count) |_| { - entities.add(rng); + for (0..add_count) |_| entities.add(rng); + add_timer = REPEAT_DELAY; + } else if (add_held) { + add_timer -= dt; + if (add_timer <= 0) { + for (0..add_count) |_| entities.add(rng); + add_timer = REPEAT_RATE; } + } else { + add_timer = 0; } // remove entities: - or _ if (rl.isKeyPressed(.minus) or rl.isKeyPressed(.kp_subtract)) { entities.remove(add_count); + sub_timer = REPEAT_DELAY; + } else if (sub_held) { + sub_timer -= dt; + if (sub_timer <= 0) { + entities.remove(add_count); + sub_timer = REPEAT_RATE; + } + } else { + sub_timer = 0; } // reset: r