From fb9968c19f96bf3f15654a94aca560be208c0937 Mon Sep 17 00:00:00 2001 From: dusk Date: Fri, 22 Sep 2023 20:30:19 +0200 Subject: [PATCH] Implement timers --- src/Game.zig | 12 ++++++------ src/Game/Piece.zig | 2 +- src/Timer.zig | 20 ++++++++++---------- src/raylib.zig | 4 ++++ 4 files changed, 21 insertions(+), 17 deletions(-) diff --git a/src/Game.zig b/src/Game.zig index 722700c..ddb7fc6 100644 --- a/src/Game.zig +++ b/src/Game.zig @@ -65,12 +65,12 @@ pub fn init(renderer: *Renderer.Renderer) Self { .held = null, .renderer = Renderer.init(renderer), - .timer_down = Timer.init(50), - .timer_left_arr = Timer.init(50), - .timer_left_das = Timer.init(200), - .timer_right_arr = Timer.init(50), - .timer_right_das = Timer.init(200), - .timer_gravity = Timer.init(200), + .timer_down = Timer.init(0.05), + .timer_left_arr = Timer.init(0.05), + .timer_left_das = Timer.init(0.2), + .timer_right_arr = Timer.init(0.05), + .timer_right_das = Timer.init(0.2), + .timer_gravity = Timer.init(0.2), }; // Get a piece from the bag diff --git a/src/Game/Piece.zig b/src/Game/Piece.zig index 7815758..3f7415d 100644 --- a/src/Game/Piece.zig +++ b/src/Game/Piece.zig @@ -58,7 +58,7 @@ pub fn init(piece_type: Type) Self { .col = (Grid.ncolumns / 2) - (ncols / 2), .piece_type = piece_type, - .timer_dropped = Timer.init(500), + .timer_dropped = Timer.init(0.5), .structure = switch (piece_type) { Type.o => .{ diff --git a/src/Timer.zig b/src/Timer.zig index 29c99fd..f4ddc56 100644 --- a/src/Timer.zig +++ b/src/Timer.zig @@ -1,13 +1,13 @@ -//const SDL = @import("sdl2"); - const Self = @This(); -started: bool, -initial: u64, -progress: u64, -target: u64, +const getTime = @import("raylib.zig").getTime; -pub fn init(target: u64) Self { +started: bool, +initial: f64, +progress: f64, +target: f64, + +pub fn init(target: f64) Self { return Self{ .started = false, .initial = 0, @@ -19,12 +19,12 @@ pub fn init(target: u64) Self { pub fn start(self: *Self) void { self.started = true; self.progress = 0; - //self.initial = SDL.getTicks64(); + self.initial = getTime(); } pub fn finished(self: *Self) bool { if (self.started) { - //self.progress = SDL.getTicks64() - self.initial; + self.progress = getTime() - self.initial; if (self.progress >= self.target) { return true; } else return false; @@ -35,7 +35,7 @@ pub fn finished(self: *Self) bool { pub fn reset(self: *Self) void { self.progress = 0; - //self.initial = SDL.getTicks64(); + self.initial = getTime(); } pub fn stop(self: *Self) void { diff --git a/src/raylib.zig b/src/raylib.zig index f20b774..baa4efe 100644 --- a/src/raylib.zig +++ b/src/raylib.zig @@ -73,3 +73,7 @@ pub fn getScreenWidth() i32 { pub fn getScreenHeight() i32 { return c.GetScreenHeight(); } + +pub fn getTime() f64 { + return c.GetTime(); +}