From a9c30f6236af36f6c428b0b10dd4d48a21c70733 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Sun, 14 Aug 2022 15:36:59 +0200 Subject: Implement left and right movements --- src/brothers.zig | 52 +++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 43 insertions(+), 9 deletions(-) (limited to 'src/brothers.zig') 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 = -- cgit v1.2.3