Detect top out and return to menu WIP

This commit is contained in:
Dusk 2022-09-03 20:41:26 +02:00
parent 8d47cb26f5
commit 5adc005f6f
5 changed files with 66 additions and 1 deletions

View File

@ -53,6 +53,8 @@ timer_right_arr: Timer,
timer_right_das: Timer, timer_right_das: Timer,
timer_gravity: Timer, timer_gravity: Timer,
needs_reinit: bool = false,
pub fn init(renderer: *Renderer.Renderer) Self { pub fn init(renderer: *Renderer.Renderer) Self {
var ret = Self{ var ret = Self{
.grid = Grid.init(), .grid = Grid.init(),
@ -76,6 +78,8 @@ pub fn init(renderer: *Renderer.Renderer) Self {
return ret; return ret;
} }
// Main Game loop
pub fn tick(self: *Self) State { pub fn tick(self: *Self) State {
// TIMERS // TIMERS
@ -188,6 +192,17 @@ pub fn tick(self: *Self) State {
self.shadow.color.a /= 4; self.shadow.color.a /= 4;
} }
// Check for a top out
{
if (movement.isToppedOut(self.grid)) {
std.debug.print("Topped Out!\n", .{});
// Return to main menu
self.state = State.main_menu;
// Flag that I need a reset
self.needs_reinit = true;
}
}
self.grid.clearLines(); self.grid.clearLines();
self.renderer.render(self.*); self.renderer.render(self.*);

View File

@ -47,4 +47,3 @@ pub fn clearLines(self: *Self) void {
} }
} }
} }

View File

@ -232,3 +232,38 @@ fn checkTTwist(grid: Grid, piece: Piece) bool {
} }
return false; return false;
} }
pub fn isToppedOut(grid: Grid) bool {
// First check if a cell in the first rows are occupied
// const row_limit = 5;
// var i: usize = 0; // Go through rows
// var j: usize = 0; // Go through columns
// while(i < row_limit) {
// while (j < Grid.ncols) {
// if (!grid.cells[i][j].free) {
// return true;
// }
// }
// j = 0;
// }
// Must check certain cells (where pieces spawn)
// rows 20, 21, 22, 23, columns 3, 4, 5, 6
//
const cells_to_check = .{
.{7, 3},.{7, 4},.{7, 5},.{7, 6},
.{8, 3},.{8, 4},.{8, 5},.{8, 6},
.{9, 3},.{9, 4},.{9, 5},.{9, 6},
.{10, 3},.{10, 4},.{10, 5},.{10, 6},
};
inline for (cells_to_check) |cell| {
const row = cell[0];
const col = cell[1];
if (!grid.cells[row][col].free) {
return true;
}
}
return false;
}

View File

@ -46,6 +46,8 @@ state: State = State.main_menu,
holding_down: bool = false, holding_down: bool = false,
holding_enter: bool = false, holding_enter: bool = false,
needs_reinit: bool = false,
pub fn init(renderer: *Renderer.Renderer) Self { pub fn init(renderer: *Renderer.Renderer) Self {
return Self{ .renderer = Renderer.init(renderer) }; return Self{ .renderer = Renderer.init(renderer) };
} }
@ -84,6 +86,12 @@ pub fn getTab(self: Self) MenuTab {
} }
pub fn tick(self: *Self) State { pub fn tick(self: *Self) State {
if (self.state != State.main_menu) {
// If we are returnining
self.state = State.main_menu;
}
//const sel = self.getSel(); //const sel = self.getSel();
//const tab = self.getTab(); //const tab = self.getTab();
var key_state = SDL.getKeyboardState(); var key_state = SDL.getKeyboardState();

View File

@ -28,6 +28,14 @@ pub fn main() !void {
} }
} }
if (game.needs_reinit) {
game = Game.init(&renderer);
}
if (main_menu.needs_reinit) {
main_menu = MainMenu.init(&renderer);
}
current_state = switch (current_state) { current_state = switch (current_state) {
.main_menu => main_menu.tick(), .main_menu => main_menu.tick(),
.game => game.tick(), .game => game.tick(),