From 8dd5eff733e01df2728b270790acbace01435a91 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Tue, 16 Aug 2022 19:25:53 +0200 Subject: Implemented the jump command --- src/brothers.zig | 17 +++++++++++++++++ src/game.zig | 3 +++ src/main.zig | 2 ++ 3 files changed, 22 insertions(+) diff --git a/src/brothers.zig b/src/brothers.zig index cf78988..1b34a6b 100644 --- a/src/brothers.zig +++ b/src/brothers.zig @@ -28,6 +28,11 @@ pub const Brother = struct { _ = try rc.buffer.writer().write(line); } } + pub fn moveJump(self: *Brother) void { + if (self.dy == 0) { // no double jumps! TODO allow kicks off the wall + self.dy -= 4 / (1000 / 60.0); + } + } pub fn moveLeft(self: *Brother) void { self.dx -= 5 / (1000 / 60.0); self.moveDuration = 24; @@ -37,6 +42,7 @@ pub const Brother = struct { self.moveDuration = 24; } pub fn step(self: *Brother) void { + // Horizontal movement const x = self.x + self.dx; const ll = leftLimit[@enumToInt(self.side)]; const rl = rightLimit[@enumToInt(self.side)]; @@ -57,6 +63,17 @@ pub const Brother = struct { } } } + // Vertical movement + const y = self.y + self.dy; + if (y < 12) { // jumping + self.y = 12; + self.dy = -self.dy; + } else if (y > 17) { // falling + self.y = 17; + self.dy = 0; + } else { + self.y = y; + } } pub fn reset(self: *Brother, side: ?Side) void { if (side) |s| { diff --git a/src/game.zig b/src/game.zig index 97c2c80..ddd0b62 100644 --- a/src/game.zig +++ b/src/game.zig @@ -12,6 +12,9 @@ pub const Game = struct { try self.brothers[0].draw(rc); try self.brothers[1].draw(rc); } + pub fn moveJump(self: *Game) void { + self.brothers[@enumToInt(self.side)].moveJump(); + } pub fn moveLeft(self: *Game) void { self.brothers[@enumToInt(self.side)].moveLeft(); } diff --git a/src/main.zig b/src/main.zig index 120e926..3a534b9 100644 --- a/src/main.zig +++ b/src/main.zig @@ -53,6 +53,8 @@ pub fn main() !void { gs.moveLeft(); } else if (in.eqlDescription("arrow-right") or in.eqlDescription("d")) { gs.moveRight(); + } else if (in.eqlDescription("arrow-up") or in.eqlDescription("space")) { + gs.moveJump(); } } } else { -- cgit v1.2.3