Addedd animations when moving tiles

Looks weird for merging animation, it's because one of the tiles isn't refered as its origin by the merged one, so it won't appears.
* Solution 1: Make a Grid with previous tiles of which animation is based with nextPosition instead
* Solution 2: Make two previousPosition. It does what the original one does, however less stable (affects every new tile definition)
This commit is contained in:
Geoffrey Frogeye 2014-04-05 19:11:02 +02:00
parent a1301e94a0
commit e363b2872b
4 changed files with 274 additions and 79 deletions

BIN
2048.G1A

Binary file not shown.

175
2048.c
View file

@ -54,43 +54,24 @@ typedef struct findFarthestPosition_return {
int Game_score = 0;
bool Game_over = false;
bool Game_won = false;
bool Game_terminated = false;
bool Game_keepPlaying = true;
int storage_bestScore = 0;
Grid Grid_grid;
// Fonctions
// Usual functions
int rand_int(int min, int max) {
return min + (rand() % (int)(max - min + 1));
}
int drawFixedTiles() {
int x, y;
for (x = 0; x <= 3; x++) {
for (y = 0; y <= 3; y++) {
drawTileCase(Grid_grid.array[x][y]);
}
}
}
int drawTile(int x, int y, int value) {
ML_rectangle(x + 1, y + 1, x + 11, y + 11, 0, ML_TRANSPARENT, ML_WHITE);
ML_bmp_or(tile[value], x, y, 13, 13);
}
int drawTileCase(Tile tile) {
drawTile(5+tile.x*14, 5+tile.y*14, tile.value);
}
// Grid
bool Grid_withinBounds(Cell position) {
return (position.x >= 0 && position.x < 4 && position.y >= 0 && position.y < 4);
}
Tile Grid_cellContent(Cell cell) {
Tile Grid_cellContent(Cell cell) { // TODO Make cellContentEdit(Cell cell, Tile tile), because it's read-only this way
Tile back;
if (Grid_withinBounds(cell)) {
return Grid_grid.array[cell.x][cell.y];
@ -112,16 +93,16 @@ int Grid_insertTile(Tile tile) {
Grid_grid.array[tile.x][tile.y] = tile;
}
int Grid_removeTile(Tile tile) {
int Grid_removeTile(Tile tile) { // TODO use with cellContentEdit
Cell emptyCell;
Tile emptyTile;
emptyCell.x = -1;
emptyCell.y = -1;
emptyTile.x = tile.x;
emptyTile.y = tile.y;
emptyTile.value = 0;
emptyTile.hasMerged = false;
emptyCell.x = -1;
emptyCell.y = -1;
emptyTile.previousPosition = emptyCell;
Grid_grid.array[tile.x][tile.y] = emptyTile;
@ -129,12 +110,12 @@ int Grid_removeTile(Tile tile) {
int Grid_avaiableCellsAmount() {
int avaiableCellsNumber = 0, x, y;
Cell testCell;
Cell position;
for (x = 0; x <= 3; x++) {
for (y = 0; y <= 3; y++) {
testCell.x = x;
testCell.y = y;
if (Grid_cellAvailable(testCell)) {
position.x = x;
position.y = y;
if (Grid_cellAvailable(position)) {
avaiableCellsNumber++;
}
}
@ -149,12 +130,65 @@ void storage_setBestScore(int bestScore) {
// Screen (O HTML_Actuator)
void Screen_updateScore() {
void Screen_drawTile(int x, int y, int value) {
ML_rectangle(x + 1, y + 1, x + 11, y + 11, 0, ML_TRANSPARENT, ML_WHITE);
ML_bmp_or(tile[value], x, y, 13, 13);
//ML_display_vram(); // DEBUG
}
void Screen_drawTileCase(Tile tile) {
Screen_drawTile(5+tile.x*14, 5+tile.y*14, tile.value);
}
void Screen_drawTileMoving(Tile tile, float percentage) {
int x, y, OTx, OTy, NTx, NTy;
NTx = 5+tile.x*14;
NTy = 5+tile.y*14;
OTx = 5+tile.previousPosition.x*14;
OTy = 5+tile.previousPosition.y*14;
x = OTx + (NTx - OTx) * percentage;
y = OTy + (NTy - OTy) * percentage;
Screen_drawTile(x, y, tile.value);
}
int Screen_drawFixedTiles(bool dontDrawPreviousPositionTiles) {
int x, y;
Tile tile;
Cell position;
for (x = 0; x <= 3; x++) {
for (y = 0; y <= 3; y++) {
position.x = x;
position.y = y;
tile = Grid_cellContent(position);
if (dontDrawPreviousPositionTiles && ((tile.previousPosition.x >= 0 && tile.previousPosition.y >= 0) || (tile.previousPosition.x == -2 && tile.previousPosition.y == -2))) {
tile.value = 0;
}
Screen_drawTileCase(tile);
}
}
}
int Screen_drawMovingTiles(float percentage) {
int x, y;
Tile tile;
Cell position;
for (x = 0; x <= 3; x++) {
for (y = 0; y <= 3; y++) {
position.x = x;
position.y = y;
tile = Grid_cellContent(position);
if (tile.previousPosition.x >= 0 && tile.previousPosition.y >= 0) {
Screen_drawTileMoving(tile, percentage);
}
}
}
}
void Screen_updateScore() {
PrintXY(100, 14, "0", 1);
}
void Screen_updateBestScore() {
PrintXY(100, 35, "0", 1);
}
void Screen_message(bool won) {
@ -165,14 +199,31 @@ void Screen_message(bool won) {
}
}
bool Game_isGameTerminated() { // Intentionally moved here
return (Game_over || (Game_won && !Game_keepPlaying));
}
void Screen_actuate() {
drawFixedTiles(); // O self.addTile(cell);
float i;
Screen_updateScore(); // O self.updateScore(metadata.score);
Screen_updateBestScore(); // O self.updateBestScore(metadata.bestScore);
Screen_drawFixedTiles(true);
if (Game_terminated) {
SaveDisp(SAVEDISP_PAGE1);
for (i = 0; i <= 1; i += 0.02) {
RestoreDisp(SAVEDISP_PAGE1);
Screen_drawMovingTiles(i);
ML_display_vram();
}
RestoreDisp(SAVEDISP_PAGE1);
Screen_drawFixedTiles(false);
Screen_updateScore();
Screen_updateBestScore();
if (Game_isGameTerminated()) {
if (Game_over) {
Screen_message(false);
} else if (Game_won) {
@ -185,9 +236,6 @@ void Screen_actuate() {
// Game (O Game_manager)
bool Game_isGameTerminated() {
return (Game_over || (Game_won && !Game_keepPlaying));
}
void Game_actuate() {
if (storage_bestScore < Game_score) {
@ -231,8 +279,8 @@ void Game_prepareTiles() {
for (x = 0; x <= 3; x++) {
for (y = 0; y <= 3; y++) {
Grid_grid.array[x][y].hasMerged = false;
previousPosition.x = Grid_grid.array[x][y].x;
previousPosition.y = Grid_grid.array[x][y].y;
previousPosition.x = -1;
previousPosition.y = -1;
Grid_grid.array[x][y].previousPosition = previousPosition;
}
}
@ -255,11 +303,19 @@ findFarthestPosition_return Game_findFarthestPosition(Cell cell, Cell vector) {
return back;
}
Game_moveTile(Tile tile, Cell cell) {
bool Game_moveTile(Tile tile, Cell cell) {
Cell previousPosition;
if (tile.x == cell.x && tile.y == cell.y) {
return false;
} else {
Grid_removeTile(tile);
previousPosition.x = tile.x;
previousPosition.y = tile.y;
tile.previousPosition = previousPosition;
tile.x = cell.x;
tile.y = cell.y;
Grid_insertTile(tile);
}
}
bool Game_positionsEqual(Cell first, Tile second) {
@ -287,15 +343,19 @@ Cell Grid_randomAvaiableCell() {
}
}
// Game (O game_manager)
void Game_addRandomTile() {
Tile tile; Cell position;
Tile tile; Cell position, previousPosition;
if (Grid_avaiableCellsAmount() > 0) {
position = Grid_randomAvaiableCell();
tile.value = (rand_int(0, 10) < 9 ? 1 : 2);
tile.x = position.x;
tile.y = position.y;
tile.previousPosition = position;
previousPosition.x = -2;
previousPosition.y = -2;
tile.previousPosition = previousPosition;
tile.hasMerged = false;
Grid_insertTile(tile);
}
@ -332,9 +392,6 @@ void Game_move(int direction) { // 0: up, 1: right, 2: down, 3: left
Grid_insertTile(merged);
Grid_removeTile(tile);
tile.x = next.x;
tile.y = next.y;
Game_score += merged.value;
if (merged.value == 11) {
@ -342,12 +399,13 @@ void Game_move(int direction) { // 0: up, 1: right, 2: down, 3: left
}
moved = true;
} else {
Game_moveTile(tile, farthest);
if (Game_moveTile(tile, farthest)) {
moved = true;
}
}
}
}
}
if (moved) {
Game_addRandomTile();
@ -364,36 +422,39 @@ void Game_move(int direction) { // 0: up, 1: right, 2: down, 3: left
int initGame() {
// Variables
int x, y;
ML_clear_screen();
// Draw Title
PrintXY(67, 54, "2048", 0);
Cell emptyCell;
// Reset variables
Game_score = 0;
Game_over = false;
Game_won = false;
Game_terminated = false;
Game_keepPlaying = true;
emptyCell.x = -1;
emptyCell.y = -1;
for (x = 0; x <= 3; x++) {
for (y = 0; y <= 3; y++) {
Grid_grid.array[x][y].x = x;
Grid_grid.array[x][y].y = y;
Grid_grid.array[x][y].value = 0;
Grid_grid.array[x][y].hasMerged = false;
Grid_grid.array[x][y].previousPosition = emptyCell;
}
}
drawFixedTiles();
ML_clear_screen();
// Draw Title
PrintXY(67, 54, "2048", 0);
Screen_drawFixedTiles(true);
// Draw Score
ML_bmp_or(scoreBG, 68, 4, 41, 19);
ML_bmp_or(scoreBG, 68, 25, 41, 19);
PrintXY(70, 6, "SCORE", 1);
PrintXY(100, 14, "0", 1);
PrintXY(70, 27, "BEST", 1);
PrintXY(100, 35, "0", 1);
Game_addRandomTile();
Game_addRandomTile();
@ -425,13 +486,13 @@ int AddIn_main(int isAppli, unsigned short OptionNum) {
case KEY_CTRL_DEL:
initGame();
break;
case KEY_CHAR_PLUS: // DEBUG
/*case KEY_CHAR_PLUS: // DEBUG
Game_addRandomTile();
Game_actuate();
break;
case KEY_CHAR_STORE: // DEBUG
Game_actuate();
break;
break;*/
default:
break;
}

134
2048.dlr
View file

@ -35,6 +35,7 @@ Sublevel=1
File=Z:\home\geoffrey\Documents\Programmation\CASIO\2048\2048.c
Line=150
Flags=00001012
Sublevel=1
[Breakpoint]
File=Z:\home\geoffrey\Documents\Programmation\CASIO\2048\2048.c
@ -46,11 +47,13 @@ Sublevel=1
File=Z:\home\geoffrey\Documents\Programmation\CASIO\2048\2048.c
Line=124
Flags=00001012
Sublevel=1
[Breakpoint]
File=Z:\home\geoffrey\Documents\Programmation\CASIO\2048\2048.c
Line=151
Flags=00001012
Sublevel=1
[Breakpoint]
File=Z:\home\geoffrey\Documents\Programmation\CASIO\2048\2048.c
@ -62,11 +65,13 @@ Sublevel=1
File=Z:\home\geoffrey\Documents\Programmation\CASIO\2048\2048.c
Line=157
Flags=00001012
Sublevel=1
[Breakpoint]
File=Z:\home\geoffrey\Documents\Programmation\CASIO\2048\2048.c
Line=158
Flags=00001012
Sublevel=1
[Breakpoint]
File=Z:\home\geoffrey\Documents\Programmation\CASIO\2048\2048.c
@ -152,6 +157,7 @@ Sublevel=2
File=Z:\home\geoffrey\Documents\Programmation\CASIO\2048\2048.c
Line=208
Flags=00001012
Sublevel=1
[Breakpoint]
File=Z:\home\geoffrey\Documents\Programmation\CASIO\2048\2048.c
@ -197,6 +203,7 @@ Flags=00001012
File=Z:\home\geoffrey\Documents\Programmation\CASIO\2048\2048.c
Line=316
Flags=00001012
Sublevel=1
[Breakpoint]
File=Z:\home\geoffrey\Documents\Programmation\CASIO\2048\2048.c
@ -226,6 +233,7 @@ Sublevel=1
File=Z:\home\geoffrey\Documents\Programmation\CASIO\2048\2048.c
Line=329
Flags=00001012
Sublevel=1
[Breakpoint]
File=Z:\home\geoffrey\Documents\Programmation\CASIO\2048\2048.c
@ -261,6 +269,7 @@ Sublevel=1
File=Z:\home\geoffrey\Documents\Programmation\CASIO\2048\2048.c
Line=335
Flags=00001012
Sublevel=1
[Breakpoint]
File=Z:\home\geoffrey\Documents\Programmation\CASIO\2048\2048.c
@ -296,6 +305,7 @@ Sublevel=3
File=Z:\home\geoffrey\Documents\Programmation\CASIO\2048\2048.c
Line=339
Flags=00001012
Sublevel=1
[Breakpoint]
File=Z:\home\geoffrey\Documents\Programmation\CASIO\2048\2048.c
@ -306,11 +316,13 @@ Flags=00001012
File=Z:\home\geoffrey\Documents\Programmation\CASIO\2048\2048.c
Line=341
Flags=00001012
Sublevel=1
[Breakpoint]
File=Z:\home\geoffrey\Documents\Programmation\CASIO\2048\2048.c
Line=347
Flags=00001012
Sublevel=1
[Breakpoint]
File=Z:\home\geoffrey\Documents\Programmation\CASIO\2048\2048.c
@ -328,6 +340,7 @@ Sublevel=2
File=Z:\home\geoffrey\Documents\Programmation\CASIO\2048\2048.c
Line=355
Flags=00001012
Sublevel=1
[Breakpoint]
File=Z:\home\geoffrey\Documents\Programmation\CASIO\2048\2048.c
@ -353,3 +366,124 @@ Flags=00001012
File=Z:\home\geoffrey\Documents\Programmation\CASIO\2048\2048.c
Line=476
Flags=00001012
[Breakpoint]
File=Z:\home\geoffrey\Documents\Programmation\CASIO\2048\2048.c
Line=124
Flags=00001012
Sublevel=2
[Breakpoint]
File=Z:\home\geoffrey\Documents\Programmation\CASIO\2048\2048.c
Line=316
Flags=00001012
Sublevel=2
[Breakpoint]
File=Z:\home\geoffrey\Documents\Programmation\CASIO\2048\2048.c
Line=329
Flags=00001012
Sublevel=2
[Breakpoint]
File=Z:\home\geoffrey\Documents\Programmation\CASIO\2048\2048.c
Line=339
Flags=00001012
Sublevel=2
[Breakpoint]
File=Z:\home\geoffrey\Documents\Programmation\CASIO\2048\2048.c
Line=341
Flags=00001012
Sublevel=2
[Breakpoint]
File=Z:\home\geoffrey\Documents\Programmation\CASIO\2048\2048.c
Line=150
Flags=00001012
Sublevel=2
[Breakpoint]
File=Z:\home\geoffrey\Documents\Programmation\CASIO\2048\2048.c
Line=151
Flags=00001012
Sublevel=2
[Breakpoint]
File=Z:\home\geoffrey\Documents\Programmation\CASIO\2048\2048.c
Line=157
Flags=00001012
Sublevel=2
[Breakpoint]
File=Z:\home\geoffrey\Documents\Programmation\CASIO\2048\2048.c
Line=157
Flags=00001012
Sublevel=3
[Breakpoint]
File=Z:\home\geoffrey\Documents\Programmation\CASIO\2048\2048.c
Line=158
Flags=00001012
Sublevel=2
[Breakpoint]
File=Z:\home\geoffrey\Documents\Programmation\CASIO\2048\2048.c
Line=175
Flags=00001012
Sublevel=1
[Breakpoint]
File=Z:\home\geoffrey\Documents\Programmation\CASIO\2048\2048.c
Line=175
Flags=00001012
Sublevel=2
[Breakpoint]
File=Z:\home\geoffrey\Documents\Programmation\CASIO\2048\2048.c
Line=140
Flags=00001012
[Breakpoint]
File=Z:\home\geoffrey\Documents\Programmation\CASIO\2048\2048.c
Line=137
Flags=00001012
[Breakpoint]
File=Z:\home\geoffrey\Documents\Programmation\CASIO\2048\2048.c
Line=208
Flags=00001012
Sublevel=2
[Breakpoint]
File=Z:\home\geoffrey\Documents\Programmation\CASIO\2048\2048.c
Line=347
Flags=00001012
Sublevel=2
[Breakpoint]
File=Z:\home\geoffrey\Documents\Programmation\CASIO\2048\2048.c
Line=238
Flags=00001012
[Breakpoint]
File=Z:\home\geoffrey\Documents\Programmation\CASIO\2048\2048.c
Line=245
Flags=00001012
[Breakpoint]
File=Z:\home\geoffrey\Documents\Programmation\CASIO\2048\2048.c
Line=416
Flags=00001012
[Breakpoint]
File=Z:\home\geoffrey\Documents\Programmation\CASIO\2048\2048.c
Line=335
Flags=00001012
Sublevel=2
[Breakpoint]
File=Z:\home\geoffrey\Documents\Programmation\CASIO\2048\2048.c
Line=355
Flags=00001012
Sublevel=2

View file

@ -2,19 +2,19 @@
[_1]
Type=5
Order=1
Order=0
Top=60
Left=1965
Height=10080
Height=8295
Width=10470
State=0
Flags=00000020
Setting=298:9:2048.c
Setting=417:1:2048.c
OptionA=0
[_2]
Type=1
Order=2
Order=1
Top=0
Left=12645
Height=4665
@ -37,10 +37,10 @@ OptionA=0
[_4]
Type=7
Order=6
Top=10095
Order=5
Top=8400
Left=2040
Height=3120
Height=4815
Width=5145
State=0
Flags=00000000
@ -48,10 +48,10 @@ OptionA=0
[_5]
Type=8
Order=7
Top=10155
Left=7230
Height=3150
Order=4
Top=8400
Left=7200
Height=4845
Width=5475
State=0
Flags=00000000
@ -81,7 +81,7 @@ OptionA=0
[_8]
Type=17
Order=5
Order=7
Top=30
Left=30
Height=13200
@ -92,7 +92,7 @@ OptionA=0
[_9]
Type=15
Order=0
Order=2
Top=0
Left=2010
Height=11580
@ -103,24 +103,24 @@ OptionA=0
[_32]
Type=16
Order=8
Order=6
Top=15
Left=1995
Height=10095
Width=10710
State=0
Flags=00000020
Setting=209:1:2048.c
Setting=412:1:2048.c
OptionA=0
[_33]
Type=16
Order=4
Order=8
Top=990
Left=990
Height=9060
Width=13635
State=0
State=16
Flags=00000020
Setting=1:1:MonochromeLib.c
OptionA=0