diff options
author | Julien Dessaux | 2022-08-14 15:36:59 +0200 |
---|---|---|
committer | Julien Dessaux | 2022-08-14 15:52:24 +0200 |
commit | a9c30f6236af36f6c428b0b10dd4d48a21c70733 (patch) | |
tree | cc31fd0e649409edb114046270a9a9f3100e8687 /src/main.zig | |
parent | Wrote initial game objects (diff) | |
download | grenade-brothers-a9c30f6236af36f6c428b0b10dd4d48a21c70733.tar.gz grenade-brothers-a9c30f6236af36f6c428b0b10dd4d48a21c70733.tar.bz2 grenade-brothers-a9c30f6236af36f6c428b0b10dd4d48a21c70733.zip |
Implement left and right movements
Diffstat (limited to '')
-rw-r--r-- | src/main.zig | 29 |
1 files changed, 20 insertions, 9 deletions
diff --git a/src/main.zig b/src/main.zig index cec8688..120e926 100644 --- a/src/main.zig +++ b/src/main.zig @@ -33,21 +33,32 @@ pub fn main() !void { try term.fetchSize(); try term.setWindowTitle("Grenade Brothers", .{}); - gs.reset(); + gs.reset(.left); try renderAll(); var buf: [16]u8 = undefined; while (!done) { - _ = try std.os.poll(&fds, 100); - - const read = try term.readInput(&buf); - var it = spoon.inputParser(buf[0..read]); - while (it.next()) |in| { - if (in.eqlDescription("escape") or in.eqlDescription("q")) { - done = true; - break; + // TODO We need to measure how long it took before a key was hit so that we can adjust the timeout on the next loop + // otherwise we will get inconsistent ticks for movement steps + const timeout = try std.os.poll(&fds, @floatToInt(u64, 1000 / 60.0)); + + if (timeout > 0) { // if timeout if not 0 then some fds we are polling have events for us + const read = try term.readInput(&buf); + var it = spoon.inputParser(buf[0..read]); + while (it.next()) |in| { + if (in.eqlDescription("escape") or in.eqlDescription("q")) { + done = true; + break; + } else if (in.eqlDescription("arrow-left") or in.eqlDescription("a")) { + gs.moveLeft(); + } else if (in.eqlDescription("arrow-right") or in.eqlDescription("d")) { + gs.moveRight(); + } } + } else { + gs.step(); } + try renderAll(); } } |