aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJulien Dessaux2022-09-29 22:03:25 +0200
committerJulien Dessaux2022-09-29 22:03:25 +0200
commit2f76bca8dc84525f18df8b0658176cf806815b15 (patch)
tree342c49f27bf662381a81d352ddffd7b5b83c0d1f /src
parentImproved net collisions (diff)
downloadgrenade-brothers-2f76bca8dc84525f18df8b0658176cf806815b15.tar.gz
grenade-brothers-2f76bca8dc84525f18df8b0658176cf806815b15.tar.bz2
grenade-brothers-2f76bca8dc84525f18df8b0658176cf806815b15.zip
Added end of round detection and scoring
Diffstat (limited to '')
-rw-r--r--src/ball.zig8
-rw-r--r--src/game.zig13
-rw-r--r--src/main.zig13
3 files changed, 30 insertions, 4 deletions
diff --git a/src/ball.zig b/src/ball.zig
index 9603d9a..a48fe9f 100644
--- a/src/ball.zig
+++ b/src/ball.zig
@@ -20,7 +20,7 @@ pub const Ball = struct {
self.vx = 0;
self.vy = -250;
}
- pub fn update(self: *Ball) void {
+ pub fn update(self: *Ball) ?utils.side {
self.vy += utils.gravity;
self.x += self.vx * utils.frequency;
self.y += self.vy * utils.frequency;
@@ -40,6 +40,11 @@ pub const Ball = struct {
if (self.y >= 160 - ball_height) { // floor
self.vy = 0;
self.y = 160 - ball_height;
+ if (self.x <= 80) {
+ return .left;
+ } else {
+ return .right;
+ }
}
// Net collision left
var x1: f64 = 78 - ball_width;
@@ -85,6 +90,7 @@ pub const Ball = struct {
self.x = x2;
}
}
+ return null;
}
};
diff --git a/src/game.zig b/src/game.zig
index 9597a83..9b8086d 100644
--- a/src/game.zig
+++ b/src/game.zig
@@ -37,13 +37,22 @@ pub const Game = struct {
self.brothers[0].resetRound();
self.brothers[1].resetRound();
}
- pub fn update(self: *Game) void {
+ pub fn update(self: *Game) bool {
self.gamepads[0].update(w4.GAMEPAD1.*);
self.gamepads[1].update(w4.GAMEPAD2.*);
self.brothers[0].update(self.gamepads[0]);
self.brothers[1].update(self.gamepads[1]);
- self.ball.update();
+ const finished = self.ball.update();
self.brothers[0].collide(&self.ball);
self.brothers[1].collide(&self.ball);
+ if (finished) |side| {
+ if (side == .left) {
+ self.brothers[1].score += 1;
+ } else {
+ self.brothers[0].score += 1;
+ }
+ return true;
+ }
+ return false;
}
};
diff --git a/src/main.zig b/src/main.zig
index 1b051fc..f24057b 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -5,12 +5,23 @@ const w4 = @import("wasm4.zig");
//----- Globals ---------------------------------------------------------------
var Game: game.Game = undefined;
+var wait_before_new_round: u8 = 0;
export fn start() void {
Game.reset();
}
export fn update() void {
- Game.update();
+ if (wait_before_new_round == 0) {
+ const finished = Game.update();
+ if (finished) {
+ wait_before_new_round = 60;
+ }
+ } else {
+ wait_before_new_round -= 1;
+ if (wait_before_new_round == 0) {
+ Game.resetRound();
+ }
+ }
Game.draw();
}