usg/src/movement.zig

57 lines
1.9 KiB
Zig
Raw Normal View History

2022-06-28 12:42:28 +00:00
const Grid = @import("grid.zig");
const Piece = @import("piece.zig");
pub fn moveRight(grid: Grid, piece: Piece) Piece {
var new_piece = piece;
new_piece.col += 1;
for (new_piece.structure) |_, y| {
for (new_piece.structure[y]) |_, x| {
if (new_piece.structure[y][x] and
@intCast(i32, x) + new_piece.col > Grid.ncolumns - 1) {
return piece;
}
if (new_piece.structure[y][x] and
!grid.cells[@intCast(usize, new_piece.row + @intCast(i32, y))][@intCast(usize, new_piece.col + @intCast(i32, x))].free) {
return piece;
}
}
}
return new_piece;
}
pub fn moveLeft(grid: Grid, piece: Piece) Piece {
var new_piece = piece;
new_piece.col -= 1;
for (new_piece.structure) |_, y| {
for (new_piece.structure[y]) |_, x| {
if (new_piece.structure[y][x] and
@intCast(i32, x) + new_piece.col < 0) {
return piece;
}
if (new_piece.structure[y][x] and
!grid.cells[@intCast(usize, new_piece.row + @intCast(i32, y))][@intCast(usize, new_piece.col + @intCast(i32, x))].free) {
return piece;
}
}
}
return new_piece;
}
pub fn moveDown(grid: Grid, piece: Piece) Piece {
var new_piece = piece;
new_piece.row += 1;
for (new_piece.structure) |_, y| {
for (new_piece.structure[y]) |_, x| {
if (new_piece.structure[y][x] and
@intCast(i32, y) + new_piece.row > Grid.nrows - 1) {
return piece;
}
if (new_piece.structure[y][x] and
!grid.cells[@intCast(usize, new_piece.row + @intCast(i32, y))][@intCast(usize, new_piece.col + @intCast(i32, x))].free) {
return piece;
}
}
}
return new_piece;
}