aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulien Dessaux2022-08-16 19:25:53 +0200
committerJulien Dessaux2022-08-16 19:25:53 +0200
commit8dd5eff733e01df2728b270790acbace01435a91 (patch)
tree4a73d8484578d815d75edb606ed775283e8c2bad
parentImplement left and right movements (diff)
downloadgrenade-brothers-8dd5eff733e01df2728b270790acbace01435a91.tar.gz
grenade-brothers-8dd5eff733e01df2728b270790acbace01435a91.tar.bz2
grenade-brothers-8dd5eff733e01df2728b270790acbace01435a91.zip
Implemented the jump command
-rw-r--r--src/brothers.zig17
-rw-r--r--src/game.zig3
-rw-r--r--src/main.zig2
3 files changed, 22 insertions, 0 deletions
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 {