mirror of
https://github.com/thepigeongenerator/tetris_clone.git
synced 2025-12-17 14:05:45 +01:00
add rotation & make code a bit more readable
This commit is contained in:
@@ -77,6 +77,13 @@ void game_update(GameData* game_data, const uint8_t* keys) {
|
||||
if (keys[SDL_SCANCODE_ESCAPE])
|
||||
stop();
|
||||
|
||||
place_update(game_data, keys);
|
||||
InputData move = MOVE_NONE;
|
||||
if (keys[SDL_SCANCODE_LEFT] || keys[SDL_SCANCODE_A]) move |= MOVE_LEFT;
|
||||
if (keys[SDL_SCANCODE_RIGHT] || keys[SDL_SCANCODE_D]) move |= MOVE_RIGHT;
|
||||
if (keys[SDL_SCANCODE_DOWN] || keys[SDL_SCANCODE_S] || keys[SDL_SCANCODE_SPACE]) move |= MOVE_DOWN;
|
||||
if (keys[SDL_SCANCODE_Q]) move |= MOVE_ROTLEFT;
|
||||
if (keys[SDL_SCANCODE_E]) move |= MOVE_ROTRIGHT;
|
||||
place_update(game_data, move);
|
||||
|
||||
// dbg_set_all(game_data);
|
||||
}
|
||||
|
||||
@@ -48,39 +48,39 @@ static bool shape_intersects(const Row* rows, const ShapeId id, const uint8_t x,
|
||||
return false;
|
||||
}
|
||||
|
||||
void place_update(GameData* const game_data, const uint8_t* const keys) {
|
||||
int8_t move = 0;
|
||||
static inline ShapeId rotate_id(const ShapeId id, const int dir) {
|
||||
return (id + dir) & 31;
|
||||
}
|
||||
|
||||
if (keys[SDL_SCANCODE_LEFT] || keys[SDL_SCANCODE_A])
|
||||
move |= 1;
|
||||
|
||||
if (keys[SDL_SCANCODE_RIGHT] || keys[SDL_SCANCODE_D])
|
||||
move |= 2;
|
||||
|
||||
if (keys[SDL_SCANCODE_DOWN] || keys[SDL_SCANCODE_S] || keys[SDL_SCANCODE_SPACE])
|
||||
move |= 4;
|
||||
|
||||
// set the shape if we moved vertically and collided
|
||||
if (!!(move & 4)) {
|
||||
const uint8_t y = game_data->selected.y + 1;
|
||||
if (shape_intersects(game_data->row, game_data->selected.id, game_data->selected.x, y)) {
|
||||
set_shape(game_data->row, game_data->selected.id, game_data->selected.x, y);
|
||||
void place_update(GameData* const game_data, const InputData move) {
|
||||
SelectedShape* const selected = &game_data->selected;
|
||||
// set the shape if we moved vertically and intersected
|
||||
if (move & 4) {
|
||||
const uint8_t y = selected->y + 1;
|
||||
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
|
||||
return;
|
||||
}
|
||||
|
||||
game_data->selected.y = y;
|
||||
// otherwise, just set Y
|
||||
selected->y = y;
|
||||
}
|
||||
|
||||
// ignore if the movement cancles itself out
|
||||
if ((move & 3) == 0 || (move & 3) == 3)
|
||||
return;
|
||||
// update shape's X coordinate movement
|
||||
if ((move & 3) != 3 && (move & 3)) {
|
||||
const uint8_t x = selected->x + ((move & 3) == 1 ? -1 : 1); // either move along -x or +x
|
||||
if (shape_intersects(game_data->row, selected->id, x, selected->y) == false) {
|
||||
selected->x = x; // set X if the shape does not intersect
|
||||
}
|
||||
}
|
||||
|
||||
const uint8_t x = game_data->selected.x + ((move & 3) == 1 ? -1 : 1);
|
||||
if (shape_intersects(game_data->row, game_data->selected.id, x, game_data->selected.y))
|
||||
return;
|
||||
|
||||
game_data->selected.x = x;
|
||||
printf("%i\n", x);
|
||||
// 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->row, id, selected->x, selected->y) == false) {
|
||||
selected->id = id;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef DEBUG
|
||||
|
||||
@@ -6,7 +6,17 @@
|
||||
#include "../game.h"
|
||||
#include "shapes.h"
|
||||
|
||||
void place_update(GameData* game_data, const uint8_t* keys);
|
||||
typedef uint8_t InputData;
|
||||
enum {
|
||||
MOVE_NONE = 0,
|
||||
MOVE_LEFT = 1,
|
||||
MOVE_RIGHT = 2,
|
||||
MOVE_DOWN = 4,
|
||||
MOVE_ROTLEFT = 8,
|
||||
MOVE_ROTRIGHT = 16,
|
||||
};
|
||||
|
||||
void place_update(GameData* game_data, const InputData move);
|
||||
|
||||
#ifdef DEBUG
|
||||
void dbg_set_all(GameData* game_data);
|
||||
|
||||
Reference in New Issue
Block a user