Adjust Bomb Behavior

This commit is contained in:
2025-12-17 00:11:17 +08:00
parent 4c3980d84d
commit de2de35b1b
5 changed files with 84 additions and 47 deletions

View File

@@ -53,7 +53,7 @@ MonoBehaviour:
scoreValue: 10
width: 7
height: 7
bombDelay: 1
bombDelay: 2
bombSelfDelay: 1
bombRadius: 2
dropHeight: 2

View File

@@ -18,8 +18,6 @@ namespace ScriptableObjects {
[Header("Bomb")]
[Tooltip("How long before the gems around the bomb explode")]
public float bombDelay = 0.1f;
[Tooltip("How long before the bomb itself explodes")]
public float bombSelfDelay = 0.05f;
[Tooltip("How far the explosion reaches")]
public int bombRadius = 1;

View File

@@ -42,64 +42,95 @@ namespace Services
Func<Vector2Int, Gem> getGemAt,
Func<Vector2Int, UniTask> destroyAtAsync,
int radius,
float bombDelaySeconds,
float bombSelfDelaySeconds)
float bombDelaySeconds)
{
if (initialBombs == null || initialBombs.Count == 0)
return;
Queue<Vector2Int> queue = new Queue<Vector2Int>(initialBombs);
HashSet<Vector2Int> processed = new HashSet<Vector2Int>();
int waveDelayMs = Mathf.Max(0, Mathf.RoundToInt(bombDelaySeconds * 1000f));
while (queue.Count > 0)
HashSet<Vector2Int> processedBombs = new HashSet<Vector2Int>();
Queue<Vector2Int> waveQueue = new Queue<Vector2Int>(
initialBombs.Where(p =>
{
Vector2Int bombPos = queue.Dequeue();
if (processed.Contains(bombPos))
continue;
if (!inBounds(p)) return false;
Gem g = getGemAt(p);
return g is { Type: GemType.Bomb };
})
);
if (!inBounds(bombPos))
continue;
Gem bomb = getGemAt(bombPos);
if (bomb is not { Type: GemType.Bomb })
continue;
processed.Add(bombPos);
int ringDelayMs = Mathf.RoundToInt(bombDelaySeconds * 1000f);
for (int dist = 1; dist <= radius; dist++)
while (waveQueue.Count > 0)
{
if (ringDelayMs > 0)
await UniTask.Delay(ringDelayMs);
foreach (Vector2Int n in DiamondRing(bombPos, dist))
// current wave (per bomb)
List<Vector2Int> waveBombs = new List<Vector2Int>();
while (waveQueue.Count > 0)
{
if (!inBounds(n))
Vector2Int b = waveQueue.Dequeue();
if (processedBombs.Contains(b))
continue;
Gem g = getGemAt(n);
if (g == null)
if (!inBounds(b))
continue;
if (g.Type == GemType.Bomb)
Gem g = getGemAt(b);
if (g is not { Type: GemType.Bomb })
continue;
processedBombs.Add(b);
waveBombs.Add(b);
}
if (waveBombs.Count == 0)
continue;
// delay once per wave
if (waveDelayMs > 0)
await UniTask.Delay(waveDelayMs);
HashSet<Vector2Int> nextWaveBombs = new HashSet<Vector2Int>();
HashSet<Vector2Int> toDestroyNow = new HashSet<Vector2Int>();
for (int i = 0; i < waveBombs.Count; i++)
{
if (!processed.Contains(n))
queue.Enqueue(n);
Vector2Int bombPos = waveBombs[i];
// destroy self when it detonates
toDestroyNow.Add(bombPos);
foreach (Vector2Int p in DiamondAreaInclusive(bombPos, radius))
{
if (!inBounds(p))
continue;
if (p == bombPos)
continue;
Gem cellGem = getGemAt(p);
if (cellGem == null)
continue;
if (cellGem.Type == GemType.Bomb)
{
// bombs in range are NOT destroyed now. triggered to explode in a later wave.
if (!processedBombs.Contains(p))
nextWaveBombs.Add(p);
continue;
}
await destroyAtAsync(n);
// Non-bomb gem gets destroyed by this bomb
toDestroyNow.Add(p);
}
}
int selfDelayMs = Mathf.Max(0, Mathf.RoundToInt(bombSelfDelaySeconds * 1000f));
if (selfDelayMs > 0)
await UniTask.Delay(selfDelayMs);
// Destroy everything for this wave (non-bombs in range + the detonating bombs themselves)
foreach (Vector2Int p in toDestroyNow)
await destroyAtAsync(p);
Gem stillBomb = getGemAt(bombPos);
if (stillBomb is { Type: GemType.Bomb })
await destroyAtAsync(bombPos);
// Schedule the next wave (triggered bombs)
foreach (Vector2Int b in nextWaveBombs)
waveQueue.Enqueue(b);
}
}
@@ -119,5 +150,16 @@ namespace Services
}
}
}
private static IEnumerable<Vector2Int> DiamondAreaInclusive(Vector2Int center, int radius)
{
yield return center;
for (int dist = 1; dist <= radius; dist++)
{
foreach (Vector2Int pivot in DiamondRing(center, dist))
yield return pivot;
}
}
}
}

View File

@@ -217,8 +217,7 @@ namespace Services {
GetGem,
DestroyAtAsync,
this.gameVariables.bombRadius,
this.gameVariables.bombDelay,
this.gameVariables.bombSelfDelay);
this.gameVariables.bombDelay);
await MoveGemsDown();
return;
@@ -239,8 +238,7 @@ namespace Services {
GetGem,
DestroyAtAsync,
this.gameVariables.bombRadius,
this.gameVariables.bombDelay,
this.gameVariables.bombSelfDelay);
this.gameVariables.bombDelay);
await MoveGemsDown();
}

View File

@@ -15,7 +15,6 @@ namespace Services.Interfaces
Func<Vector2Int, Gem> getGemAt,
Func<Vector2Int, UniTask> destroyAtAsync,
int radius,
float bombDelaySeconds,
float bombSelfDelaySeconds);
float bombDelaySeconds);
}
}