From 89afe54e370d7780dd58e17975b3dd00ef615a07 Mon Sep 17 00:00:00 2001 From: Strangedusk Date: Sun, 10 Jul 2022 19:44:48 +0200 Subject: [PATCH] Drop Timer --- src/game.zig | 21 +++++++++++++-------- src/movement.zig | 28 ++++++++++++++++++++++++---- src/piece.zig | 5 +++++ src/timer.zig | 5 +++++ 4 files changed, 47 insertions(+), 12 deletions(-) diff --git a/src/game.zig b/src/game.zig index 8ac1923..2bfd3bb 100644 --- a/src/game.zig +++ b/src/game.zig @@ -89,6 +89,11 @@ pub fn init(_renderer: *SDL.SDL_Renderer) Self { pub fn tick(self: *Self) void { // TIMERS + if (self.piece.timer_dropped.finished()) { + self.piece.timer_dropped.stop(); + self.piece.dropped = true; + } + if (self.action_list[@enumToInt(Action.Offset.down)].holding) { if (!self.timer_down.started) { self.timer_down.start(); @@ -100,6 +105,14 @@ pub fn tick(self: *Self) void { self.timer_down.stop(); } + // DROP + + if (self.piece.dropped) { + self.grid = movement.drop(self.grid, self.piece); + // New Piece + self.piece = self.bag.pop(); + } + // KEY EVENTS var key_state = SDL.SDL_GetKeyboardState(null); @@ -120,14 +133,6 @@ pub fn tick(self: *Self) void { } - // DROP - - if (self.piece.dropped) { - self.grid = movement.drop(self.grid, self.piece); - // New Piece - self.piece = self.bag.pop(); - } - // Update Shadow { self.shadow = movement.shadow(self.grid, self.piece); diff --git a/src/movement.zig b/src/movement.zig index 926b9e7..f7f0acd 100644 --- a/src/movement.zig +++ b/src/movement.zig @@ -2,12 +2,28 @@ const std = @import("std"); const Grid = @import("grid.zig"); const Piece = @import("piece.zig"); +fn checkDrop (grid: Grid, piece: Piece) Piece { + var new_piece = piece; + if(moveDown(grid, piece).timer_dropped.started) { + if (new_piece.timer_dropped.started) { + new_piece.timer_dropped.reset(); + } else { + new_piece.timer_dropped.start(); + } + } else { + new_piece.timer_dropped.stop(); + } + return new_piece; +} + pub fn moveRight(grid: Grid, piece: Piece) Piece { + var new_piece = piece; new_piece.col += 1; if (checkCollision(grid, new_piece)) { return piece; } else { + new_piece = checkDrop(grid, new_piece); return new_piece; } } @@ -18,6 +34,7 @@ pub fn moveLeft(grid: Grid, piece: Piece) Piece { if (checkCollision(grid, new_piece)) { return piece; } else { + new_piece = checkDrop(grid, new_piece); return new_piece; } } @@ -27,16 +44,19 @@ pub fn moveDown(grid: Grid, piece: Piece) Piece { new_piece.row += 1; if (checkCollision(grid, new_piece)) { new_piece.row -= 1; - new_piece.dropped = true; + if (!new_piece.timer_dropped.started) { + new_piece.timer_dropped.start(); + } return new_piece; } else { + new_piece.timer_dropped.stop(); return new_piece; } } pub fn hardDrop(grid: Grid, piece: Piece) Piece { var new_piece = piece; - while (!new_piece.dropped) { + while (!new_piece.timer_dropped.started) { new_piece = moveDown(grid, new_piece); } return new_piece; @@ -76,14 +96,14 @@ fn checkCollision(grid: Grid, piece: Piece) bool { pub fn rotateLeft(grid: Grid, piece: Piece) Piece { var new_piece = piece.rotate(Piece.Rot.left); new_piece = kick(grid, new_piece, piece); - std.debug.print("{}\n", .{new_piece.rot_stage}); + new_piece = checkDrop(grid, new_piece); return new_piece; } pub fn rotateRight(grid: Grid, piece: Piece) Piece { var new_piece = piece.rotate(Piece.Rot.right); new_piece = kick(grid, new_piece, piece); - std.debug.print("{}\n", .{new_piece.rot_stage}); + new_piece = checkDrop(grid, new_piece); return new_piece; } diff --git a/src/piece.zig b/src/piece.zig index 693a436..91b3db9 100644 --- a/src/piece.zig +++ b/src/piece.zig @@ -3,6 +3,7 @@ const std = @import("std"); const Color = @import("color.zig"); const Grid = @import("grid.zig"); +const Timer = @import("timer.zig"); const Self = @This(); @@ -41,6 +42,8 @@ structure: [4][4]bool, // Rotation Stage rot_stage: RotStage = RotStage.init, +// Timer to drop the piece +timer_dropped: Timer, // Should this be dropped and become part of the grid (flag) dropped: bool = false, // Has the piece been swapped? @@ -55,6 +58,8 @@ pub fn init(piece_type: Type) Self { .col = (Grid.ncolumns / 2) - (ncols / 2), .piece_type = piece_type, + .timer_dropped = Timer.init(500), + .structure = switch (piece_type) { Type.o => .{ .{ false, true, true, false }, diff --git a/src/timer.zig b/src/timer.zig index 85b4fa2..01691a3 100644 --- a/src/timer.zig +++ b/src/timer.zig @@ -33,6 +33,11 @@ pub fn finished(self: *Self) bool { } } +pub fn reset(self: *Self) void { + self.progress = 0; + self.initial = SDL.SDL_GetTicks64(); +} + pub fn stop(self: *Self) void { self.started = false; }