aboutsummaryrefslogtreecommitdiff
path: root/src/brothers.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/brothers.zig')
-rw-r--r--src/brothers.zig52
1 files changed, 43 insertions, 9 deletions
diff --git a/src/brothers.zig b/src/brothers.zig
index b221ddc..cf78988 100644
--- a/src/brothers.zig
+++ b/src/brothers.zig
@@ -8,6 +8,8 @@ pub const Side = enum(u1) {
const startingX = [2]f64{ 15, 60 };
const colors = [2]spoon.Attribute.Colour{ .blue, .red };
+const leftLimit = [2]f64{ 1, 40 };
+const rightLimit = [2]f64{ 33, 74 }; // (38, 79) minus a brother's width
pub const Brother = struct {
side: Side,
@@ -15,15 +17,7 @@ pub const Brother = struct {
y: f64,
dx: f64,
dy: f64,
- pub fn reset(self: *Brother, side: ?Side) void {
- if (side) |s| {
- self.side = s;
- }
- self.x = startingX[@enumToInt(self.side)];
- self.y = 17;
- self.dx = 0;
- self.dy = 0;
- }
+ moveDuration: u64,
pub fn draw(self: Brother, rc: *spoon.Term.RenderContext) !void {
try rc.setAttribute(.{ .fg = colors[@enumToInt(self.side)] });
var iter = std.mem.split(u8, brother, "\n");
@@ -34,6 +28,46 @@ pub const Brother = struct {
_ = try rc.buffer.writer().write(line);
}
}
+ pub fn moveLeft(self: *Brother) void {
+ self.dx -= 5 / (1000 / 60.0);
+ self.moveDuration = 24;
+ }
+ pub fn moveRight(self: *Brother) void {
+ self.dx += 5 / (1000 / 60.0);
+ self.moveDuration = 24;
+ }
+ pub fn step(self: *Brother) void {
+ const x = self.x + self.dx;
+ const ll = leftLimit[@enumToInt(self.side)];
+ const rl = rightLimit[@enumToInt(self.side)];
+ if (x < ll) {
+ self.x = ll;
+ self.dx = 0;
+ self.moveDuration = 0;
+ } else if (x > rl) {
+ self.x = rl;
+ self.dx = 0;
+ self.moveDuration = 0;
+ } else {
+ self.x = x;
+ if (self.moveDuration > 0) {
+ self.moveDuration -= 1;
+ if (self.moveDuration == 0) {
+ self.dx = 0;
+ }
+ }
+ }
+ }
+ pub fn reset(self: *Brother, side: ?Side) void {
+ if (side) |s| {
+ self.side = s;
+ }
+ self.x = startingX[@enumToInt(self.side)];
+ self.y = 17;
+ self.dx = 0;
+ self.dy = 0;
+ self.moveDuration = 0;
+ }
};
const brother =