This commit is contained in:
Dusk 2022-07-05 23:09:59 +02:00
parent 0bfe66f9f3
commit 4f6a010564
143 changed files with 54 additions and 76 deletions

View File

@ -28,7 +28,8 @@ left_action: bool = false,
down_action: bool = false,
hdrop_action: bool = false,
swap_action: bool = false,
rotate_action: bool = false,
rotate_right_action: bool = false,
rotate_left_action: bool = false,
pub fn init(_renderer: *SDL.SDL_Renderer) Self {
var ret = Self{
@ -106,12 +107,20 @@ pub fn tick(self: *Self) void {
}
// ROTATE
if (key_state[SDL.SDL_SCANCODE_LEFT] == 1 and !self.rotate_action) {
self.rotate_action = true;
self.piece = movement.rotateAW(self.grid, self.piece);
if (key_state[SDL.SDL_SCANCODE_RIGHT] == 1 and !self.rotate_right_action) {
self.rotate_right_action = true;
self.piece = movement.rotateRight(self.grid, self.piece);
}
if (key_state[SDL.SDL_SCANCODE_RIGHT] == 0) {
self.rotate_right_action = false;
}
if (key_state[SDL.SDL_SCANCODE_LEFT] == 1 and !self.rotate_left_action) {
self.rotate_left_action = true;
self.piece = movement.rotateLeft(self.grid, self.piece);
}
if (key_state[SDL.SDL_SCANCODE_LEFT] == 0) {
self.rotate_action = false;
self.rotate_left_action = false;
}
// DROP

View File

@ -76,8 +76,16 @@ fn checkCollision(grid: Grid, piece: Piece) bool {
return false;
}
pub fn rotateAW(grid: Grid, piece: Piece) Piece {
var new_piece = piece.rotateAW();
pub fn rotateLeft(grid: Grid, piece: Piece) Piece {
var new_piece = piece.rotate(Piece.Rot.left);
if (checkCollision(grid, new_piece)) {
return piece;
}
return new_piece;
}
pub fn rotateRight(grid: Grid, piece: Piece) Piece {
var new_piece = piece.rotate(Piece.Rot.right);
if (checkCollision(grid, new_piece)) {
return piece;
}

View File

@ -6,6 +6,11 @@ const Grid = @import("grid.zig");
const Self = @This();
pub const Rot = enum(u2) {
right = 0,
left = 2,
};
pub const Type = enum {
o,
i,
@ -95,15 +100,29 @@ pub fn init(piece_type: Type) Self {
};
}
pub fn rotateAW(self: Self) Self {
var seq_outer = [4][2]u8{ .{ 0, 0 }, .{ 0, 2 }, .{ 2, 2 }, .{ 2, 0 } };
var seq_inner = [4][2]u8{ .{ 0, 1 }, .{ 1, 2 }, .{ 2, 1 }, .{ 1, 0 } };
pub fn rotate(self: Self, dir: Rot) Self {
if (self.piece_type == Type.o) {
return self;
}
var new_piece = self;
for (seq_outer) |_, i| {
new_piece.structure[seq_outer[i][0]][seq_outer[i][1]] = self.structure[seq_outer[(i + 1) % 4][0]][seq_outer[(i + 1) % 4][1]];
new_piece.structure[seq_inner[i][0]][seq_inner[i][1]] = self.structure[seq_inner[(i + 1) % 4][0]][seq_inner[(i + 1) % 4][1]];
if (self.piece_type == Type.i) {
const sequences = .{ .{ .{ 1, 0 }, .{ 0, 2 }, .{ 2, 3 }, .{ 3, 1 } }, .{ .{ 2, 0 }, .{ 0, 1 }, .{ 1, 3 }, .{ 3, 2 } }, .{ .{ 1, 1 }, .{ 1, 2 }, .{ 2, 2 }, .{ 2, 1 } } };
inline for (sequences) |seq| {
inline for (seq) |_, i| {
new_piece.structure[seq[i][0]][seq[i][1]] = self.structure[seq[(i + @enumToInt(dir) - 1) % 4][0]][seq[(i + @enumToInt(dir) - 1) % 4][1]];
}
}
} else {
const sequences = .{ .{ .{ 0, 0 }, .{ 0, 2 }, .{ 2, 2 }, .{ 2, 0 } }, .{ .{ 0, 1 }, .{ 1, 2 }, .{ 2, 1 }, .{ 1, 0 } } };
inline for (sequences) |seq| {
inline for (seq) |_, i| {
new_piece.structure[seq[i][0]][seq[i][1]] = self.structure[seq[(i + @enumToInt(dir) - 1) % 4][0]][seq[(i + @enumToInt(dir) - 1) % 4][1]];
}
}
}
//[0,0][0,1][0,2]

View File

@ -1,7 +1,7 @@
1361 36569768 1657038271710135652 e361ab3d1987688681b5db7b52fd5cba /home/strange/Documentos/Projectes/Programació/usg/src/main.zig
1361 36569768 1657038271710135652 e361ab3d1987688681b5db7b52fd5cba /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 36570205 1657045337970187844 91a8da097c7c1645ba2a6217a5171c4e /home/strange/Documentos/Projectes/Programació/usg/zig-cache/options/kh59p23UXKId3bWMadeQ0Yp6cv2kATA4qPk2nBQ2q_3IsG9JTjKRYS1YxdIMHL6s
32 36570080 1657053720550249757 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,15 +21,15 @@
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 36570205 1657045337970187844 91a8da097c7c1645ba2a6217a5171c4e /home/strange/Documentos/Projectes/Programació/usg/zig-cache/options/kh59p23UXKId3bWMadeQ0Yp6cv2kATA4qPk2nBQ2q_3IsG9JTjKRYS1YxdIMHL6s
32 36570080 1657053720550249757 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
4050 36570427 1657045309340187632 4eed9925bcf56f1124a8104551200db3 /home/strange/Documentos/Projectes/Programació/usg/src/game.zig
4399 36570427 1657050625270226896 5a5f1095331817ca2935aae8f1c887ef /home/strange/Documentos/Projectes/Programació/usg/src/game.zig
1985 36569864 1657040654430153251 a4190b51e743c069b95bce0368e169d7 /home/strange/Documentos/Projectes/Programació/usg/src/grid.zig
320 36569823 1657026237740046769 9c0018b94509c3c3f7e8edd793177de6 /home/strange/Documentos/Projectes/Programació/usg/src/cell.zig
1708 36569840 1657038395020136563 151ce67aaf4e36de54f56be701bc0b66 /home/strange/Documentos/Projectes/Programació/usg/src/bag.zig
4299 36569846 1657045241410187130 c52538c3dca05a31c4dc94d1cf21cf11 /home/strange/Documentos/Projectes/Programació/usg/src/piece.zig
4796 36569846 1657053719270249748 1273e72a46280cdcf35d48a94d969800 /home/strange/Documentos/Projectes/Programació/usg/src/piece.zig
16192 36968506 1655270162000000000 744a2bb13e662f350a196780c9fbcad2 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/rand.zig
3105 36968346 1655270162000000000 412e5ff0bcf84571f173caa8acab5393 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/rand/Xoshiro256.zig
45023 36968356 1655270162000000000 c30e605ecc099ad6f5498460429756fa /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/elf.zig
@ -40,65 +40,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
2416 36569862 1657045224520187006 3194e475c20e8da58a2478a81369f81b /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
44333 36968425 1655270162000000000 e849aa8f5b1991a4c903e7f1cf5d2b1c /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/dwarf.zig
55109 36968306 1655270162000000000 076580071cf17cb72c33bcfac107e806 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/array_list.zig
45424 36968383 1655270162000000000 7560f06faabdb139b53b1f2e8b2fae06 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/heap.zig
4685 36968633 1655270162000000000 bccc2a1210ecea59d9b108bd05b44f4d /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/heap/arena_allocator.zig
13435 36968492 1655270162000000000 841107470272eebec404e2b902f8b639 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/linked_list.zig
8944 36968553 1655270162000000000 2d66740614c92899c32322c4528aa6bb /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/os/linux/x86_64.zig
5832 36968454 1655270162000000000 27d9545fd30b6e99a4c147ba6d8c07db /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/io/fixed_buffer_stream.zig
27187 36968450 1655270162000000000 18454c1abe49c3ed1428829ba043ca05 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/io/reader.zig
1117 36968442 1655270162000000000 3f2f071b498497f7d54bcfc458d51f5e /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/io/seekable_stream.zig
15524 36968361 1655270162000000000 fd35bd5ce692e376140efff780ae8fd9 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/leb128.zig
1399 36968421 1655270162000000000 39287cc8a2a858bc3b39edecab481aff /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/dwarf/FORM.zig
3848 36968420 1655270162000000000 673136e55905f01155e9463de5e36e7e /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/dwarf/TAG.zig
7395 36968424 1655270162000000000 fe0e04bcd58863e97993f2137b09a3ca /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/dwarf/AT.zig
50955 36968340 1655270162000000000 89afffa3bc72846bab2c1ed13b0672f4 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/fs/path.zig
7889 36968577 1655270162000000000 562fc4eee397a5037d16dffd5e048cd4 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/os/linux/errno/generic.zig
12529 36968527 1655270162000000000 2e8a443dd5c55df11dc0ea7ce5b52bf5 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/time.zig
594 36573579 1656676335150089774 d6e14fd58e4cc4e82c4401b04e62b9f2 /home/strange/Documentos/Projectes/Programació/usg/src/preview.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
22658 36968211 1655270162000000000 d723b434ed5405277f53a378f6673454 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/start.zig
64748 36968503 1655270162000000000 903d93656adf53121b2ee1260ae9b2e4 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/target.zig
88870 36968320 1655270162000000000 a4e267754cdf9c71d18dca0dfe685156 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/target/x86.zig
65693 36968358 1655270162000000000 887583a6d3a492ee4a8aaa71c3f4c9c8 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/math.zig
36988 36968463 1655270162000000000 618c6412f696610fe70ffed272e536b7 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/meta.zig
79192 36968432 1655270162000000000 85fe64f01ec67e2d404d41ed6434a1ee /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/debug.zig
6467 36968212 1655270162000000000 b98e509893b4c3e8c2c75e1c75434fe5 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/io.zig
120768 36968521 1655270162000000000 3364cc81aeb809c2a35e3ed71557eac0 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/fs.zig
62393 36968339 1655270162000000000 e7a0b747c6b4d9a3f2444e01bc913e3b /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/fs/file.zig
280877 36968431 1655270162000000000 5aee6408eb3f6bc06fa6b4a703a784c4 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/os.zig
19406 36968288 1655270162000000000 91f313ee3555d57d621bba6c0b89b441 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/c.zig
12404 36968388 1655270162000000000 5af0dd8b7f0c7a916f42d40bf1fc0341 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/c/linux.zig
156620 36968545 1655270162000000000 44227d065f1b9c55778b0b1716c1fe62 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/os/linux.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
1640 36570393 1655299923670057346 c3d50ea43c9e10688a521a9ac927ded4 /home/strange/Documentos/Projectes/Programació/usg/lib/SDL.zig/src/binding/sdl_image.zig
32 36570205 1657045337970187844 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
4050 36570427 1657045309340187632 4eed9925bcf56f1124a8104551200db3 /home/strange/Documentos/Projectes/Programació/usg/src/game.zig
1985 36569864 1657040654430153251 a4190b51e743c069b95bce0368e169d7 /home/strange/Documentos/Projectes/Programació/usg/src/grid.zig
320 36569823 1657026237740046769 9c0018b94509c3c3f7e8edd793177de6 /home/strange/Documentos/Projectes/Programació/usg/src/cell.zig
1708 36569840 1657038395020136563 151ce67aaf4e36de54f56be701bc0b66 /home/strange/Documentos/Projectes/Programació/usg/src/bag.zig
4299 36569846 1657045241410187130 c52538c3dca05a31c4dc94d1cf21cf11 /home/strange/Documentos/Projectes/Programació/usg/src/piece.zig
16192 36968506 1655270162000000000 744a2bb13e662f350a196780c9fbcad2 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/rand.zig
3105 36968346 1655270162000000000 412e5ff0bcf84571f173caa8acab5393 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/rand/Xoshiro256.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
18630 36968523 1655270162000000000 c72160564e2e203a04f11f4b13553da0 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/meta/trait.zig
42085 36968308 1655270162000000000 b77ce915e7496d89b54ab38aee097b81 /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/Thread.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
41490 36968415 1655270162000000000 f3d5b2d6057676d8866cf9291ef27dee /home/strange/Documentos/Projectes/Programació/usg/zig/lib/std/Thread/Futex.zig
2416 36569862 1657045224520187006 3194e475c20e8da58a2478a81369f81b /home/strange/Documentos/Projectes/Programació/usg/src/movement.zig
2630 36569862 1657050236460224024 5dff7cfcc8018dda84a0c5cdfefabc5b /home/strange/Documentos/Projectes/Programació/usg/src/movement.zig
1287 36569807 1657025367450040341 019ede768d3bb377ce293523ab6255bf /home/strange/Documentos/Projectes/Programació/usg/src/renderer.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

Some files were not shown because too many files have changed in this diff Show More