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 <raylib.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
// 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
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 moved = false;
switch (direction) {
@@ -158,6 +160,8 @@ void move(GameState_t *state, move_direction_t direction) {
}
if (moved)
state->moves_made++;
return moved;
}
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_count = 0;
@@ -198,13 +202,17 @@ bool spawn_tile(GameState_t *state) {
}
if (empty_count == 0)
return false;
return;
int random_index = rand() % empty_count;
int spawn_y = empty_tiles[random_index][0];
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
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