Drop Timer

This commit is contained in:
Dusk 2022-07-10 19:44:48 +02:00
parent 516efd17e2
commit 89afe54e37
4 changed files with 47 additions and 12 deletions

View File

@ -89,6 +89,11 @@ pub fn init(_renderer: *SDL.SDL_Renderer) Self {
pub fn tick(self: *Self) void { pub fn tick(self: *Self) void {
// TIMERS // 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.action_list[@enumToInt(Action.Offset.down)].holding) {
if (!self.timer_down.started) { if (!self.timer_down.started) {
self.timer_down.start(); self.timer_down.start();
@ -100,6 +105,14 @@ pub fn tick(self: *Self) void {
self.timer_down.stop(); 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 // KEY EVENTS
var key_state = SDL.SDL_GetKeyboardState(null); 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 // Update Shadow
{ {
self.shadow = movement.shadow(self.grid, self.piece); self.shadow = movement.shadow(self.grid, self.piece);

View File

@ -2,12 +2,28 @@ const std = @import("std");
const Grid = @import("grid.zig"); const Grid = @import("grid.zig");
const Piece = @import("piece.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 { pub fn moveRight(grid: Grid, piece: Piece) Piece {
var new_piece = piece; var new_piece = piece;
new_piece.col += 1; new_piece.col += 1;
if (checkCollision(grid, new_piece)) { if (checkCollision(grid, new_piece)) {
return piece; return piece;
} else { } else {
new_piece = checkDrop(grid, new_piece);
return new_piece; return new_piece;
} }
} }
@ -18,6 +34,7 @@ pub fn moveLeft(grid: Grid, piece: Piece) Piece {
if (checkCollision(grid, new_piece)) { if (checkCollision(grid, new_piece)) {
return piece; return piece;
} else { } else {
new_piece = checkDrop(grid, new_piece);
return new_piece; return new_piece;
} }
} }
@ -27,16 +44,19 @@ pub fn moveDown(grid: Grid, piece: Piece) Piece {
new_piece.row += 1; new_piece.row += 1;
if (checkCollision(grid, new_piece)) { if (checkCollision(grid, new_piece)) {
new_piece.row -= 1; new_piece.row -= 1;
new_piece.dropped = true; if (!new_piece.timer_dropped.started) {
new_piece.timer_dropped.start();
}
return new_piece; return new_piece;
} else { } else {
new_piece.timer_dropped.stop();
return new_piece; return new_piece;
} }
} }
pub fn hardDrop(grid: Grid, piece: Piece) Piece { pub fn hardDrop(grid: Grid, piece: Piece) Piece {
var new_piece = piece; var new_piece = piece;
while (!new_piece.dropped) { while (!new_piece.timer_dropped.started) {
new_piece = moveDown(grid, new_piece); new_piece = moveDown(grid, new_piece);
} }
return new_piece; return new_piece;
@ -76,14 +96,14 @@ fn checkCollision(grid: Grid, piece: Piece) bool {
pub fn rotateLeft(grid: Grid, piece: Piece) Piece { pub fn rotateLeft(grid: Grid, piece: Piece) Piece {
var new_piece = piece.rotate(Piece.Rot.left); var new_piece = piece.rotate(Piece.Rot.left);
new_piece = kick(grid, new_piece, piece); new_piece = kick(grid, new_piece, piece);
std.debug.print("{}\n", .{new_piece.rot_stage}); new_piece = checkDrop(grid, new_piece);
return new_piece; return new_piece;
} }
pub fn rotateRight(grid: Grid, piece: Piece) Piece { pub fn rotateRight(grid: Grid, piece: Piece) Piece {
var new_piece = piece.rotate(Piece.Rot.right); var new_piece = piece.rotate(Piece.Rot.right);
new_piece = kick(grid, new_piece, piece); new_piece = kick(grid, new_piece, piece);
std.debug.print("{}\n", .{new_piece.rot_stage}); new_piece = checkDrop(grid, new_piece);
return new_piece; return new_piece;
} }

View File

@ -3,6 +3,7 @@ const std = @import("std");
const Color = @import("color.zig"); const Color = @import("color.zig");
const Grid = @import("grid.zig"); const Grid = @import("grid.zig");
const Timer = @import("timer.zig");
const Self = @This(); const Self = @This();
@ -41,6 +42,8 @@ structure: [4][4]bool,
// Rotation Stage // Rotation Stage
rot_stage: RotStage = RotStage.init, rot_stage: RotStage = RotStage.init,
// Timer to drop the piece
timer_dropped: Timer,
// Should this be dropped and become part of the grid (flag) // Should this be dropped and become part of the grid (flag)
dropped: bool = false, dropped: bool = false,
// Has the piece been swapped? // Has the piece been swapped?
@ -55,6 +58,8 @@ pub fn init(piece_type: Type) Self {
.col = (Grid.ncolumns / 2) - (ncols / 2), .col = (Grid.ncolumns / 2) - (ncols / 2),
.piece_type = piece_type, .piece_type = piece_type,
.timer_dropped = Timer.init(500),
.structure = switch (piece_type) { .structure = switch (piece_type) {
Type.o => .{ Type.o => .{
.{ false, true, true, false }, .{ false, true, true, false },

View File

@ -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 { pub fn stop(self: *Self) void {
self.started = false; self.started = false;
} }