Skip to content

Commit

Permalink
Needs more glue logic, but I'm tired
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnywycliffe committed Sep 7, 2024
1 parent 3ce896a commit c7c7dc8
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 103 deletions.
98 changes: 19 additions & 79 deletions main/modes/games/2048/T48_animations.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,19 @@
*/
#include "T48_animations.h"

static void t48StartTileMoving(t48_t* t48, int8_t idx, t48Cell_t* start, t48Cell_t* end)
{
t48->mvTiles[idx].start = *start;
t48->mvTiles[idx].end = *end;
t48->mvTiles[idx].progress = 0;
t48->mvTiles[idx].speed = (end->x - start->x + end->y - start->y) * (T48_CELL_SIZE + T48_LINE_WEIGHT) / T48_MAX_SEQ;
}

void t48ResetAnim(t48_t* t48)
{
// Reset animation timer
t48->globalAnim = 0;
for (int i = 0; i < T48_BOARD_SIZE; i++)
{
t48->board[i].state = STATIC;
}
for (int i = 0; i < T48_MAX_MOVES; i++)
{
t48Cell_t cell = {0};
t48->mvTiles[i].start = cell;
t48->mvTiles[i].end = cell;
t48->mvTiles[i].speed = 0;
}
}

void t48InitSparkles(t48_t* t48, int8_t idx, int8_t x, int8_t y, wsg_t spr)
Expand All @@ -44,82 +41,25 @@ void t48InitSparkles(t48_t* t48, int8_t idx, int8_t x, int8_t y, wsg_t spr)
t48->sparks[idx].active = true;
}

void t48InitMovingTiles(t48_t* t48)
void t48InitMovingTilesVert(t48_t* t48)
{
for (int i = 0; i < T48_GRID_SIZE; i++)
{
if (t48->sliceData.src[i] == t48->sliceData.dest[i] || t48->sliceData.src[i] == -1)
if (t48->sliceData.src[i] == -1 || t48->sliceData.startVals[i] == 0)
{
// Static
continue;
}
// FIXME: Convert 1d coords to 2d coords
// FIXME: Need better glue logic between the math and the shuffly bits.
t48Cell_t start, end;
start.val = 4;
start.x = 0;
start.y = 3;
end.x = 0;
end.y = 0;
t48StartTileMoving(t48, 0, &start, &end);
}
}
start.val = t48->sliceData.startVals[i];
start.x = t48->sliceData.src[i];
start.y = t48->sliceData.lockedCoord;
end.x = i;
end.y = t48->sliceData.lockedCoord;
t48->mvTiles[t48->tileIdx].start = start;
t48->mvTiles[t48->tileIdx].end = end;
t48->mvTiles[t48->tileIdx++].speed
= (end.x - start.x) * (T48_CELL_SIZE + T48_LINE_WEIGHT) / T48_MAX_SEQ;

/* void t48ResetCellState(t48_t* t48)
{
for (int8_t i = 0; i < T48_BOARD_SIZE; i++)
{
t48->board[i].state = STATIC;
}
for (int8_t i = 0; i < T48_MAX_MERGES; i++)
{
t48->slidingTiles[i].speed = 0;
t48->slidingTiles[i].value = 0;
t48->slidingTiles[i].sequence = 0;
}
}
void t48SetCellState(t48_t* t48, int8_t idx, t48CellStateEnum_t st, int8_t startX, int8_t startY, int8_t endX,
int8_t endY, int32_t value)
{
t48->prevBoard[idx].x = startX;
t48->prevBoard[idx].y = startY;
t48->board[idx].x = endX;
t48->board[idx].y = endY;
t48->board[idx].state = st;
t48->board[idx].val = value;
}
void t48UpdateCells(t48_t* t48)
{
int8_t idx = 0;
t48->globalAnim = 0;
for (int8_t i = 0; i < 12; i++)
{
switch (t48->board[i].state)
{
case MOVING:
t48SetSlidingTile(t48, idx++, t48->prevBoard[i], t48->board[i],
t48->board[i].val);
t48->board[t48->board[i].x * T48_GRID_SIZE + t48->board[i].y].state = MOVED;
break;
case MERGED:
// FIXME: Move this to where the final cell is merged
// t48->cellState[t48->cellState[i].end.x * T48_GRID_SIZE + t48->cellState[i].end.y].state = MERGED;
break;
case NEW:
break;
default:
break;
}
}
}
void t48SetSlidingTile(t48_t* t48, int8_t idx, t48Cell_t start, t48Cell_t end, int32_t value)
{
t48->slidingTiles[idx].value = value;
t48->slidingTiles[idx].gridStart.x = start.x;
t48->slidingTiles[idx].gridStart.y = start.y;
t48->slidingTiles[idx].speed
= (end.x - start.x + end.y - start.y) * (T48_CELL_SIZE + T48_LINE_WEIGHT) / T48_MAX_SEQ;
t48->slidingTiles[idx].horizontal = (end.x != start.x);
} */
11 changes: 1 addition & 10 deletions main/modes/games/2048/T48_animations.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,4 @@ void t48ResetAnim(t48_t* t48);

void t48InitSparkles(t48_t* t48, int8_t idx, int8_t x, int8_t y, wsg_t spr);

void t48InitMovingTiles(t48_t* t48);

/* void t48ResetCellState(t48_t* t48);
void t48SetCellState(t48_t* t48, int8_t idx, t48CellStateEnum_t st, int8_t startX, int8_t startY, int8_t endX,
int8_t endY, int32_t value);
void t48UpdateCells(t48_t* t48);
void t48SetSlidingTile(t48_t* t48, int8_t idx, t48Cell_t start, t48Cell_t end, int32_t value); */
void t48InitMovingTilesVert(t48_t* t48);
5 changes: 3 additions & 2 deletions main/modes/games/2048/T48_draw.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ static void t48DrawSlidingTiles(t48_t* t48)
yVal = t48->globalAnim * t48->mvTiles[idx].speed;
}
t48DrawTileOnGrid(t48, &t48->tiles[getTileSprIdx(t48->mvTiles[idx].start.val)],
t48->mvTiles[idx].start.y, t48->mvTiles[idx].start.x, xVal, yVal,
t48->mvTiles[idx].start.x, t48->mvTiles[idx].start.y, xVal, yVal,
t48->mvTiles[idx].start.val);
}
}
Expand All @@ -183,7 +183,8 @@ static void t48DrawTiles(t48_t* t48)
// New tiles

// Moving tiles
t48DrawSlidingTiles(t48);
if (t48->globalAnim < T48_MAX_SEQ)
t48DrawSlidingTiles(t48);

// Static tiles
int8_t sparkleIdx = 0;
Expand Down
12 changes: 11 additions & 1 deletion main/modes/games/2048/T48_logic.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ static void t48MergeSlice(t48_t* t48, bool* updated)

void t48SlideDown(t48_t* t48)
{
// Reset animation timer
t48->globalAnim = 0;
t48ResetAnim(t48);
bool updated = false;
for (uint8_t row = 0; row < T48_GRID_SIZE; row++)
Expand Down Expand Up @@ -138,6 +140,9 @@ void t48SlideDown(t48_t* t48)

void t48SlideUp(t48_t* t48)
{
// Reset animation timer
t48->globalAnim = 0;
t48->tileIdx = 0;
t48ResetAnim(t48);
bool updated = false;
for (uint8_t row = 0; row < T48_GRID_SIZE; row++)
Expand All @@ -149,6 +154,7 @@ void t48SlideUp(t48_t* t48)
t48->sliceData.dest[i] = i;
t48->sliceData.startVals[i] = 0;
t48->sliceData.endVals[i] = 0;
t48->sliceData.lockedCoord = row;
// Math
t48->sliceData.slice[i].val = 0;
t48->sliceData.slice[i].x = -1;
Expand Down Expand Up @@ -176,7 +182,7 @@ void t48SlideUp(t48_t* t48)
}
}
t48MergeSlice(t48, &updated);
t48InitMovingTiles(t48);
t48InitMovingTilesVert(t48);
for (int8_t col = 0, i = 0; col <= T48_GRID_SIZE - 1; col++)
{
t48->board[(row * 4) + col].val = t48->sliceData.slice[i].val;
Expand All @@ -188,6 +194,8 @@ void t48SlideUp(t48_t* t48)

void t48SlideRight(t48_t* t48)
{
// Reset animation timer
t48->globalAnim = 0;
t48ResetAnim(t48);
bool updated = false;
for (uint8_t col = 0; col < T48_GRID_SIZE; col++)
Expand Down Expand Up @@ -224,6 +232,8 @@ void t48SlideRight(t48_t* t48)

void t48SlideLeft(t48_t* t48)
{
// Reset animation timer
t48->globalAnim = 0;
t48ResetAnim(t48);
bool updated = false;
for (uint8_t col = 0; col < T48_GRID_SIZE; col++)
Expand Down
10 changes: 5 additions & 5 deletions main/modes/games/2048/mode_2048.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ static void t48MainLoop(int64_t elapsedUs)
// Play BGM if it's not playing
if (!t48->bgmIsPlaying)
{
//soundPlayBgmCb(&t48->bgm, MIDI_BGM, t48BgmCb);
soundPlayBgmCb(&t48->bgm, MIDI_BGM, t48BgmCb);
t48->bgmIsPlaying = true;
}
// Get inputs
Expand All @@ -229,10 +229,10 @@ static void t48MainLoop(int64_t elapsedUs)
soundPlaySfx(&t48->click, MIDI_SFX);
t48StartGame(t48);
t48->ds = GAME;
t48->board[0].val = 2;
t48->board[0].val = 0;
t48->board[0].val = 2;
t48->board[0].val = 4;
/* t48->board[0].val = 2;
t48->board[1].val = 0;
t48->board[2].val = 2;
t48->board[3].val = 4; */
}
}
// Draw
Expand Down
13 changes: 7 additions & 6 deletions main/modes/games/2048/mode_2048.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@
#define T48_MAX_SPARKLES 24

// Animations
#define T48_MAX_SEQ 16 // Frames per animation. Less is faster.
#define T48_MAX_SEQ 50 // Frames per animation. Less is faster.
#define T48_SPARKLE_COUNT 8
#define T48_SPARKLE_SIZE 16
#define T48_MAX_MOVES 12
#define T48_MAX_MOVES 16
#define T48_MAX_MERGES 8

// High score
Expand Down Expand Up @@ -135,22 +135,22 @@ typedef struct
bool active;
} t48Sparkles_t;

typedef struct
typedef struct
{
//TODO: Remove redundancies
// TODO: Remove redundancies
int8_t src[T48_GRID_SIZE];
int8_t dest[T48_GRID_SIZE];
int8_t lockedCoord;
uint32_t startVals[T48_GRID_SIZE];
uint32_t endVals[T48_GRID_SIZE];
t48Cell_t slice[T48_GRID_SIZE];
} t48SliceData_t;

typedef struct
{
//wsg_t img;
// wsg_t img;
t48Cell_t start;
t48Cell_t end;
int16_t progress;
int16_t speed;
} t48MovingTile_t;

Expand Down Expand Up @@ -190,6 +190,7 @@ typedef struct
int8_t globalAnim;
t48Sparkles_t sparks[T48_MAX_SPARKLES];
t48MovingTile_t mvTiles[T48_MAX_MOVES];
int8_t tileIdx;

// OLD
// t48SlidingTile_t slidingTiles[12]; // Max amount of sliding tiles
Expand Down

0 comments on commit c7c7dc8

Please sign in to comment.