Change some names to coincide with zig style
This commit is contained in:
		
							parent
							
								
									af1c24853f
								
							
						
					
					
						commit
						df495eb608
					
				|  | @ -1,6 +1,6 @@ | ||||||
| const std = @import("std"); | const std = @import("std"); | ||||||
| 
 | 
 | ||||||
| const Piece = @import("piece.zig"); | const Piece = @import("Piece.zig"); | ||||||
| 
 | 
 | ||||||
| const Self = @This(); | const Self = @This(); | ||||||
| 
 | 
 | ||||||
|  | @ -1,10 +1,10 @@ | ||||||
| const std = @import("std"); | const std = @import("std"); | ||||||
| const SDL = @import("sdl2"); | const SDL = @import("sdl2"); | ||||||
| 
 | 
 | ||||||
| const Grid = @import("grid.zig"); | const Grid = @import("Grid.zig"); | ||||||
| const Piece = @import("piece.zig"); | const Piece = @import("Piece.zig"); | ||||||
| const Bag = @import("bag.zig"); | const Bag = @import("Bag.zig"); | ||||||
| const Timer = @import("timer.zig"); | const Timer = @import("Timer.zig"); | ||||||
| 
 | 
 | ||||||
| const renderer = @import("renderer.zig"); | const renderer = @import("renderer.zig"); | ||||||
| const movement = @import("movement.zig"); | const movement = @import("movement.zig"); | ||||||
|  | @ -15,7 +15,7 @@ const Action = struct { | ||||||
|     activate: bool = false, |     activate: bool = false, | ||||||
|     holding: bool = false, |     holding: bool = false, | ||||||
|     code: u8, |     code: u8, | ||||||
|     callback: fn(*Self)void, |     callback: fn (*Self) void, | ||||||
|     pub const Offset = enum(usize) { |     pub const Offset = enum(usize) { | ||||||
|         right = 0, |         right = 0, | ||||||
|         left = 1, |         left = 1, | ||||||
|  | @ -27,7 +27,6 @@ const Action = struct { | ||||||
|     }; |     }; | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| 
 |  | ||||||
| grid: Grid, | grid: Grid, | ||||||
| grid_cell_size: i32, | grid_cell_size: i32, | ||||||
| grid_pos_x: i32, | grid_pos_x: i32, | ||||||
|  | @ -90,8 +89,8 @@ pub fn tick(self: *Self) void { | ||||||
| 
 | 
 | ||||||
|     // TIMERS |     // TIMERS | ||||||
|     if (self.piece.timer_dropped.finished()) { |     if (self.piece.timer_dropped.finished()) { | ||||||
|             self.piece.timer_dropped.stop(); |         self.piece.timer_dropped.stop(); | ||||||
|             self.piece.dropped = true; |         self.piece.dropped = true; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     if (self.action_list[@enumToInt(Action.Offset.down)].holding) { |     if (self.action_list[@enumToInt(Action.Offset.down)].holding) { | ||||||
|  | @ -132,15 +131,14 @@ pub fn tick(self: *Self) void { | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| 
 |  | ||||||
|     // Update Shadow |     // Update Shadow | ||||||
|     { |     { | ||||||
|         self.shadow = movement.shadow(self.grid, self.piece); |         self.shadow = movement.shadow(self.grid, self.piece); | ||||||
|         self.shadow.color.a /= 4; |         self.shadow.color.a /= 4; | ||||||
| 
 |  | ||||||
|         self.grid.clearLines(); |  | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     self.grid.clearLines(); | ||||||
|  | 
 | ||||||
|     renderer.render(self.*); |     renderer.render(self.*); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | @ -1,8 +1,8 @@ | ||||||
| const std = @import("std"); | const std = @import("std"); | ||||||
| const SDL = @import("sdl2"); | const SDL = @import("sdl2"); | ||||||
| 
 | 
 | ||||||
| const Color = @import("color.zig"); | const color = @import("color.zig"); | ||||||
| const Cell = @import("cell.zig"); | const Cell = @import("Cell.zig"); | ||||||
| const utils = @import("utils.zig"); | const utils = @import("utils.zig"); | ||||||
| 
 | 
 | ||||||
| const range = utils.range; | 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_SetRenderDrawColor(renderer, r, g, b, a); | ||||||
|             _ = SDL.SDL_RenderFillRect(renderer, &rect); |             _ = 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); |             _ = SDL.SDL_RenderDrawRect(renderer, &rect); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  | @ -1,9 +1,10 @@ | ||||||
| const SDL = @import("sdl2"); | const SDL = @import("sdl2"); | ||||||
| const std = @import("std"); | const std = @import("std"); | ||||||
| 
 | 
 | ||||||
| const Color = @import("color.zig"); | const Grid = @import("Grid.zig"); | ||||||
| const Grid = @import("grid.zig"); | const Timer = @import("Timer.zig"); | ||||||
| const Timer = @import("timer.zig"); | 
 | ||||||
|  | const color = @import("color.zig"); | ||||||
| 
 | 
 | ||||||
| const Self = @This(); | const Self = @This(); | ||||||
| 
 | 
 | ||||||
|  | @ -106,13 +107,13 @@ pub fn init(piece_type: Type) Self { | ||||||
|         }, |         }, | ||||||
| 
 | 
 | ||||||
|         .color = switch (piece_type) { |         .color = switch (piece_type) { | ||||||
|             Type.o => Color.yellow, |             Type.o => color.yellow, | ||||||
|             Type.i => Color.cyan, |             Type.i => color.cyan, | ||||||
|             Type.l => Color.orange, |             Type.l => color.orange, | ||||||
|             Type.j => Color.blue, |             Type.j => color.blue, | ||||||
|             Type.s => Color.green, |             Type.s => color.green, | ||||||
|             Type.z => Color.red, |             Type.z => color.red, | ||||||
|             Type.t => Color.purple, |             Type.t => color.purple, | ||||||
|         }, |         }, | ||||||
|     }; |     }; | ||||||
| } | } | ||||||
|  | @ -1,7 +1,7 @@ | ||||||
| const std = @import("std"); | const std = @import("std"); | ||||||
| const SDL = @import("sdl2"); | const SDL = @import("sdl2"); | ||||||
| 
 | 
 | ||||||
| const Game = @import("game.zig"); | const Game = @import("Game.zig"); | ||||||
| 
 | 
 | ||||||
| pub fn main() !void { | pub fn main() !void { | ||||||
|     const stderr = std.io.getStdErr(); |     const stderr = std.io.getStdErr(); | ||||||
|  |  | ||||||
							
								
								
									
										105
									
								
								src/movement.zig
								
								
								
								
							
							
						
						
									
										105
									
								
								src/movement.zig
								
								
								
								
							|  | @ -1,10 +1,10 @@ | ||||||
| const std = @import("std"); | const std = @import("std"); | ||||||
| const Grid = @import("grid.zig"); | const Grid = @import("Grid.zig"); | ||||||
| const Piece = @import("piece.zig"); | const Piece = @import("Piece.zig"); | ||||||
| 
 | 
 | ||||||
| fn checkDrop (grid: Grid, piece: Piece) Piece { | fn checkDrop(grid: Grid, piece: Piece) Piece { | ||||||
|     var new_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) { |         if (new_piece.timer_dropped.started) { | ||||||
|             new_piece.timer_dropped.reset(); |             new_piece.timer_dropped.reset(); | ||||||
|         } else { |         } else { | ||||||
|  | @ -17,7 +17,6 @@ fn checkDrop (grid: Grid, piece: Piece) Piece { | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| pub fn moveRight(grid: Grid, piece: Piece) Piece { | pub fn moveRight(grid: Grid, piece: Piece) Piece { | ||||||
| 
 |  | ||||||
|     var new_piece = piece; |     var new_piece = piece; | ||||||
|     new_piece.col += 1; |     new_piece.col += 1; | ||||||
|     if (checkCollision(grid, new_piece)) { |     if (checkCollision(grid, new_piece)) { | ||||||
|  | @ -86,7 +85,13 @@ pub fn shadow(grid: Grid, piece: Piece) Piece { | ||||||
| fn checkCollision(grid: Grid, piece: Piece) bool { | fn checkCollision(grid: Grid, piece: Piece) bool { | ||||||
|     for (piece.structure) |_, y| { |     for (piece.structure) |_, y| { | ||||||
|         for (piece.structure[y]) |_, x| { |         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; |                 return true; | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|  | @ -117,26 +122,54 @@ pub fn kick(grid: Grid, piece: Piece, prev_piece: Piece) Piece { | ||||||
|     var new_piece = piece; |     var new_piece = piece; | ||||||
| 
 | 
 | ||||||
|     // Test 1 |     // 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; |     var offsets: [4][2]i8 = undefined; | ||||||
| 
 | 
 | ||||||
|     if (piece.piece_type == Piece.Type.i) { |     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 } }; |             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 } }; |             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 } }; |             offsets = .{ .{ -1, 0 }, .{ 2, 0 }, .{ -1, -2 }, .{ 2, 1 } }; | ||||||
|         } else { |         } else { | ||||||
|             offsets = .{ .{ 1, 0 }, .{ -2, 0 }, .{ 1, 2 }, .{ -2, -1 } }; |             offsets = .{ .{ 1, 0 }, .{ -2, 0 }, .{ 1, 2 }, .{ -2, -1 } }; | ||||||
|         } |         } | ||||||
|     } else { |     } 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 } }; |             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 } }; |             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 } }; |             offsets = .{ .{ 1, 0 }, .{ 1, -1 }, .{ 0, 2 }, .{ 1, 2 } }; | ||||||
|         } else { |         } else { | ||||||
|             offsets = .{ .{ -1, 0 }, .{ -1, 1 }, .{ 0, -2 }, .{ -1, -2 } }; |             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| { |     for (offsets) |offset| { | ||||||
|         new_piece.col += offset[0]; |         new_piece.col += offset[0]; | ||||||
|         new_piece.row += offset[1]; |         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.col -= offset[0]; | ||||||
|         new_piece.row -= offset[1]; |         new_piece.row -= offset[1]; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     return prev_piece; |     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; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | @ -1,10 +1,11 @@ | ||||||
| const SDL = @import("sdl2"); | const SDL = @import("sdl2"); | ||||||
| 
 | 
 | ||||||
| const Game = @import("game.zig"); | const Game = @import("Game.zig"); | ||||||
| const Bag = @import("bag.zig"); | const Bag = @import("Bag.zig"); | ||||||
| const Grid = @import("grid.zig"); | const Grid = @import("Grid.zig"); | ||||||
| const Piece = @import("piece.zig"); | const Piece = @import("Piece.zig"); | ||||||
| const Color = @import("color.zig"); | 
 | ||||||
|  | const color = @import("color.zig"); | ||||||
| 
 | 
 | ||||||
| pub fn renderBag(game: Game) void { | pub fn renderBag(game: Game) void { | ||||||
|     const pos_x = game.grid_pos_x + ((Grid.ncolumns + 1) * game.grid_cell_size); |     const pos_x = game.grid_pos_x + ((Grid.ncolumns + 1) * game.grid_cell_size); | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue