From df495eb608eeb0a8b5a3f6a24a292aefbf98dd15 Mon Sep 17 00:00:00 2001 From: Strangedusk Date: Tue, 12 Jul 2022 16:20:15 +0200 Subject: [PATCH] Change some names to coincide with zig style --- src/{bag.zig => Bag.zig} | 2 +- src/{cell.zig => Cell.zig} | 0 src/{game.zig => Game.zig} | 20 +++---- src/{grid.zig => Grid.zig} | 6 +- src/MainMenu.zig | 0 src/{piece.zig => Piece.zig} | 21 +++---- src/{timer.zig => Timer.zig} | 0 src/main.zig | 2 +- src/movement.zig | 105 ++++++++++++++++++++++++++++++----- src/renderer.zig | 11 ++-- 10 files changed, 122 insertions(+), 45 deletions(-) rename src/{bag.zig => Bag.zig} (97%) rename src/{cell.zig => Cell.zig} (100%) rename src/{game.zig => Game.zig} (93%) rename src/{grid.zig => Grid.zig} (90%) create mode 100644 src/MainMenu.zig rename src/{piece.zig => Piece.zig} (94%) rename src/{timer.zig => Timer.zig} (100%) diff --git a/src/bag.zig b/src/Bag.zig similarity index 97% rename from src/bag.zig rename to src/Bag.zig index 76f3e3a..fba1206 100644 --- a/src/bag.zig +++ b/src/Bag.zig @@ -1,6 +1,6 @@ const std = @import("std"); -const Piece = @import("piece.zig"); +const Piece = @import("Piece.zig"); const Self = @This(); diff --git a/src/cell.zig b/src/Cell.zig similarity index 100% rename from src/cell.zig rename to src/Cell.zig diff --git a/src/game.zig b/src/Game.zig similarity index 93% rename from src/game.zig rename to src/Game.zig index 2bfd3bb..a8f47c4 100644 --- a/src/game.zig +++ b/src/Game.zig @@ -1,10 +1,10 @@ const std = @import("std"); const SDL = @import("sdl2"); -const Grid = @import("grid.zig"); -const Piece = @import("piece.zig"); -const Bag = @import("bag.zig"); -const Timer = @import("timer.zig"); +const Grid = @import("Grid.zig"); +const Piece = @import("Piece.zig"); +const Bag = @import("Bag.zig"); +const Timer = @import("Timer.zig"); const renderer = @import("renderer.zig"); const movement = @import("movement.zig"); @@ -15,7 +15,7 @@ const Action = struct { activate: bool = false, holding: bool = false, code: u8, - callback: fn(*Self)void, + callback: fn (*Self) void, pub const Offset = enum(usize) { right = 0, left = 1, @@ -27,7 +27,6 @@ const Action = struct { }; }; - grid: Grid, grid_cell_size: i32, grid_pos_x: i32, @@ -90,8 +89,8 @@ pub fn tick(self: *Self) void { // TIMERS if (self.piece.timer_dropped.finished()) { - self.piece.timer_dropped.stop(); - self.piece.dropped = true; + self.piece.timer_dropped.stop(); + self.piece.dropped = true; } if (self.action_list[@enumToInt(Action.Offset.down)].holding) { @@ -132,15 +131,14 @@ pub fn tick(self: *Self) void { } } - // Update Shadow { self.shadow = movement.shadow(self.grid, self.piece); self.shadow.color.a /= 4; - - self.grid.clearLines(); } + self.grid.clearLines(); + renderer.render(self.*); } diff --git a/src/grid.zig b/src/Grid.zig similarity index 90% rename from src/grid.zig rename to src/Grid.zig index 1dad378..58d6622 100644 --- a/src/grid.zig +++ b/src/Grid.zig @@ -1,8 +1,8 @@ const std = @import("std"); const SDL = @import("sdl2"); -const Color = @import("color.zig"); -const Cell = @import("cell.zig"); +const color = @import("color.zig"); +const Cell = @import("Cell.zig"); const utils = @import("utils.zig"); const range = utils.range; @@ -69,7 +69,7 @@ pub fn render(self: Self, renderer: *SDL.SDL_Renderer, cell_size: i32, pos_x: i3 _ = SDL.SDL_SetRenderDrawColor(renderer, r, g, b, a); _ = SDL.SDL_RenderFillRect(renderer, &rect); - _ = SDL.SDL_SetRenderDrawColor(renderer, Color.light_grey.r, Color.light_grey.g, Color.light_grey.b, Color.light_grey.a); + _ = SDL.SDL_SetRenderDrawColor(renderer, color.light_grey.r, color.light_grey.g, color.light_grey.b, color.light_grey.a); _ = SDL.SDL_RenderDrawRect(renderer, &rect); } } diff --git a/src/MainMenu.zig b/src/MainMenu.zig new file mode 100644 index 0000000..e69de29 diff --git a/src/piece.zig b/src/Piece.zig similarity index 94% rename from src/piece.zig rename to src/Piece.zig index 91b3db9..e1178d9 100644 --- a/src/piece.zig +++ b/src/Piece.zig @@ -1,9 +1,10 @@ const SDL = @import("sdl2"); const std = @import("std"); -const Color = @import("color.zig"); -const Grid = @import("grid.zig"); -const Timer = @import("timer.zig"); +const Grid = @import("Grid.zig"); +const Timer = @import("Timer.zig"); + +const color = @import("color.zig"); const Self = @This(); @@ -106,13 +107,13 @@ pub fn init(piece_type: Type) Self { }, .color = switch (piece_type) { - Type.o => Color.yellow, - Type.i => Color.cyan, - Type.l => Color.orange, - Type.j => Color.blue, - Type.s => Color.green, - Type.z => Color.red, - Type.t => Color.purple, + Type.o => color.yellow, + Type.i => color.cyan, + Type.l => color.orange, + Type.j => color.blue, + Type.s => color.green, + Type.z => color.red, + Type.t => color.purple, }, }; } diff --git a/src/timer.zig b/src/Timer.zig similarity index 100% rename from src/timer.zig rename to src/Timer.zig diff --git a/src/main.zig b/src/main.zig index 85ec9fa..2314476 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,7 +1,7 @@ const std = @import("std"); const SDL = @import("sdl2"); -const Game = @import("game.zig"); +const Game = @import("Game.zig"); pub fn main() !void { const stderr = std.io.getStdErr(); diff --git a/src/movement.zig b/src/movement.zig index 798ff57..214725d 100644 --- a/src/movement.zig +++ b/src/movement.zig @@ -1,10 +1,10 @@ const std = @import("std"); -const Grid = @import("grid.zig"); -const Piece = @import("piece.zig"); +const Grid = @import("Grid.zig"); +const Piece = @import("Piece.zig"); -fn checkDrop (grid: Grid, piece: Piece) Piece { +fn checkDrop(grid: Grid, piece: Piece) Piece { var new_piece = piece; - if(moveDown(grid, piece).timer_dropped.started) { + if (moveDown(grid, piece).timer_dropped.started) { if (new_piece.timer_dropped.started) { new_piece.timer_dropped.reset(); } else { @@ -17,7 +17,6 @@ fn checkDrop (grid: Grid, piece: Piece) Piece { } pub fn moveRight(grid: Grid, piece: Piece) Piece { - var new_piece = piece; new_piece.col += 1; if (checkCollision(grid, new_piece)) { @@ -86,7 +85,13 @@ pub fn shadow(grid: Grid, piece: Piece) Piece { fn checkCollision(grid: Grid, piece: Piece) bool { for (piece.structure) |_, y| { for (piece.structure[y]) |_, x| { - if (piece.structure[y][x] and ((@intCast(i32, x) + piece.col > Grid.ncolumns - 1) or (@intCast(i32, x) + piece.col < 0) or (@intCast(i32, y) + piece.row > Grid.nrows - 1) or (@intCast(i32, y) + piece.row < 0) or (!grid.cells[@intCast(usize, piece.row + @intCast(i32, y))][@intCast(usize, piece.col + @intCast(i32, x))].free))) { + if (piece.structure[y][x] and + ((@intCast(i32, x) + piece.col > Grid.ncolumns - 1) or + (@intCast(i32, x) + piece.col < 0) or + (@intCast(i32, y) + piece.row > Grid.nrows - 1) or + (@intCast(i32, y) + piece.row < 0) or + (!grid.cells[@intCast(usize, piece.row + @intCast(i32, y))][@intCast(usize, piece.col + @intCast(i32, x))].free))) + { return true; } } @@ -117,26 +122,54 @@ pub fn kick(grid: Grid, piece: Piece, prev_piece: Piece) Piece { var new_piece = piece; // Test 1 - if (!checkCollision(grid, new_piece)) return new_piece; + if (!checkCollision(grid, new_piece)) { + // T TWIST DETECTION + _ = checkTTwist(grid, piece); + return new_piece; + } var offsets: [4][2]i8 = undefined; if (piece.piece_type == Piece.Type.i) { - if ((prev_stage == Piece.RotStage.init and next_stage == Piece.RotStage.right) or (prev_stage == Piece.RotStage.left and next_stage == Piece.RotStage.flip)) { + if ((prev_stage == Piece.RotStage.init and + next_stage == Piece.RotStage.right) or + (prev_stage == Piece.RotStage.left and + next_stage == Piece.RotStage.flip)) + { offsets = .{ .{ -2, 0 }, .{ 1, 0 }, .{ -2, 1 }, .{ 1, -2 } }; - } else if ((prev_stage == Piece.RotStage.right and next_stage == Piece.RotStage.init) or (prev_stage == Piece.RotStage.flip and next_stage == Piece.RotStage.left)) { + } else if ((prev_stage == Piece.RotStage.right and + next_stage == Piece.RotStage.init) or + (prev_stage == Piece.RotStage.flip and + next_stage == Piece.RotStage.left)) + { offsets = .{ .{ 2, 0 }, .{ -1, 0 }, .{ 2, -1 }, .{ -1, 2 } }; - } else if ((prev_stage == Piece.RotStage.right and next_stage == Piece.RotStage.flip) or (prev_stage == Piece.RotStage.init and next_stage == Piece.RotStage.left)) { + } else if ((prev_stage == Piece.RotStage.right and + next_stage == Piece.RotStage.flip) or + (prev_stage == Piece.RotStage.init and + next_stage == Piece.RotStage.left)) + { offsets = .{ .{ -1, 0 }, .{ 2, 0 }, .{ -1, -2 }, .{ 2, 1 } }; } else { offsets = .{ .{ 1, 0 }, .{ -2, 0 }, .{ 1, 2 }, .{ -2, -1 } }; } } else { - if ((prev_stage == Piece.RotStage.init and next_stage == Piece.RotStage.right) or (prev_stage == Piece.RotStage.flip and next_stage == Piece.RotStage.right)) { + if ((prev_stage == Piece.RotStage.init and + next_stage == Piece.RotStage.right) or + (prev_stage == Piece.RotStage.flip and + next_stage == Piece.RotStage.right)) + { offsets = .{ .{ -1, 0 }, .{ -1, -1 }, .{ 0, 2 }, .{ -1, 2 } }; - } else if ((prev_stage == Piece.RotStage.right and next_stage == Piece.RotStage.init) or (prev_stage == Piece.RotStage.right and next_stage == Piece.RotStage.flip)) { + } else if ((prev_stage == Piece.RotStage.right and + next_stage == Piece.RotStage.init) or + (prev_stage == Piece.RotStage.right and + next_stage == Piece.RotStage.flip)) + { offsets = .{ .{ 1, 0 }, .{ 1, 1 }, .{ 0, -2 }, .{ 1, -2 } }; - } else if ((prev_stage == Piece.RotStage.flip and next_stage == Piece.RotStage.left) or (prev_stage == Piece.RotStage.init and next_stage == Piece.RotStage.left)) { + } else if ((prev_stage == Piece.RotStage.flip and + next_stage == Piece.RotStage.left) or + (prev_stage == Piece.RotStage.init and + next_stage == Piece.RotStage.left)) + { offsets = .{ .{ 1, 0 }, .{ 1, -1 }, .{ 0, 2 }, .{ 1, 2 } }; } else { offsets = .{ .{ -1, 0 }, .{ -1, 1 }, .{ 0, -2 }, .{ -1, -2 } }; @@ -148,10 +181,54 @@ pub fn kick(grid: Grid, piece: Piece, prev_piece: Piece) Piece { for (offsets) |offset| { new_piece.col += offset[0]; new_piece.row += offset[1]; - if (!checkCollision(grid, new_piece)) return new_piece; + if (!checkCollision(grid, new_piece)) { + // T TWIST DETECTION + _ = checkTTwist(grid, piece); + return new_piece; + } new_piece.col -= offset[0]; new_piece.row -= offset[1]; } return prev_piece; } + +fn checkTTwist(grid: Grid, piece: Piece) bool { + if (piece.piece_type == Piece.Type.t) { + var diagonals: u8 = 0; + + // [x][-][-] + // [-][-][-] + // [-][-][-] + // + // [-][-][-] + // [-][-][-] + // [x][-][-] + // + // [-][-][x] + // [-][-][-] + // [-][-][-] + // + // [-][-][-] + // [-][-][-] + // [-][-][x] + // + const rows = [_]i32{ 0, 2, 0, 2 }; + const cols = [_]i32{ 0, 0, 2, 2 }; + for (rows) |_, i| { + var row = piece.row + rows[i]; + var col = piece.col + cols[i]; + if ((row > 0 and row < Grid.nrows and col > 0 and col < Grid.ncolumns) and + !grid.cells[@intCast(usize, row)][@intCast(usize, col)].free) + { + std.debug.print("Hit\n", .{}); + diagonals += 1; + } + } + if (diagonals > 2) { + std.debug.print("T-Twist\n", .{}); + return true; + } + } + return false; +} diff --git a/src/renderer.zig b/src/renderer.zig index a76bd52..feed79d 100644 --- a/src/renderer.zig +++ b/src/renderer.zig @@ -1,10 +1,11 @@ const SDL = @import("sdl2"); -const Game = @import("game.zig"); -const Bag = @import("bag.zig"); -const Grid = @import("grid.zig"); -const Piece = @import("piece.zig"); -const Color = @import("color.zig"); +const Game = @import("Game.zig"); +const Bag = @import("Bag.zig"); +const Grid = @import("Grid.zig"); +const Piece = @import("Piece.zig"); + +const color = @import("color.zig"); pub fn renderBag(game: Game) void { const pos_x = game.grid_pos_x + ((Grid.ncolumns + 1) * game.grid_cell_size);