Working on the bombs

This commit is contained in:
Alex Vasilev 2024-08-21 20:45:01 +03:00
parent 9ecf924a8c
commit a93577a606
2 changed files with 105 additions and 60 deletions

View File

@ -236,7 +236,8 @@ class Board extends FlameGame {
int _removeMatchedElements(int row, int col) { int _removeMatchedElements(int row, int col) {
int score = 0; int score = 0;
final int? value = tiles[row][col]?.spriteIndex; final int? value = tiles[row][col]?.spriteIndex;
bool transformToBomb = false; bool bombTriggered = false;
Tile? tileToTransformIntoBomb = lastMovedTile;
int left = col; int left = col;
while (left > 0 && tiles[row][left - 1]?.spriteIndex == value) left--; while (left > 0 && tiles[row][left - 1]?.spriteIndex == value) left--;
@ -245,24 +246,25 @@ class Board extends FlameGame {
right++; right++;
if (right - left + 1 >= 3) { if (right - left + 1 >= 3) {
int matchLength = right - left + 1; score += _calculateScore(right - left + 1);
score += _calculateScore(matchLength);
if (matchLength == 4 && if (right - left + 1 >= 4) {
lastMovedTile != null && tileToTransformIntoBomb = tiles[row][col];
lastMovedTile!.row == row &&
lastMovedTile!.col == col) {
transformToBomb = true;
} }
for (int i = left; i <= right; i++) { for (int i = left; i <= right; i++) {
if (tiles[row][i] != null && if (tiles[row][i] != null) {
(!transformToBomb || tiles[row][i] != lastMovedTile)) { if (tiles[row][i]!.isBomb) {
bombTriggered = true;
_triggerBomb(row, i);
}
if (tiles[row][i] != tileToTransformIntoBomb) {
_animateRemoveTile(tiles[row][i]!); _animateRemoveTile(tiles[row][i]!);
tiles[row][i] = null; tiles[row][i] = null;
} }
} }
} }
}
int top = row; int top = row;
while (top > 0 && tiles[top - 1][col]?.spriteIndex == value) top--; while (top > 0 && tiles[top - 1][col]?.spriteIndex == value) top--;
@ -271,37 +273,81 @@ class Board extends FlameGame {
bottom++; bottom++;
if (bottom - top + 1 >= 3) { if (bottom - top + 1 >= 3) {
int matchLength = bottom - top + 1; score += _calculateScore(bottom - top + 1);
score += _calculateScore(matchLength);
if (matchLength == 4 && if (bottom - top + 1 >= 4) {
lastMovedTile != null && tileToTransformIntoBomb = tiles[row][col];
lastMovedTile!.row == row &&
lastMovedTile!.col == col) {
transformToBomb = true;
} }
for (int i = top; i <= bottom; i++) { for (int i = top; i <= bottom; i++) {
if (tiles[i][col] != null && if (tiles[i][col] != null) {
(!transformToBomb || tiles[i][col] != lastMovedTile)) { if (tiles[i][col]!.isBomb) {
bombTriggered = true;
_triggerBomb(i, col);
}
if (tiles[i][col] != tileToTransformIntoBomb) {
_animateRemoveTile(tiles[i][col]!); _animateRemoveTile(tiles[i][col]!);
tiles[i][col] = null; tiles[i][col] = null;
} }
} }
} }
if (transformToBomb && lastMovedTile != null) {
_createBomb(lastMovedTile!.row, lastMovedTile!.col);
} else {
if (tiles[row][col] != null) {
_animateRemoveTile(tiles[row][col]!);
tiles[row][col] = null;
} }
if (bombTriggered) {
_triggerBomb(row, col);
}
if (tileToTransformIntoBomb != null) {
_createBomb(tileToTransformIntoBomb.row, tileToTransformIntoBomb.col);
} }
return score; return score;
} }
void _triggerBomb(int row, int col) {
final tile = tiles[row][col];
if (tile == null || !tile.isBomb) return;
final bombPosition = tile.position.clone();
for (int i = max(0, row - 1); i <= min(rows - 1, row + 1); i++) {
for (int j = max(0, col - 1); j <= min(cols - 1, col + 1); j++) {
if (tiles[i][j] != null) {
if (tiles[i][j]!.isBomb && !(i == row && j == col)) {
tiles[i][j]!.isBomb = false;
_triggerBomb(i, j);
} else {
_animateRemoveTile(tiles[i][j]!);
tiles[i][j] = null;
}
}
}
}
_animateBombExplosion(bombPosition);
_animateRemoveTile(tile);
tiles[row][col] = null;
}
void _animateBombExplosion(Vector2 position) {
final explosion = CircleComponent(
radius: tileSize / 2,
paint: Paint()..color = Colors.orange.withOpacity(0.7),
position: position - Vector2.all(tileSize / 2),
);
add(explosion);
explosion.add(
ScaleEffect.to(
Vector2.all(3),
EffectController(duration: 0.5),
onComplete: () => explosion.removeFromParent(),
),
);
}
int _calculateScore(int matchLength) { int _calculateScore(int matchLength) {
if (matchLength == 3) { if (matchLength == 3) {
return 50; return 50;
@ -314,13 +360,15 @@ class Board extends FlameGame {
} }
void _animateRemoveTile(Tile tile) { void _animateRemoveTile(Tile tile) {
tile.animateRemove(() { tile.add(RemoveEffect(
remove(tile); delay: 0.5,
}); onComplete: () => remove(tile),
));
} }
void _createBomb(int row, int col) { void _createBomb(int row, int col) {
final tile = tiles[row][col]!; final tile = tiles[row][col];
if (tile != null) {
tile.isBomb = true; tile.isBomb = true;
tile.add( tile.add(
@ -347,6 +395,7 @@ class Board extends FlameGame {
), ),
); );
} }
}
void _createMagicCube(int row, int col) { void _createMagicCube(int row, int col) {
final tile = tiles[row][col]!; final tile = tiles[row][col]!;

View File

@ -31,11 +31,7 @@ class Tile extends SpriteComponent with TapCallbacks, DragCallbacks {
if (parent is Board && (parent as Board).animating) { if (parent is Board && (parent as Board).animating) {
return false; return false;
} }
if (isBomb) {
(parent as Board).explodeBomb(this);
} else {
onTileTap(this); onTileTap(this);
}
return true; return true;
} }