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,21 +246,22 @@ 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) {
_animateRemoveTile(tiles[row][i]!); bombTriggered = true;
tiles[row][i] = null; _triggerBomb(row, i);
}
if (tiles[row][i] != tileToTransformIntoBomb) {
_animateRemoveTile(tiles[row][i]!);
tiles[row][i] = null;
}
} }
} }
} }
@ -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) {
_animateRemoveTile(tiles[i][col]!); bombTriggered = true;
tiles[i][col] = null; _triggerBomb(i, col);
}
if (tiles[i][col] != tileToTransformIntoBomb) {
_animateRemoveTile(tiles[i][col]!);
tiles[i][col] = null;
}
} }
} }
} }
if (transformToBomb && lastMovedTile != null) { if (bombTriggered) {
_createBomb(lastMovedTile!.row, lastMovedTile!.col); _triggerBomb(row, col);
} else { }
if (tiles[row][col] != null) {
_animateRemoveTile(tiles[row][col]!); if (tileToTransformIntoBomb != null) {
tiles[row][col] = 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,38 +360,41 @@ 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];
tile.isBomb = true; if (tile != null) {
tile.isBomb = true;
tile.add( tile.add(
OpacityEffect.to( OpacityEffect.to(
0.5, 0.5,
EffectController( EffectController(
duration: 0.5, duration: 0.5,
infinite: true, infinite: true,
reverseDuration: 0.5, reverseDuration: 0.5,
),
), ),
), );
);
tile.add( tile.add(
ColorEffect( ColorEffect(
Colors.orange, Colors.orange,
EffectController( EffectController(
duration: 0.5, duration: 0.5,
infinite: true, infinite: true,
reverseDuration: 0.5, reverseDuration: 0.5,
),
opacityFrom: 0.5,
opacityTo: 1.0,
), ),
opacityFrom: 0.5, );
opacityTo: 1.0, }
),
);
} }
void _createMagicCube(int row, int col) { void _createMagicCube(int row, int 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) { onTileTap(this);
(parent as Board).explodeBomb(this);
} else {
onTileTap(this);
}
return true; return true;
} }