diff options
author | Julien Dessaux | 2022-08-22 20:18:30 +0200 |
---|---|---|
committer | Julien Dessaux | 2022-08-22 20:18:30 +0200 |
commit | a899bc060b6180635f3ff4ac73b7e6aee4fe9a7b (patch) | |
tree | 4ffe0a498eff6980e948c96c0e1cbbb69c37063a | |
parent | Allow to control both brothers from the same keyboard (diff) | |
download | grenade-brothers-a899bc060b6180635f3ff4ac73b7e6aee4fe9a7b.tar.gz grenade-brothers-a899bc060b6180635f3ff4ac73b7e6aee4fe9a7b.tar.bz2 grenade-brothers-a899bc060b6180635f3ff4ac73b7e6aee4fe9a7b.zip |
Implemented a ball with basic physics and collisions
-rw-r--r-- | src/brothers.zig | 31 | ||||
-rw-r--r-- | src/game.zig | 9 |
2 files changed, 37 insertions, 3 deletions
diff --git a/src/brothers.zig b/src/brothers.zig index 7aaf7de..448aaec 100644 --- a/src/brothers.zig +++ b/src/brothers.zig @@ -1,6 +1,8 @@ const std = @import("std"); const spoon = @import("spoon"); +const ball = @import("ball.zig"); + pub const Side = enum(u1) { left, right, @@ -41,7 +43,7 @@ pub const Brother = struct { self.dx += 5 / (1000 / 60.0); self.moveDuration = 24; } - pub fn step(self: *Brother) void { + pub fn step(self: *Brother, b: *ball.Ball) void { // Horizontal movement const x = self.x + self.dx; const ll = leftLimit[@enumToInt(self.side)]; @@ -74,6 +76,33 @@ pub const Brother = struct { } else { self.y = y; } + // Check for ball collisions + if (b.y >= y and b.y <= y + 2 and b.x >= x and b.x < x + 5) { + if (b.dy > 0) { + b.dy = -b.dy / 1.5; + } + b.dx = b.dx / 2.0; + var strength: f64 = 1; + if (b.dx > 0 and self.dx < 0) + strength *= 2; // moving in opposite directions + if (y < 12) { // jumping + strength *= 2; + } + if (b.x < x + 1) { + b.dx -= strength * 4 / (1000 / 60.0); + } else if (b.x < x + 2) { + b.dx -= strength * 2 / (1000 / 60.0); + } else if (b.x < x + 3) { + var modifier: f64 = 1; + if (self.side == .left) modifier = -1; + b.dx += modifier * strength * 2 / (1000 / 60.0); + } else if (b.x < x + 4) { + b.dx += strength * 2 / (1000 / 60.0); + } else { + b.dx += strength * 4 / (1000 / 60.0); + } + b.dy = b.dy * strength - 0.04; + } } pub fn reset(self: *Brother, side: ?Side) void { if (side) |s| { diff --git a/src/game.zig b/src/game.zig index 8af1889..c8492df 100644 --- a/src/game.zig +++ b/src/game.zig @@ -1,16 +1,19 @@ const std = @import("std"); const spoon = @import("spoon"); +const ball = @import("ball.zig"); const brothers = @import("brothers.zig"); const playfield = @import("playfield.zig"); pub const Game = struct { + ball: ball.Ball = undefined, brothers: [2]brothers.Brother = undefined, side: brothers.Side = undefined, pub fn draw(self: Game, rc: *spoon.Term.RenderContext) !void { try playfield.draw(rc); try self.brothers[0].draw(rc); try self.brothers[1].draw(rc); + try self.ball.draw(rc); } pub fn moveJump(self: *Game, side: brothers.Side) void { self.brothers[@enumToInt(side)].moveJump(); @@ -26,11 +29,13 @@ pub const Game = struct { self.resetRound(); } pub fn resetRound(self: *Game) void { + self.ball.reset(.left); self.brothers[0].reset(.left); self.brothers[1].reset(.right); } pub fn step(self: *Game) void { - self.brothers[0].step(); - self.brothers[1].step(); + self.ball.step(); + self.brothers[0].step(&self.ball); + self.brothers[1].step(&self.ball); } }; |