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])
|
if (keys[SDL_SCANCODE_ESCAPE])
|
||||||
stop();
|
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);
|
// 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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void place_update(GameData* const game_data, const uint8_t* const keys) {
|
static inline ShapeId rotate_id(const ShapeId id, const int dir) {
|
||||||
int8_t move = 0;
|
return (id + dir) & 31;
|
||||||
|
}
|
||||||
|
|
||||||
if (keys[SDL_SCANCODE_LEFT] || keys[SDL_SCANCODE_A])
|
void place_update(GameData* const game_data, const InputData move) {
|
||||||
move |= 1;
|
SelectedShape* const selected = &game_data->selected;
|
||||||
|
// set the shape if we moved vertically and intersected
|
||||||
if (keys[SDL_SCANCODE_RIGHT] || keys[SDL_SCANCODE_D])
|
if (move & 4) {
|
||||||
move |= 2;
|
const uint8_t y = selected->y + 1;
|
||||||
|
if (shape_intersects(game_data->row, selected->id, selected->x, y)) {
|
||||||
if (keys[SDL_SCANCODE_DOWN] || keys[SDL_SCANCODE_S] || keys[SDL_SCANCODE_SPACE])
|
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
|
||||||
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);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
game_data->selected.y = y;
|
// otherwise, just set Y
|
||||||
|
selected->y = y;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ignore if the movement cancles itself out
|
// update shape's X coordinate movement
|
||||||
if ((move & 3) == 0 || (move & 3) == 3)
|
if ((move & 3) != 3 && (move & 3)) {
|
||||||
return;
|
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);
|
// update the shape's rotation
|
||||||
if (shape_intersects(game_data->row, game_data->selected.id, x, game_data->selected.y))
|
if (move & 8 || move & 16) {
|
||||||
return;
|
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) {
|
||||||
game_data->selected.x = x;
|
selected->id = id;
|
||||||
printf("%i\n", x);
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
|
|||||||
@@ -6,7 +6,17 @@
|
|||||||
#include "../game.h"
|
#include "../game.h"
|
||||||
#include "shapes.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
|
#ifdef DEBUG
|
||||||
void dbg_set_all(GameData* game_data);
|
void dbg_set_all(GameData* game_data);
|
||||||
|
|||||||
Reference in New Issue
Block a user