Skip to content

Commit

Permalink
Merge pull request #333 from AEFeinstein/2048_anim
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnywycliffe authored Dec 2, 2024
2 parents 36b5b6f + 5b51ce5 commit c5f3b01
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 19 deletions.
Binary file added assets/2048/Sounds/lullaby_in_numbers.mid
Binary file not shown.
13 changes: 8 additions & 5 deletions main/modes/games/2048/2048_game.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,9 @@ static const int32_t sparkleIndices[] = {
* @brief Initialize the game state
*
* @param t48 The game data to initialize
* @param tiltControls true to use tilt controls, false to use D-Pad
*/
void t48_gameInit(t48_t* t48)
void t48_gameInit(t48_t* t48, bool tiltControls)
{
// Clear the board
memset(t48->board, 0, sizeof(t48->board));
Expand All @@ -92,6 +93,7 @@ void t48_gameInit(t48_t* t48)

// Accept input
t48->acceptGameInput = true;
t48->tiltControls = tiltControls;
}

/**
Expand Down Expand Up @@ -141,7 +143,8 @@ void t48_gameLoop(t48_t* t48, int32_t elapsedUs)
drawText(&t48->font, c555, textBuffer, T48_SIDE_MARGIN, 4);

// Check if anything is animating
bool animationInProgress = false;
t48->cellsAnimating = false;
bool sparklesAnimating = false;

// Draw new tile third
if (t48->nTile.active)
Expand All @@ -166,7 +169,7 @@ void t48_gameLoop(t48_t* t48, int32_t elapsedUs)
// Draw the tile(s) for this cell
if (t48_drawCellTiles(t48, x, y, elapsedUs))
{
animationInProgress = true;
t48->cellsAnimating = true;
}
else if (cell->drawnTiles[1].value)
{
Expand Down Expand Up @@ -199,13 +202,13 @@ void t48_gameLoop(t48_t* t48, int32_t elapsedUs)
// Draw sparkles for this cell
if (t48_drawSparkles(&t48->board[x][y], elapsedUs))
{
animationInProgress = true;
sparklesAnimating = true;
}
}
}

// When the animation is done
if (!t48->acceptGameInput && !animationInProgress)
if (!t48->acceptGameInput && !(t48->cellsAnimating || sparklesAnimating))
{
// Check if game has been won
if (!t48->alreadyWon && t48_checkWin(t48))
Expand Down
2 changes: 1 addition & 1 deletion main/modes/games/2048/2048_game.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@

#include "mode_2048.h"

void t48_gameInit(t48_t* t48);
void t48_gameInit(t48_t* t48, bool tiltControls);
void t48_gameLoop(t48_t* t48, int32_t elapsedUs);
void t48_gameInput(t48_t* t48, buttonBit_t button);
16 changes: 11 additions & 5 deletions main/modes/games/2048/2048_menus.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
//==============================================================================

const char mode[] = "2048";
static const char pressKey[] = "Press any key to play";
static const char pressKeyA[] = "Press A to play with D-Pad";
static const char pressKeyB[] = "Press B to play with Tilt Controls";
static const char highScore[] = "You got a high score!";
static const char pressAB[] = "Press A or B to reset the game";

Expand Down Expand Up @@ -109,11 +110,16 @@ void t48_drawStartScreen(t48_t* t48, paletteColor_t color, int32_t elapsedUs)

// Draw current High Score
static char textBuffer[20];
snprintf(textBuffer, sizeof(textBuffer) - 1, "High score: %" PRIu32, t48->highScore[0]);
drawText(&t48->font, c444, textBuffer, (TFT_WIDTH - textWidth(&t48->font, textBuffer)) / 2, TFT_HEIGHT - 32);
int16_t yOff = TFT_HEIGHT - 64;

// Press button to start
drawText(&t48->font, c555, pressKeyA, (TFT_WIDTH - textWidth(&t48->font, pressKeyA)) / 2, yOff);
yOff += t48->font.height + 9;
drawText(&t48->font, c555, pressKeyB, (TFT_WIDTH - textWidth(&t48->font, pressKeyB)) / 2, yOff);
yOff += t48->font.height + 9;

// Press any key...
drawText(&t48->font, c555, pressKey, (TFT_WIDTH - textWidth(&t48->font, pressKey)) / 2, TFT_HEIGHT - 64);
snprintf(textBuffer, sizeof(textBuffer) - 1, "High score: %" PRIu32, t48->highScore[0]);
drawText(&t48->font, c444, textBuffer, (TFT_WIDTH - textWidth(&t48->font, textBuffer)) / 2, yOff);
}

/**
Expand Down
76 changes: 68 additions & 8 deletions main/modes/games/2048/mode_2048.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ static paletteColor_t t48_generateRainbow(void);
static void t48_fadeLEDs(int32_t elapsedUs);
static void t48_chaseLEDs(int32_t elapseUs);
static led_t t48_randColor(void);
static void t48BackgroundDraw(int16_t x, int16_t y, int16_t w, int16_t h, int16_t up, int16_t upNum);

//==============================================================================
// Const Variables
Expand Down Expand Up @@ -75,14 +76,14 @@ swadgeMode_t t48Mode = {
.modeName = modeName,
.wifiMode = NO_WIFI,
.overrideUsb = false,
.usesAccelerometer = false,
.usesAccelerometer = true,
.usesThermometer = false,
.overrideSelectBtn = false,
.fnEnterMode = t48EnterMode,
.fnExitMode = t48ExitMode,
.fnMainLoop = t48MainLoop,
.fnAudioCallback = NULL,
.fnBackgroundDrawCallback = NULL,
.fnBackgroundDrawCallback = t48BackgroundDraw,
.fnEspNowRecvCb = NULL,
.fnEspNowSendCb = NULL,
.fnAdvancedUSB = NULL,
Expand Down Expand Up @@ -129,9 +130,13 @@ static void t48EnterMode(void)
}

// Load sounds
loadMidiFile("Follinesque.mid", &t48->bgm, true);
loadMidiFile("lullaby_in_numbers.mid", &t48->bgm, true);
loadMidiFile("sndBounce.mid", &t48->click, true);

// Init sound player
midiPlayer_t* player = globalMidiPlayerGet(MIDI_BGM);
midiGmOn(player);

// Init Text Entry
textEntryInit(&t48->font, 4, t48->playerInitials);
textEntrySetBGColor(c001);
Expand Down Expand Up @@ -208,13 +213,48 @@ static void t48MainLoop(int64_t elapsedUs)
// Get inputs
while (checkButtonQueueWrapper(&evt))
{
if (evt.down)
bool isDpad = (PB_UP == evt.button) || //
(PB_DOWN == evt.button) || //
(PB_LEFT == evt.button) || //
(PB_RIGHT == evt.button);
// Accept buttons if it's not tilt, or not D-Pad
if (evt.down && (!t48->tiltControls || !isDpad))
{
// Process inputs
t48_gameInput(t48, evt.button);
}
}

// Take accel input if cells aren't moving
if (t48->tiltControls && !t48->cellsAnimating)
{
int16_t ox, oy, oz;
if (ESP_OK == accelGetOrientVec(&ox, &oy, &oz))
{
if (ABS(ox) > ABS(oy))
{
if (ox > 128)
{
t48_gameInput(t48, PB_LEFT);
}
else if (ox < -128)
{
t48_gameInput(t48, PB_RIGHT);
}
}
else
{
if (oy > 128)
{
t48_gameInput(t48, PB_DOWN);
}
else if (oy < -128)
{
t48_gameInput(t48, PB_UP);
}
}
}
}

// Loop the game
t48_gameLoop(t48, elapsedUs);
break;
Expand All @@ -224,10 +264,10 @@ static void t48MainLoop(int64_t elapsedUs)
// Check any button is pressed
while (checkButtonQueueWrapper(&evt))
{
if (evt.down)
if (evt.down && (PB_A == evt.button || PB_B == evt.button))
{
soundPlaySfx(&t48->click, MIDI_SFX);
t48_gameInit(t48);
t48_gameInit(t48, PB_B == evt.button);
t48->state = T48_IN_GAME;
}
}
Expand Down Expand Up @@ -495,4 +535,24 @@ led_t t48_randColor()
col.g = 128 + (esp_random() % 127);
col.b = 128 + (esp_random() % 127);
return col;
}
}

/**
* @brief Integrate the accelerometer in the background
*
* @param x Unused
* @param y Unused
* @param w Unused
* @param h Unused
* @param up Unused
* @param upNum Unused
*/
static void t48BackgroundDraw(int16_t x __attribute__((unused)), int16_t y __attribute__((unused)),
int16_t w __attribute__((unused)), int16_t h __attribute__((unused)),
int16_t up __attribute__((unused)), int16_t upNum __attribute__((unused)))
{
if (t48->tiltControls)
{
accelIntegrate();
}
}
2 changes: 2 additions & 0 deletions main/modes/games/2048/mode_2048.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ typedef struct
t48cell_t board[T48_GRID_SIZE][T48_GRID_SIZE]; ///< The board with cells, tiles, and sparkles
int32_t score; ///< The current score
bool acceptGameInput; ///< true if the game accepts input, false if it is animating
bool tiltControls; ///< true if tilt controls are used instead of buttons
bool cellsAnimating; ///< true if cells are in motion, false if at rest
bool paused; ///< If the game is paused
bool alreadyWon; ///< If the win screen has already displayed
t48ModeStateEnum_t state; ///< Where in the game sequence we are
Expand Down

0 comments on commit c5f3b01

Please sign in to comment.