Skip to content

Commit

Permalink
refactor tetris's fall down logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ktpss95112 committed Aug 18, 2024
1 parent ffcec1e commit c750107
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 50 deletions.
2 changes: 1 addition & 1 deletion fw/Core/Hitcon/App/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.PHONY: format

/tmp/test-tetris: *.cc *.h
g++ -g -O0 -DHITCON_TEST_MODE -o /tmp/test-tetris -I.. test-tetris.cc TetrisGame.cc
g++ -Wall -Wextra -pedantic -g -O0 -DHITCON_TEST_MODE -o /tmp/test-tetris -I.. test-tetris.cc TetrisGame.cc

format:
clang-format -i *.cc *.h
Expand Down
22 changes: 3 additions & 19 deletions fw/Core/Hitcon/App/TetrisApp.cc
Original file line number Diff line number Diff line change
Expand Up @@ -192,25 +192,9 @@ void TetrisApp::periodic_task_callback(void *) {
}

case hitcon::tetris::GAME_STATE_PLAYING: {
// check if it's time to fall down
unsigned now = SysTimer::GetTime();
unsigned fall_period;
if (hitcon::tetris::FALL_PERIOD >
game.game_get_cleared_lines() *
hitcon::tetris::SPEED_UP_PER_CLEAR_LINE) {
fall_period = hitcon::tetris::FALL_PERIOD -
game.game_get_cleared_lines() *
hitcon::tetris::SPEED_UP_PER_CLEAR_LINE;
} else {
fall_period = hitcon::tetris::MIN_FALL_PERIOD;
}

if (fall_period < hitcon::tetris::MIN_FALL_PERIOD) {
fall_period = hitcon::tetris::MIN_FALL_PERIOD;
}

if (now - last_fall_time >= fall_period) {
game.game_fall_down_tetromino();
static int last_fall_time = 0;
int now = static_cast<int>(SysTimer::GetTime());
if (game.game_fall_down_if_its_time(now, last_fall_time)) {
last_fall_time = now;
}

Expand Down
1 change: 0 additions & 1 deletion fw/Core/Hitcon/App/TetrisApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ class TetrisApp : public App {
hitcon::tetris::TetrisGame game;
hitcon::service::sched::PeriodicTask periodic_task;

unsigned last_fall_time = 0;
void RecvAttackPacket(hitcon::service::xboard::PacketCallbackArg *packet);

public:
Expand Down
24 changes: 18 additions & 6 deletions fw/Core/Hitcon/App/TetrisGame.cc
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,6 @@ bool TetrisGame::generate_new_tetromino() {
return place_tetromino(current_x, current_y);
}

void TetrisGame::game_fall_down_tetromino() {
if (state == GAME_STATE_PLAYING) {
fall_down_tetromino();
}
}

void TetrisGame::game_on_input(TetrisDirection direction) {
if (state == GAME_STATE_PLAYING) {
switch (direction) {
Expand Down Expand Up @@ -205,5 +199,23 @@ void TetrisGame::game_register_attack_enemy_callback(
attack_enemy_callback = callback;
}

inline int max(int a, int b) { return a > b ? a : b; }

/**
* Check if it's time to fall down the tetromino.
* If fall down, return true.
*/
bool TetrisGame::game_fall_down_if_its_time(int now, int last_fall_time) {
int fall_period =
max(MIN_FALL_PERIOD,
FALL_PERIOD - game_get_cleared_lines() * SPEED_UP_PER_CLEAR_LINE);

if (now - last_fall_time >= fall_period) {
fall_down_tetromino();
return true;
}
return false;
}

} // namespace tetris
} // namespace hitcon
12 changes: 6 additions & 6 deletions fw/Core/Hitcon/App/TetrisGame.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
namespace hitcon {
namespace tetris {

constexpr unsigned FALL_PERIOD = 1000; // ms
constexpr unsigned UPDATE_INTERVAL = 200; // ms
constexpr unsigned SPEED_UP_PER_CLEAR_LINE = 50; // ms
constexpr unsigned MIN_FALL_PERIOD = 100; // ms
constexpr unsigned UPDATE_PRIORITY = 960;
constexpr int FALL_PERIOD = 1000; // ms
constexpr int UPDATE_INTERVAL = 200; // ms
constexpr int SPEED_UP_PER_CLEAR_LINE = 50; // ms
constexpr int MIN_FALL_PERIOD = 100; // ms
constexpr int UPDATE_PRIORITY = 960;

constexpr int BOARD_WIDTH = 8;
constexpr int BOARD_HEIGHT = 16;
Expand Down Expand Up @@ -138,7 +138,7 @@ class TetrisGame {
TetrisGame() = default;
TetrisGame(unsigned (*rand)(void));

void game_fall_down_tetromino();
bool game_fall_down_if_its_time(int now, int last_fall_time);
void game_on_input(TetrisDirection direction);
void game_draw_to_display(display_buf_t *buf);
inline void game_start_playing() { state = GAME_STATE_PLAYING; }
Expand Down
24 changes: 7 additions & 17 deletions fw/Core/Hitcon/App/test-tetris.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,26 +56,16 @@ void gameFunction() {
std::chrono::system_clock::now().time_since_epoch())
.count();
};
auto prev_update = now_ms() - hitcon::tetris::FALL_PERIOD;
int prev_update = 0;
game.game_start_playing();
while (1) {
auto now = now_ms();

unsigned fall_period;
if (hitcon::tetris::FALL_PERIOD >
game.game_get_cleared_lines() *
hitcon::tetris::SPEED_UP_PER_CLEAR_LINE) {
fall_period = hitcon::tetris::FALL_PERIOD -
game.game_get_cleared_lines() *
hitcon::tetris::SPEED_UP_PER_CLEAR_LINE;
} else {
fall_period = hitcon::tetris::MIN_FALL_PERIOD;
}

if (now - prev_update >= fall_period) {
{
std::lock_guard<std::mutex> lock(game_mutex);
game.game_fall_down_tetromino();
prev_update = now;
int now = now_ms();
if (game.game_fall_down_if_its_time(now, prev_update)) {
prev_update = now;
}

if (game.game_get_state() == hitcon::tetris::GAME_STATE_GAME_OVER) {
exit(0);
break;
Expand Down

0 comments on commit c750107

Please sign in to comment.