seems to work but some unpleasant delays

This commit is contained in:
Wolfgang Hottgenroth 2024-05-04 11:31:27 +02:00
parent 1607dc62dd
commit 30d50dcc5e

View File

@ -1,3 +1,5 @@
// #define STATE_DEBUGGING
#include "stddef.h" #include "stddef.h"
#include "stdint.h" #include "stdint.h"
@ -12,9 +14,9 @@
#include "buttons.h" #include "buttons.h"
#define GAME_CYCLE_TIME 50 #define GAME_CYCLE_TIME 10
#define GAMEOVER_DELAY 10 #define GAMEOVER_DELAY 10
#define MAX_LEVEL 20 #define MAX_LEVEL 100
static uint8_t delayFactor(uint8_t level) { static uint8_t delayFactor(uint8_t level) {
@ -22,7 +24,8 @@ static uint8_t delayFactor(uint8_t level) {
} }
typedef enum { typedef enum {
e_Start, e_NewStone, e_Down, e_DownDelay, e_ClearRows, e_Start, e_NewStone, e_Down, e_DownDelay,
e_ClearRowInit, e_ClearRowNext, e_ClearRowCheck, e_ClearRowFlash, e_ClearRowWipe,
e_GameOver, e_GameOverFill, e_GameOverWipe, e_GameOverDelay e_GameOver, e_GameOverFill, e_GameOverWipe, e_GameOverDelay
} state_t; } state_t;
@ -30,14 +33,17 @@ void gameExec(void *handle) {
static state_t state = e_Start; static state_t state = e_Start;
static uint8_t gameOverDelay; static uint8_t gameOverDelay;
static uint8_t rowIndex; static uint8_t rowIndex;
static uint8_t proceedDelay; static uint16_t proceedDelay;
static uint8_t level; static uint16_t level;
static uint16_t filledLines; static uint16_t filledLines;
static uint16_t score; static uint16_t score;
static bool newHighScoreAchieved; static bool newHighScoreAchieved;
bool wipedLines = false; static uint8_t clearCheckCnt;
#ifdef STATE_DEBUGGING
displaySetValue(state);
#endif
// --- engine begin ------------------------------------------------------- // --- engine begin -------------------------------------------------------
switch (state) { switch (state) {
// --- phase: game -------------------------------------------------------- // --- phase: game --------------------------------------------------------
@ -64,7 +70,6 @@ void gameExec(void *handle) {
case e_DownDelay: case e_DownDelay:
proceedDelay--; proceedDelay--;
if (proceedDelay == 0) { if (proceedDelay == 0) {
rowIndex = 0;
state = e_Down; state = e_Down;
} }
break; break;
@ -73,42 +78,59 @@ void gameExec(void *handle) {
if (! stoneMoveDown()) { if (! stoneMoveDown()) {
soundCtrl(SOUND_LOCK); soundCtrl(SOUND_LOCK);
stoneLock(); stoneLock();
state = e_ClearRows; state = e_ClearRowInit;
} else { } else {
proceedDelay = delayFactor(level); proceedDelay = delayFactor(level);
state = e_DownDelay; state = e_DownDelay;
} }
break; break;
case e_ClearRows: // --- phase: clear rows --------------------------------------------------
// clear filled lines case e_ClearRowInit:
for (uint8_t r = 0; r < CANVAS_HEIGHT; r++) { clearCheckCnt = 0;
if (canvasIsRowFilled(r)) { state = e_ClearRowCheck;
score += level; break;
if (score > eepromReadHighScore()) {
newHighScoreAchieved = true; case e_ClearRowNext:
eepromWriteHighScore(score); if (clearCheckCnt >= CANVAS_HEIGHT) {
} state = e_NewStone;
displaySetValue(score); } else {
canvasWipeRow(r); clearCheckCnt += 1;
canvasShow(); state = e_ClearRowCheck;
wipedLines = true; }
filledLines += 1; break;
case e_ClearRowCheck:
if (canvasIsRowFilled(clearCheckCnt)) {
score += level;
if (score > eepromReadHighScore()) {
newHighScoreAchieved = true;
eepromWriteHighScore(score);
} }
state = e_ClearRowFlash;
} else {
state = e_ClearRowNext;
} }
break;
if (wipedLines) { case e_ClearRowFlash:
soundCtrl(SOUND_PLING); canvasFillRow(clearCheckCnt, _white);
} state = e_ClearRowWipe;
break;
if (wipedLines && (filledLines > 0) && ((filledLines % 10) == 0)) { case e_ClearRowWipe:
canvasWipeRow(clearCheckCnt);
filledLines += 1;
if ((filledLines > 0) && ((filledLines % 10) == 0)) {
if (level < MAX_LEVEL) { if (level < MAX_LEVEL) {
level += 1; level += 1;
} }
soundCtrl(SOUND_FANFARE); soundCtrl(SOUND_FANFARE);
} else {
soundCtrl(SOUND_PLING);
} }
state = e_ClearRowNext;
state = e_NewStone;
break; break;
// --- phase: game over --------------------------------------------------- // --- phase: game over ---------------------------------------------------
@ -146,11 +168,13 @@ void gameExec(void *handle) {
canvasShow(); canvasShow();
#ifndef STATE_DEBUGGING
if (isGameActive()) { if (isGameActive()) {
displaySetValue(score); displaySetValue(score);
} else { } else {
displaySetValue(eepromReadHighScore()); displaySetValue(eepromReadHighScore());
} }
#endif
} }
void gameInit() { void gameInit() {