diff options
author | Julien Dessaux | 2022-09-23 19:09:28 +0200 |
---|---|---|
committer | Julien Dessaux | 2022-09-23 19:09:28 +0200 |
commit | 63029a3a27eb3ae44d802cb17b0c453e84595ade (patch) | |
tree | 9c5ad6c4fc077448d172bb30df90494665bae73a /src/ball.zig | |
parent | Added brothers jumping (diff) | |
download | grenade-brothers-63029a3a27eb3ae44d802cb17b0c453e84595ade.tar.gz grenade-brothers-63029a3a27eb3ae44d802cb17b0c453e84595ade.tar.bz2 grenade-brothers-63029a3a27eb3ae44d802cb17b0c453e84595ade.zip |
Added ball physics
Diffstat (limited to '')
-rw-r--r-- | src/ball.zig | 33 |
1 files changed, 28 insertions, 5 deletions
diff --git a/src/ball.zig b/src/ball.zig index 11737bb..f2458d4 100644 --- a/src/ball.zig +++ b/src/ball.zig @@ -3,21 +3,44 @@ const utils = @import("utils.zig"); const w4 = @import("wasm4.zig"); pub const Ball = struct { + // position of the top left corner x: f64, y: f64, - dx: f64, - dy: f64, + vx: f64, + vy: f64, pub fn draw(self: Ball) void { var y = @floatToInt(u8, std.math.round(self.y)); var x = @floatToInt(u8, std.math.round(self.x)); - w4.DRAW_COLORS.* = 0x4321; + w4.DRAW_COLORS.* = 0x2400; w4.blit(&ball, x, y, ball_width, ball_height, ball_flags); } pub fn resetRound(self: *Ball, side: utils.side) void { self.x = @intToFloat(f64, utils.startingX[@enumToInt(side)] + 4); self.y = 160 - 32 - 8; - self.dx = 0; - self.dy = 0; + self.vx = 0; + self.vy = -250; + } + pub fn update(self: *Ball) void { + self.vy += utils.gravity; + self.x += self.vx * utils.frequency; + self.y += self.vy * utils.frequency; + // collisions handling + if (self.x < 0) { // left wall + self.vx = -self.vx * utils.bounce; + self.x = 0; + } + if (self.x >= 160 - ball_width) { // right wall + self.vx = -self.vx * utils.bounce; + self.x = 160 - ball_width - 1; + } + if (self.y < 0) { // ceiling + self.vy = -self.vy * utils.bounce; + self.y = 0; + } + if (self.y >= 160 - ball_height) { // floor + self.vy = 0; + self.y = 160 - ball_height; + } } }; |