set next block when placing a new block

This commit is contained in:
2025-01-29 15:51:37 +01:00
parent 3c2902baa6
commit 90031cf3da
3 changed files with 12 additions and 1 deletions

View File

@@ -12,6 +12,11 @@
#include "tetromino/placing.h" #include "tetromino/placing.h"
void set_next_shape(GameData* const game_data) {
ShapeId id = (rand() % TETROMINO_COUNT) | (rand() % 4 << 3);
game_data->next_shape = id;
}
void game_init(GameData* const game_data) { void game_init(GameData* const game_data) {
// zero-initialize the game data // zero-initialize the game data
*game_data = (GameData){0}; *game_data = (GameData){0};
@@ -20,7 +25,9 @@ void game_init(GameData* const game_data) {
for (uint8_t i = 0; i < ROWS; i++) for (uint8_t i = 0; i < ROWS; i++)
game_data->row[i] = game_data->row_raw[i]; game_data->row[i] = game_data->row_raw[i];
game_data->selected = (SelectedShape){TETROMINO_L, 0, 0}; set_next_shape(game_data);
game_data->selected = (SelectedShape){game_data->next_shape, 0, 0};
set_next_shape(game_data);
// set a random seed using the system clock // set a random seed using the system clock
srand(time(NULL)); srand(time(NULL));

View File

@@ -22,7 +22,9 @@ typedef struct {
Colour* row[ROWS]; // stores how to interpert the raw level data Colour* row[ROWS]; // stores how to interpert the raw level data
Colour row_raw[ROWS][COLUMNS]; // stores the raw level data Colour row_raw[ROWS][COLUMNS]; // stores the raw level data
SelectedShape selected; SelectedShape selected;
ShapeId next_shape;
} GameData; } GameData;
void set_next_shape(GameData* game_data);
void game_init(GameData* game_data); // initializes the game void game_init(GameData* game_data); // initializes the game
void game_update(GameData* game_data, const uint8_t* keys); // updates the game's state void game_update(GameData* game_data, const uint8_t* keys); // updates the game's state

View File

@@ -102,6 +102,8 @@ void place_update(GameData* const game_data, const InputData move) {
if (shape_intersects(game_data->row, selected->id, selected->x, y)) { if (shape_intersects(game_data->row, selected->id, selected->x, y)) {
set_shape(game_data->row, selected->id, selected->x, selected->y); // if the shape intersects vertically, write the shape at the current position and return set_shape(game_data->row, selected->id, selected->x, selected->y); // if the shape intersects vertically, write the shape at the current position and return
clear_rows(game_data->row); // clear the rows that have been completed clear_rows(game_data->row); // clear the rows that have been completed
game_data->selected = (SelectedShape){game_data->next_shape, 0, 0};
set_next_shape(game_data);
return; return;
} }