Implemented a ball with basic physics and collisions
This commit is contained in:
parent
94008bfbe3
commit
a899bc060b
2 changed files with 37 additions and 3 deletions
|
@ -1,6 +1,8 @@
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const spoon = @import("spoon");
|
const spoon = @import("spoon");
|
||||||
|
|
||||||
|
const ball = @import("ball.zig");
|
||||||
|
|
||||||
pub const Side = enum(u1) {
|
pub const Side = enum(u1) {
|
||||||
left,
|
left,
|
||||||
right,
|
right,
|
||||||
|
@ -41,7 +43,7 @@ pub const Brother = struct {
|
||||||
self.dx += 5 / (1000 / 60.0);
|
self.dx += 5 / (1000 / 60.0);
|
||||||
self.moveDuration = 24;
|
self.moveDuration = 24;
|
||||||
}
|
}
|
||||||
pub fn step(self: *Brother) void {
|
pub fn step(self: *Brother, b: *ball.Ball) void {
|
||||||
// Horizontal movement
|
// Horizontal movement
|
||||||
const x = self.x + self.dx;
|
const x = self.x + self.dx;
|
||||||
const ll = leftLimit[@enumToInt(self.side)];
|
const ll = leftLimit[@enumToInt(self.side)];
|
||||||
|
@ -74,6 +76,33 @@ pub const Brother = struct {
|
||||||
} else {
|
} else {
|
||||||
self.y = y;
|
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 {
|
pub fn reset(self: *Brother, side: ?Side) void {
|
||||||
if (side) |s| {
|
if (side) |s| {
|
||||||
|
|
|
@ -1,16 +1,19 @@
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const spoon = @import("spoon");
|
const spoon = @import("spoon");
|
||||||
|
|
||||||
|
const ball = @import("ball.zig");
|
||||||
const brothers = @import("brothers.zig");
|
const brothers = @import("brothers.zig");
|
||||||
const playfield = @import("playfield.zig");
|
const playfield = @import("playfield.zig");
|
||||||
|
|
||||||
pub const Game = struct {
|
pub const Game = struct {
|
||||||
|
ball: ball.Ball = undefined,
|
||||||
brothers: [2]brothers.Brother = undefined,
|
brothers: [2]brothers.Brother = undefined,
|
||||||
side: brothers.Side = undefined,
|
side: brothers.Side = undefined,
|
||||||
pub fn draw(self: Game, rc: *spoon.Term.RenderContext) !void {
|
pub fn draw(self: Game, rc: *spoon.Term.RenderContext) !void {
|
||||||
try playfield.draw(rc);
|
try playfield.draw(rc);
|
||||||
try self.brothers[0].draw(rc);
|
try self.brothers[0].draw(rc);
|
||||||
try self.brothers[1].draw(rc);
|
try self.brothers[1].draw(rc);
|
||||||
|
try self.ball.draw(rc);
|
||||||
}
|
}
|
||||||
pub fn moveJump(self: *Game, side: brothers.Side) void {
|
pub fn moveJump(self: *Game, side: brothers.Side) void {
|
||||||
self.brothers[@enumToInt(side)].moveJump();
|
self.brothers[@enumToInt(side)].moveJump();
|
||||||
|
@ -26,11 +29,13 @@ pub const Game = struct {
|
||||||
self.resetRound();
|
self.resetRound();
|
||||||
}
|
}
|
||||||
pub fn resetRound(self: *Game) void {
|
pub fn resetRound(self: *Game) void {
|
||||||
|
self.ball.reset(.left);
|
||||||
self.brothers[0].reset(.left);
|
self.brothers[0].reset(.left);
|
||||||
self.brothers[1].reset(.right);
|
self.brothers[1].reset(.right);
|
||||||
}
|
}
|
||||||
pub fn step(self: *Game) void {
|
pub fn step(self: *Game) void {
|
||||||
self.brothers[0].step();
|
self.ball.step();
|
||||||
self.brothers[1].step();
|
self.brothers[0].step(&self.ball);
|
||||||
|
self.brothers[1].step(&self.ball);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue