Piece dropping

This commit is contained in:
Dusk 2022-06-28 18:42:40 +02:00
parent 9866839080
commit b64efefea7
12 changed files with 57 additions and 14 deletions

View File

@ -43,10 +43,12 @@ fn render(self: Self) void {
} }
pub fn tick(self: *Self) void { pub fn tick(self: *Self) void {
if (self.piece) |safe_piece| { if (self.piece) |safe_piece| {
std.debug.print("Piece: Col: {} Row: {}\n", .{ safe_piece.col, safe_piece.row }); std.debug.print("Piece: Col: {} Row: {}\n", .{ safe_piece.col, safe_piece.row });
var key_state = SDL.SDL_GetKeyboardState(null); var key_state = SDL.SDL_GetKeyboardState(null);
// MOVEMENT
if (key_state[SDL.SDL_SCANCODE_D] == 1 and !self.right_action) { if (key_state[SDL.SDL_SCANCODE_D] == 1 and !self.right_action) {
self.right_action = true; self.right_action = true;
self.piece = movement.moveRight(self.grid, safe_piece); self.piece = movement.moveRight(self.grid, safe_piece);
@ -70,5 +72,14 @@ pub fn tick(self: *Self) void {
} }
} }
// DROP
if (self.piece) |safe_piece| {
if (safe_piece.dropped) {
self.grid = movement.drop(self.grid, safe_piece);
self.piece = Piece.init(Piece.Type.t);
}
}
self.render(); self.render();
} }

View File

@ -7,11 +7,13 @@ pub fn moveRight(grid: Grid, piece: Piece) Piece {
for (new_piece.structure) |_, y| { for (new_piece.structure) |_, y| {
for (new_piece.structure[y]) |_, x| { for (new_piece.structure[y]) |_, x| {
if (new_piece.structure[y][x] and if (new_piece.structure[y][x] and
@intCast(i32, x) + new_piece.col > Grid.ncolumns - 1) { @intCast(i32, x) + new_piece.col > Grid.ncolumns - 1)
{
return piece; return piece;
} }
if (new_piece.structure[y][x] and 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) { !grid.cells[@intCast(usize, new_piece.row + @intCast(i32, y))][@intCast(usize, new_piece.col + @intCast(i32, x))].free)
{
return piece; return piece;
} }
} }
@ -25,11 +27,13 @@ pub fn moveLeft(grid: Grid, piece: Piece) Piece {
for (new_piece.structure) |_, y| { for (new_piece.structure) |_, y| {
for (new_piece.structure[y]) |_, x| { for (new_piece.structure[y]) |_, x| {
if (new_piece.structure[y][x] and if (new_piece.structure[y][x] and
@intCast(i32, x) + new_piece.col < 0) { @intCast(i32, x) + new_piece.col < 0)
{
return piece; return piece;
} }
if (new_piece.structure[y][x] and 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) { !grid.cells[@intCast(usize, new_piece.row + @intCast(i32, y))][@intCast(usize, new_piece.col + @intCast(i32, x))].free)
{
return piece; return piece;
} }
} }
@ -43,14 +47,35 @@ pub fn moveDown(grid: Grid, piece: Piece) Piece {
for (new_piece.structure) |_, y| { for (new_piece.structure) |_, y| {
for (new_piece.structure[y]) |_, x| { for (new_piece.structure[y]) |_, x| {
if (new_piece.structure[y][x] and if (new_piece.structure[y][x] and
@intCast(i32, y) + new_piece.row > Grid.nrows - 1) { @intCast(i32, y) + new_piece.row > Grid.nrows - 1)
return piece; {
new_piece.row -= 1;
new_piece.dropped = true;
return new_piece;
} }
if (new_piece.structure[y][x] and 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) { !grid.cells[@intCast(usize, new_piece.row + @intCast(i32, y))][@intCast(usize, new_piece.col + @intCast(i32, x))].free)
return piece; {
new_piece.row -= 1;
new_piece.dropped = true;
return new_piece;
} }
} }
} }
return new_piece; return new_piece;
} }
pub fn drop(grid: Grid, piece: Piece) Grid {
var new_grid = grid;
for (piece.structure) |_, y| {
for (piece.structure[y]) |_, x| {
if (piece.structure[y][x]) {
new_grid.cells[@intCast(usize, piece.row + @intCast(i32, y))][@intCast(usize, piece.col + @intCast(i32, x))].free = false;
new_grid.cells[@intCast(usize, piece.row + @intCast(i32, y))][@intCast(usize, piece.col + @intCast(i32, x))].color = piece.color;
}
}
}
return new_grid;
}

View File

@ -18,9 +18,16 @@ pub const Type = enum {
// Positions relative to a grid // Positions relative to a grid
row: i32, row: i32,
col: i32, col: i32,
// 4x4 grid indicating piece form
structure: [4][4]bool, structure: [4][4]bool,
// Should this be dropped and become part of the grid (flag)
dropped: bool = false,
color: SDL.SDL_Color, color: SDL.SDL_Color,
pub fn init(piece_type: Type) Self { pub fn init(piece_type: Type) Self {
return Self{ return Self{
.row = 0, .row = 0,

View File

@ -1,7 +1,7 @@
1271 36569768 1656334617470040282 fdaa7b4da8476360daa4621804b40f8f /home/strange/Documentos/Projectes/Programació/usg/src/main.zig 1271 36569768 1656334617470040282 fdaa7b4da8476360daa4621804b40f8f /home/strange/Documentos/Projectes/Programació/usg/src/main.zig
1271 36569768 1656334617470040282 fdaa7b4da8476360daa4621804b40f8f /home/strange/Documentos/Projectes/Programació/usg/src/main.zig 1271 36569768 1656334617470040282 fdaa7b4da8476360daa4621804b40f8f /home/strange/Documentos/Projectes/Programació/usg/src/main.zig
135300 36570392 1655299923670057346 b4c2d851673df5aab566633b89bdaa84 /home/strange/Documentos/Projectes/Programació/usg/lib/SDL.zig/src/binding/sdl.zig 135300 36570392 1655299923670057346 b4c2d851673df5aab566633b89bdaa84 /home/strange/Documentos/Projectes/Programació/usg/lib/SDL.zig/src/binding/sdl.zig
32 36968726 1656419975960035072 91a8da097c7c1645ba2a6217a5171c4e /home/strange/Documentos/Projectes/Programació/usg/zig-cache/options/kh59p23UXKId3bWMadeQ0Yp6cv2kATA4qPk2nBQ2q_3IsG9JTjKRYS1YxdIMHL6s 32 36968726 1656434162539985452 91a8da097c7c1645ba2a6217a5171c4e /home/strange/Documentos/Projectes/Programació/usg/zig-cache/options/kh59p23UXKId3bWMadeQ0Yp6cv2kATA4qPk2nBQ2q_3IsG9JTjKRYS1YxdIMHL6s
5621 36968525 1655270162000000000 66001e1a55f68e4681c1f32a69223647 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/std.zig 5621 36968525 1655270162000000000 66001e1a55f68e4681c1f32a69223647 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/std.zig
5621 36968525 1655270162000000000 66001e1a55f68e4681c1f32a69223647 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/std.zig 5621 36968525 1655270162000000000 66001e1a55f68e4681c1f32a69223647 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/std.zig
29901 36968437 1655270162000000000 5c441d88acbf5e7fe2a26137df2b5376 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/builtin.zig 29901 36968437 1655270162000000000 5c441d88acbf5e7fe2a26137df2b5376 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/builtin.zig
@ -21,14 +21,14 @@
121502 36968568 1655270162000000000 4bad05f05e4a10364e515a13d2fa2b3f /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/os/linux/io_uring.zig 121502 36968568 1655270162000000000 4bad05f05e4a10364e515a13d2fa2b3f /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/os/linux/io_uring.zig
135300 36570392 1655299923670057346 b4c2d851673df5aab566633b89bdaa84 /home/strange/Documentos/Projectes/Programació/usg/lib/SDL.zig/src/binding/sdl.zig 135300 36570392 1655299923670057346 b4c2d851673df5aab566633b89bdaa84 /home/strange/Documentos/Projectes/Programació/usg/lib/SDL.zig/src/binding/sdl.zig
1640 36570393 1655299923670057346 c3d50ea43c9e10688a521a9ac927ded4 /home/strange/Documentos/Projectes/Programació/usg/lib/SDL.zig/src/binding/sdl_image.zig 1640 36570393 1655299923670057346 c3d50ea43c9e10688a521a9ac927ded4 /home/strange/Documentos/Projectes/Programació/usg/lib/SDL.zig/src/binding/sdl_image.zig
32 36968726 1656419975960035072 91a8da097c7c1645ba2a6217a5171c4e /home/strange/Documentos/Projectes/Programació/usg/zig-cache/options/kh59p23UXKId3bWMadeQ0Yp6cv2kATA4qPk2nBQ2q_3IsG9JTjKRYS1YxdIMHL6s 32 36968726 1656434162539985452 91a8da097c7c1645ba2a6217a5171c4e /home/strange/Documentos/Projectes/Programació/usg/zig-cache/options/kh59p23UXKId3bWMadeQ0Yp6cv2kATA4qPk2nBQ2q_3IsG9JTjKRYS1YxdIMHL6s
7338 36968289 1655270162000000000 ee4fd6c02929ff52d99a8faa2539dce8 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/zig.zig 7338 36968289 1655270162000000000 ee4fd6c02929ff52d99a8faa2539dce8 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/zig.zig
21121 36968483 1655270162000000000 669c911cf26ce07c5f5516d896b30e33 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/zig/c_translation.zig 21121 36968483 1655270162000000000 669c911cf26ce07c5f5516d896b30e33 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/zig/c_translation.zig
113573 36968636 1655270162000000000 2c3980264c753ba754322c7c8c4f2634 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/mem.zig 113573 36968636 1655270162000000000 2c3980264c753ba754322c7c8c4f2634 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/mem.zig
2093 36570091 1656419286550029980 ca1621dc43254a56e24a1d2bece18d59 /home/strange/Documentos/Projectes/Programació/usg/src/game.zig 2326 36570413 1656433976089986564 b5923a8a7d90c6a495a5914d992e6f25 /home/strange/Documentos/Projectes/Programació/usg/src/game.zig
1359 36569821 1656418680670025505 5643391f858689faa3fe97fa8d814454 /home/strange/Documentos/Projectes/Programació/usg/src/grid.zig 1359 36569821 1656418680670025505 5643391f858689faa3fe97fa8d814454 /home/strange/Documentos/Projectes/Programació/usg/src/grid.zig
342 36569823 1656079597800036836 fcaa0d5efa9177d38e199b7727f8f020 /home/strange/Documentos/Projectes/Programació/usg/src/cell.zig 342 36569823 1656079597800036836 fcaa0d5efa9177d38e199b7727f8f020 /home/strange/Documentos/Projectes/Programació/usg/src/cell.zig
3375 36569819 1656418592760024855 16d00cd07d8a20e15e989c4bcb844021 /home/strange/Documentos/Projectes/Programació/usg/src/piece.zig 3497 36569819 1656433455109989671 609c6515376d60dd45ea803bc24f2389 /home/strange/Documentos/Projectes/Programació/usg/src/piece.zig
45023 36968356 1655270162000000000 c30e605ecc099ad6f5498460429756fa /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/elf.zig 45023 36968356 1655270162000000000 c30e605ecc099ad6f5498460429756fa /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/elf.zig
5432 36968458 1655270162000000000 624956409eee1e93746e77415e9cdca9 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/atomic.zig 5432 36968458 1655270162000000000 624956409eee1e93746e77415e9cdca9 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/atomic.zig
25568 36968501 1655270162000000000 f15ad6de023e2f7205ca6550cee7becd /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/atomic/Atomic.zig 25568 36968501 1655270162000000000 f15ad6de023e2f7205ca6550cee7becd /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/atomic/Atomic.zig
@ -37,7 +37,7 @@
9352 36968413 1655270162000000000 8c526bbafa435d2af666b490a1e838ad /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/Thread/Mutex.zig 9352 36968413 1655270162000000000 8c526bbafa435d2af666b490a1e838ad /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/Thread/Mutex.zig
3400 36968452 1655270162000000000 5d1d518c32f4fd700943417f56120125 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/io/writer.zig 3400 36968452 1655270162000000000 5d1d518c32f4fd700943417f56120125 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/io/writer.zig
41490 36968415 1655270162000000000 f3d5b2d6057676d8866cf9291ef27dee /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/Thread/Futex.zig 41490 36968415 1655270162000000000 f3d5b2d6057676d8866cf9291ef27dee /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/Thread/Futex.zig
1927 36570132 1656419974100035058 ec127107cd07f104cca644a6dafc1e17 /home/strange/Documentos/Projectes/Programació/usg/src/movement.zig 2668 36570091 1656434160779985462 f3c4e20f05debba69b5c21f77e7e35f9 /home/strange/Documentos/Projectes/Programació/usg/src/movement.zig
102705 36968384 1655270162000000000 4896646ce9abce74b86bcbbc275a18c1 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/fmt.zig 102705 36968384 1655270162000000000 4896646ce9abce74b86bcbbc275a18c1 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/fmt.zig
31107 36968497 1655270162000000000 efe5272d00145b6eca03d42fcf5fcd30 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/mem/Allocator.zig 31107 36968497 1655270162000000000 efe5272d00145b6eca03d42fcf5fcd30 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/mem/Allocator.zig
87065 36968504 1655270162000000000 64f7d52fa9f0953194c996773f4ed130 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/hash_map.zig 87065 36968504 1655270162000000000 64f7d52fa9f0953194c996773f4ed130 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/hash_map.zig

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.