Change some names to coincide with zig style

This commit is contained in:
Dusk 2022-07-12 16:20:15 +02:00
parent af1c24853f
commit df495eb608
10 changed files with 122 additions and 45 deletions

View File

@ -1,6 +1,6 @@
const std = @import("std");
const Piece = @import("piece.zig");
const Piece = @import("Piece.zig");
const Self = @This();

View File

@ -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.*);
}

View File

@ -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);
}
}

0
src/MainMenu.zig Normal file
View File

View File

@ -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,
},
};
}

View File

@ -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();

View File

@ -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;
}

View File

@ -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);