diff --git a/src/game.zig b/src/game.zig index 8338cdd..ba732b2 100644 --- a/src/game.zig +++ b/src/game.zig @@ -43,10 +43,12 @@ fn render(self: Self) void { } pub fn tick(self: *Self) void { if (self.piece) |safe_piece| { - std.debug.print("Piece: Col: {} Row: {}\n", .{ safe_piece.col, safe_piece.row }); var key_state = SDL.SDL_GetKeyboardState(null); + + // MOVEMENT + if (key_state[SDL.SDL_SCANCODE_D] == 1 and !self.right_action) { self.right_action = true; 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(); } diff --git a/src/movement.zig b/src/movement.zig index bd5ed6e..ed55cd5 100644 --- a/src/movement.zig +++ b/src/movement.zig @@ -7,11 +7,13 @@ pub fn moveRight(grid: Grid, piece: Piece) Piece { 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) { + @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) { + !grid.cells[@intCast(usize, new_piece.row + @intCast(i32, y))][@intCast(usize, new_piece.col + @intCast(i32, x))].free) + { return piece; } } @@ -25,11 +27,13 @@ pub fn moveLeft(grid: Grid, piece: Piece) Piece { 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) { + @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) { + !grid.cells[@intCast(usize, new_piece.row + @intCast(i32, y))][@intCast(usize, new_piece.col + @intCast(i32, x))].free) + { return piece; } } @@ -43,14 +47,35 @@ pub fn moveDown(grid: Grid, piece: Piece) Piece { 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; + @intCast(i32, y) + new_piece.row > Grid.nrows - 1) + { + new_piece.row -= 1; + new_piece.dropped = true; + return new_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; + !grid.cells[@intCast(usize, new_piece.row + @intCast(i32, y))][@intCast(usize, new_piece.col + @intCast(i32, x))].free) + { + new_piece.row -= 1; + new_piece.dropped = true; + 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; +} diff --git a/src/piece.zig b/src/piece.zig index 1f4e35a..550d9f3 100644 --- a/src/piece.zig +++ b/src/piece.zig @@ -18,9 +18,16 @@ pub const Type = enum { // Positions relative to a grid row: i32, col: i32, + +// 4x4 grid indicating piece form structure: [4][4]bool, + +// Should this be dropped and become part of the grid (flag) +dropped: bool = false, + color: SDL.SDL_Color, + pub fn init(piece_type: Type) Self { return Self{ .row = 0, diff --git a/zig-cache/h/8fb74d559dd889eac55495e9f1d9dc64.txt b/zig-cache/h/8fb74d559dd889eac55495e9f1d9dc64.txt index 6feb8ce..8b3b76c 100644 --- a/zig-cache/h/8fb74d559dd889eac55495e9f1d9dc64.txt +++ b/zig-cache/h/8fb74d559dd889eac55495e9f1d9dc64.txt @@ -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 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 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 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 -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 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 -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 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 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 @@ -37,7 +37,7 @@ 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 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 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 diff --git a/zig-cache/o/cb7202b334edba0c30e723c498cd683c/usg b/zig-cache/o/cb7202b334edba0c30e723c498cd683c/usg new file mode 100755 index 0000000..d65bfda Binary files /dev/null and b/zig-cache/o/cb7202b334edba0c30e723c498cd683c/usg differ diff --git a/zig-cache/o/cb7202b334edba0c30e723c498cd683c/usg.o b/zig-cache/o/cb7202b334edba0c30e723c498cd683c/usg.o new file mode 100644 index 0000000..24ea5b0 Binary files /dev/null and b/zig-cache/o/cb7202b334edba0c30e723c498cd683c/usg.o differ diff --git a/zig-cache/o/f0f790883c852ef9bcde0e2fd377d589/usg b/zig-cache/o/f0f790883c852ef9bcde0e2fd377d589/usg new file mode 100755 index 0000000..2e74a9f Binary files /dev/null and b/zig-cache/o/f0f790883c852ef9bcde0e2fd377d589/usg differ diff --git a/zig-cache/o/f0f790883c852ef9bcde0e2fd377d589/usg.o b/zig-cache/o/f0f790883c852ef9bcde0e2fd377d589/usg.o new file mode 100644 index 0000000..1f32ca2 Binary files /dev/null and b/zig-cache/o/f0f790883c852ef9bcde0e2fd377d589/usg.o differ diff --git a/zig-cache/z/15c879599ff43bd66fcf3641638f0aa9 b/zig-cache/z/15c879599ff43bd66fcf3641638f0aa9 index 48977ae..4361fdc 100644 Binary files a/zig-cache/z/15c879599ff43bd66fcf3641638f0aa9 and b/zig-cache/z/15c879599ff43bd66fcf3641638f0aa9 differ diff --git a/zig-cache/z/87092e0025c62db0f1adbe7b56bf23bb b/zig-cache/z/87092e0025c62db0f1adbe7b56bf23bb index c0c2442..291318a 100644 Binary files a/zig-cache/z/87092e0025c62db0f1adbe7b56bf23bb and b/zig-cache/z/87092e0025c62db0f1adbe7b56bf23bb differ diff --git a/zig-cache/z/a4146a97860546c83bdbfe47a036b0af b/zig-cache/z/a4146a97860546c83bdbfe47a036b0af index d0d1052..480f26e 100644 Binary files a/zig-cache/z/a4146a97860546c83bdbfe47a036b0af and b/zig-cache/z/a4146a97860546c83bdbfe47a036b0af differ diff --git a/zig-out/bin/usg b/zig-out/bin/usg index 59e653c..2e74a9f 100755 Binary files a/zig-out/bin/usg and b/zig-out/bin/usg differ