From 2b9c6f8533f786d1ae2fa2632bca25638770205e Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Sun, 25 Sep 2022 23:01:37 +0200 Subject: Added brothers and ball collisions --- src/ball.zig | 4 ++-- src/brothers.zig | 20 ++++++++++++++++++++ src/game.zig | 2 ++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/ball.zig b/src/ball.zig index 8c446b0..a9b1c77 100644 --- a/src/ball.zig +++ b/src/ball.zig @@ -59,7 +59,7 @@ pub const Ball = struct { }; //----- Sprite ---------------------------------------------------------------- -const ball_width = 8; -const ball_height = 8; +pub const ball_width = 8; +pub const ball_height = 8; const ball_flags = 1; // BLIT_2BPP const ball = [16]u8{ 0x1a, 0xa4, 0x6f, 0xf9, 0xbf, 0xae, 0xbf, 0xae, 0xbf, 0xfe, 0xbf, 0xfe, 0x6f, 0xf9, 0x1a, 0xa4 }; diff --git a/src/brothers.zig b/src/brothers.zig index c3bde96..e68cb0f 100644 --- a/src/brothers.zig +++ b/src/brothers.zig @@ -1,3 +1,4 @@ +const ball = @import("ball.zig"); const inputs = @import("inputs.zig"); const std = @import("std"); const utils = @import("utils.zig"); @@ -10,6 +11,25 @@ pub const Brother = struct { x: u8, y: f64, vy: f64, + pub fn collide(self: Brother, b: *ball.Ball) void { + // compute the collision box + const x1: f64 = @intToFloat(f64, self.x) - ball.ball_width; + const x2: f64 = @intToFloat(f64, self.x) + brother_width; + const y1: f64 = self.y - brother_height - ball.ball_height; + const y2: f64 = self.y - brother_height; + if (b.x >= x1 and b.x < x2 and b.y >= y1 and b.y < y2) { + // horizontal adjustement + b.vx += (b.x - @intToFloat(f64, self.x) - 4) * 10; + // vertical adjustment + if (b.vy > 0) { + b.vy = -b.vy * utils.bounce; + if (self.vy < 0) + b.vy *= 2; + } + b.vy -= 22; + b.y = y1; + } + } pub fn draw(self: Brother) void { var y = @floatToInt(u8, std.math.round(self.y)); w4.DRAW_COLORS.* = 0x30; diff --git a/src/game.zig b/src/game.zig index 01e3dc4..d0395a0 100644 --- a/src/game.zig +++ b/src/game.zig @@ -35,5 +35,7 @@ pub const Game = struct { self.brothers[0].update(self.gamepads[0]); self.brothers[1].update(self.gamepads[1]); self.ball.update(); + self.brothers[0].collide(&self.ball); + self.brothers[1].collide(&self.ball); } }; -- cgit v1.2.3