Drop Timer
This commit is contained in:
parent
516efd17e2
commit
89afe54e37
21
src/game.zig
21
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);
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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 },
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue