mirror of
https://github.com/thepigeongenerator/tetris_clone.git
synced 2025-12-17 14:05:45 +01:00
rework random shape selection
This commit is contained in:
@@ -13,9 +13,24 @@
|
||||
#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 next_shape(GameData* const game_data) {
|
||||
game_data->curr_idx++; // increase the current shape index
|
||||
game_data->sel_x = COLUMNS / 2 - SHAPE_WIDTH / 2; // move the shape position to the centre
|
||||
game_data->sel_y = 0;
|
||||
|
||||
// return if know which shape is next
|
||||
if (game_data->curr_idx < (TETROMINO_COUNT - 1))
|
||||
return;
|
||||
|
||||
game_data->curr_idx = 0;
|
||||
|
||||
// shuffle the next shapes using a Fisher–Yates shuffle
|
||||
for (uint8_t i = 0; i < (TETROMINO_COUNT - 1); i++) {
|
||||
const uint8_t j = i + rand() % (TETROMINO_COUNT - i);
|
||||
const ShapeId cache = game_data->nxt[i];
|
||||
game_data->nxt[i] = game_data->nxt[j];
|
||||
game_data->nxt[j] = cache;
|
||||
}
|
||||
}
|
||||
|
||||
void game_init(GameData* const game_data) {
|
||||
@@ -25,15 +40,18 @@ void game_init(GameData* const game_data) {
|
||||
// allocate size for each row
|
||||
for (int8_t i = 0; i < ROWS; i++) {
|
||||
game_data->rows[i] = calloc(COLUMNS, sizeof(Colour));
|
||||
//game_data->rows[i][0] = (Colour){(uint8_t)((((i + 1) ^ ((i + 1) >> 3)) * 0x27) & 0xFF)}; // for debugging
|
||||
// game_data->rows[i][0] = (Colour){(uint8_t)((((i + 1) ^ ((i + 1) >> 3)) * 0x27) & 0xFF)}; // for debugging
|
||||
}
|
||||
|
||||
// set a random seed using the system clock
|
||||
srand(time(NULL));
|
||||
|
||||
set_next_shape(game_data);
|
||||
game_data->selected = (SelectedShape){game_data->next_shape, COLUMNS / 2 - SHAPE_WIDTH / 2, 0};
|
||||
set_next_shape(game_data);
|
||||
// set the shape data in each slot to it's corrsponding ID
|
||||
for (ShapeId i = 0; i < TETROMINO_COUNT; i++)
|
||||
game_data->nxt[i] = i;
|
||||
|
||||
game_data->curr_idx = ~1; // set the current index to the max (well, so it becomes the max after increment)
|
||||
next_shape(game_data); // select the next shape (a refresh is triggered due to the current index being too high)
|
||||
}
|
||||
|
||||
// called every time the game's state is updated
|
||||
|
||||
@@ -9,22 +9,18 @@
|
||||
#define COLUMNS ((int8_t)10)
|
||||
#define ROWS ((int8_t)(COLUMNS * 2))
|
||||
|
||||
typedef struct {
|
||||
ShapeId id;
|
||||
int8_t x;
|
||||
int8_t y;
|
||||
} SelectedShape;
|
||||
|
||||
typedef const Colour* CRow;
|
||||
typedef Colour* Row;
|
||||
|
||||
typedef struct {
|
||||
Row rows[ROWS];
|
||||
SelectedShape selected;
|
||||
ShapeId next_shape;
|
||||
ShapeId nxt[7]; // the order of the shape ids that they should appear in
|
||||
uint8_t curr_idx; // current shape index
|
||||
int8_t sel_x; // selected shape x position
|
||||
int8_t sel_y; // selected shape y position
|
||||
} GameData;
|
||||
|
||||
void set_next_shape(GameData* game_data);
|
||||
void next_shape(GameData* game_data);
|
||||
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_free(GameData* game_data); // free all data stored with the game
|
||||
|
||||
@@ -97,36 +97,39 @@ static inline ShapeId rotate_id(const ShapeId id, const int dir) {
|
||||
}
|
||||
|
||||
void place_update(GameData* const game_data, const InputData move) {
|
||||
SelectedShape* const selected = &game_data->selected;
|
||||
// store the current index, only changes when placed (which yields no movement) and rotation (which occurs last)
|
||||
ShapeId const curr_idx = game_data->curr_idx;
|
||||
|
||||
// set the shape if we moved vertically and intersected
|
||||
if (move & 4) {
|
||||
const int8_t y = selected->y + 1;
|
||||
if (shape_intersects(game_data->rows, selected->id, selected->x, y)) {
|
||||
set_shape(game_data->rows, selected->id, selected->x, selected->y); // if the shape intersects vertically, write the shape at the current position and return
|
||||
clear_rows(game_data->rows); // clear the rows that have been completed
|
||||
const int8_t y = game_data->sel_y + 1;
|
||||
if (shape_intersects(game_data->rows, game_data->nxt[curr_idx], game_data->sel_x, y)) {
|
||||
set_shape(game_data->rows, game_data->nxt[curr_idx], game_data->sel_x, game_data->sel_y); // if the shape intersects vertically, write the shape at the current position and return
|
||||
clear_rows(game_data->rows); // clear the rows that have been completed
|
||||
|
||||
game_data->selected = (SelectedShape){game_data->next_shape, COLUMNS / 2 - SHAPE_WIDTH / 2, 0};
|
||||
set_next_shape(game_data);
|
||||
next_shape(game_data);
|
||||
return;
|
||||
}
|
||||
|
||||
// otherwise, just set Y
|
||||
selected->y = y;
|
||||
game_data->sel_y = y;
|
||||
}
|
||||
|
||||
// update shape's X coordinate movement
|
||||
if ((move & 3) != 3 && (move & 3)) {
|
||||
const int8_t x = selected->x + ((move & 3) == 1 ? -1 : 1); // either move along -x or +x
|
||||
if (shape_intersects(game_data->rows, selected->id, x, selected->y) == false) {
|
||||
selected->x = x; // set X if the shape does not intersect
|
||||
const int8_t x = game_data->sel_x + ((move & 3) == 1 ? -1 : 1); // either move along -x or +x
|
||||
if (shape_intersects(game_data->rows, game_data->nxt[curr_idx], x, game_data->sel_y) == false) {
|
||||
game_data->sel_x = x; // set X if the shape does not intersect
|
||||
}
|
||||
}
|
||||
|
||||
// update the shape's rotation
|
||||
if (move & 8 || move & 16) {
|
||||
const ShapeId id = move & 8 ? rotate_id(selected->id, -8) : rotate_id(selected->id, 8);
|
||||
if (shape_intersects(game_data->rows, id, selected->x, selected->y) == false) {
|
||||
selected->id = id;
|
||||
const ShapeId id = move & 8 // check which direction we should move
|
||||
? rotate_id(game_data->nxt[curr_idx], -8)
|
||||
: rotate_id(game_data->nxt[curr_idx], 8);
|
||||
if (shape_intersects(game_data->rows, id, game_data->sel_x, game_data->sel_y) == false) {
|
||||
game_data->nxt[curr_idx] = id;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user