Added animations for spawning of a new tile

This commit is contained in:
2026-03-08 00:22:45 +01:00
parent a47c6bd0b0
commit 3232e4f44f
3 changed files with 65 additions and 26 deletions
+12 -4
View File
@@ -1,10 +1,12 @@
#include "game.h" #include "game.h"
#include <raylib.h>
#include <stdbool.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
// Depending on the direction, move the tiles and check if we can merge with the // Depending on the direction, move the tiles and check if we can merge with the
// tiles in that direction. We merge the tiles only if they are of the same value // tiles in that direction. We merge the tiles only if they are of the same value
void move(GameState_t *state, move_direction_t direction) { bool move(GameState_t *state, move_direction_t direction) {
bool merged[BOARD_HEIGHT][BOARD_WIDTH] = {0}; bool merged[BOARD_HEIGHT][BOARD_WIDTH] = {0};
bool moved = false; bool moved = false;
switch (direction) { switch (direction) {
@@ -158,6 +160,8 @@ void move(GameState_t *state, move_direction_t direction) {
} }
if (moved) if (moved)
state->moves_made++; state->moves_made++;
return moved;
} }
void reset_board(GameState_t *state) { void reset_board(GameState_t *state) {
@@ -183,7 +187,7 @@ void reset_board(GameState_t *state) {
} }
} }
bool spawn_tile(GameState_t *state) { void spawn_tile(GameState_t *state, Vector2 *spawned_tile_pos) {
int empty_tiles[BOARD_WIDTH * BOARD_HEIGHT][2]; int empty_tiles[BOARD_WIDTH * BOARD_HEIGHT][2];
int empty_count = 0; int empty_count = 0;
@@ -198,13 +202,17 @@ bool spawn_tile(GameState_t *state) {
} }
if (empty_count == 0) if (empty_count == 0)
return false; return;
int random_index = rand() % empty_count; int random_index = rand() % empty_count;
int spawn_y = empty_tiles[random_index][0]; int spawn_y = empty_tiles[random_index][0];
int spawn_x = empty_tiles[random_index][1]; int spawn_x = empty_tiles[random_index][1];
state->board[spawn_y][spawn_x] = (rand() % 10 == 0) ? 4 : 2; // 10% chance to spawn a 4 state->board[spawn_y][spawn_x] = (rand() % 10 == 0) ? 4 : 2; // 10% chance to spawn a 4
return true;
spawned_tile_pos->x = spawn_x;
spawned_tile_pos->y = spawn_y;
return;
} }
// This function will only run, if every tile is filled (checked by `spawn_tile`). We can take advantage of that and // This function will only run, if every tile is filled (checked by `spawn_tile`). We can take advantage of that and
+2 -2
View File
@@ -16,7 +16,7 @@ typedef struct {
unsigned int moves_made; unsigned int moves_made;
} GameState_t; } GameState_t;
void move(GameState_t *state, move_direction_t direction); bool move(GameState_t *state, move_direction_t direction);
void reset_board(GameState_t *state); void reset_board(GameState_t *state);
bool spawn_tile(GameState_t *state); void spawn_tile(GameState_t *state, Vector2 *spawned_tile_pos);
bool can_move(GameState_t *state); bool can_move(GameState_t *state);
+41 -10
View File
@@ -26,13 +26,17 @@ static bool is_dragging;
static Vector2 drag_start_pos; static Vector2 drag_start_pos;
static bool is_animating; static bool is_animating;
// If there isn't a tile to animate then store it as {-1,-1}
static Vector2 new_tile = {-1, 1};
static float new_tile_animation_progress;
// Common logic shared between both input handlers // Common logic shared between both input handlers
static void after_input(GameState_t *game_state) { static void after_input(GameState_t *game_state) {
if (!spawn_tile(game_state) && !can_move(game_state)) { spawn_tile(game_state, &new_tile);
if (!can_move(game_state)) {
is_animating = false; // No more moves, so we won't be animating anymore
game_state->status = LOST; game_state->status = LOST;
return; } else {
}
// We moved, merged or spawned a tile, so we will be animating // We moved, merged or spawned a tile, so we will be animating
is_animating = true; is_animating = true;
@@ -45,6 +49,7 @@ static void after_input(GameState_t *game_state) {
} }
} }
} }
}
FILE *f = fopen("save.dat", "wb"); FILE *f = fopen("save.dat", "wb");
if (f) { if (f) {
@@ -111,23 +116,41 @@ void handle_keyboard_input(GameState_t *game_state) {
reset_board(game_state); reset_board(game_state);
} }
bool moved = false;
if (IsKeyReleased(KEY_UP)) { if (IsKeyReleased(KEY_UP)) {
move(game_state, UP); moved = move(game_state, UP);
} else if (IsKeyReleased(KEY_DOWN)) { } else if (IsKeyReleased(KEY_DOWN)) {
move(game_state, DOWN); moved = move(game_state, DOWN);
} else if (IsKeyReleased(KEY_LEFT)) { } else if (IsKeyReleased(KEY_LEFT)) {
move(game_state, LEFT); moved = move(game_state, LEFT);
} else if (IsKeyReleased(KEY_RIGHT)) { } else if (IsKeyReleased(KEY_RIGHT)) {
move(game_state, RIGHT); moved = move(game_state, RIGHT);
} else {
return;
} }
if (moved) {
after_input(game_state); after_input(game_state);
}
}
void animate_new_tile() {
if (new_tile.x == -1 && new_tile.y == -1)
return;
// Simple pop-in animation that scales the tile from 0 to full size
new_tile_animation_progress += GetFrameTime() * 5; // Adjust speed as needed
if (new_tile_animation_progress >= 1.0f) {
new_tile_animation_progress = 0.3f;
new_tile = (Vector2){-1, -1}; // Reset after animation completes
is_animating = false;
}
} }
// Main render loop, responsible ONLY for redering // Main render loop, responsible ONLY for redering
void render(GameState_t *game_state) { void render(GameState_t *game_state) {
if (is_animating) {
animate_new_tile();
}
for (int y = 0; y < BOARD_HEIGHT; y++) { for (int y = 0; y < BOARD_HEIGHT; y++) {
for (int x = 0; x < BOARD_WIDTH; x++) { for (int x = 0; x < BOARD_WIDTH; x++) {
int tile_value = game_state->board[y][x]; int tile_value = game_state->board[y][x];
@@ -139,7 +162,14 @@ void render(GameState_t *game_state) {
color_index = sizeof(tile_colors) / sizeof(tile_colors[0]) - 1; // Cap to the last color color_index = sizeof(tile_colors) / sizeof(tile_colors[0]) - 1; // Cap to the last color
} }
if (is_animating && new_tile.x == x && new_tile.y == y) {
float scale = new_tile_animation_progress; // Scale from 0 to 1
int scaled_size = (int)(TILE_SIZE * scale);
int offset = (TILE_SIZE - scaled_size) / 2;
DrawRectangle(pos_x + offset, pos_y + offset, scaled_size, scaled_size, tile_colors[color_index]);
} else {
DrawRectangle(pos_x, pos_y, TILE_SIZE, TILE_SIZE, tile_colors[color_index]); DrawRectangle(pos_x, pos_y, TILE_SIZE, TILE_SIZE, tile_colors[color_index]);
}
if (tile_value != 0) { if (tile_value != 0) {
const char *text = TextFormat("%d", tile_value); const char *text = TextFormat("%d", tile_value);
DrawText(text, pos_x + TILE_SIZE / 2 - MeasureText(text, 20) / 2, pos_y + TILE_SIZE / 2 - 10, 20, BLACK); DrawText(text, pos_x + TILE_SIZE / 2 - MeasureText(text, 20) / 2, pos_y + TILE_SIZE / 2 - 10, 20, BLACK);
@@ -150,6 +180,7 @@ void render(GameState_t *game_state) {
DrawText(TextFormat("Score: %d", game_state->score), WINDOW_HEIGHT, BOARD_MARGIN, 20, BLACK); DrawText(TextFormat("Score: %d", game_state->score), WINDOW_HEIGHT, BOARD_MARGIN, 20, BLACK);
DrawText(TextFormat("Moves used: %d", game_state->moves_made), WINDOW_HEIGHT, BOARD_MARGIN + 20, 20, BLACK); DrawText(TextFormat("Moves used: %d", game_state->moves_made), WINDOW_HEIGHT, BOARD_MARGIN + 20, 20, BLACK);
DrawText("By Piotr Kozak", WINDOW_WIDTH - MeasureText("By Piotr Kozak", 16) - 10, WINDOW_HEIGHT - 24, 16, GRAY);
if (game_state->status == PLAYING) { if (game_state->status == PLAYING) {
DrawText("Drag with the mouse to move tiles", 400 - MeasureText("Drag with the mouse to move tiles", 20) / 2, DrawText("Drag with the mouse to move tiles", 400 - MeasureText("Drag with the mouse to move tiles", 20) / 2,
WINDOW_HEIGHT - 40, 20, GRAY); WINDOW_HEIGHT - 40, 20, GRAY);