Adjust Bomb Color

This commit is contained in:
2025-12-16 20:34:13 +08:00
parent b97a9e73d8
commit d10d574cff
5 changed files with 61 additions and 49 deletions

View File

@@ -9,21 +9,40 @@ namespace Views {
public class GemView : MonoBehaviour {
private Gem gem;
public Gem Gem => this.gem;
private SpriteRenderer spriteRenderer;
private bool isFalling;
[Header("Spawn Scale")]
[SerializeField] private float spawnScaleFrom = 0f;
[SerializeField] private float spawnScaleDuration = 0.12f;
private const float SPAWN_SCALE_FROM = 0f;
private const float SPAWN_SCALE_DURATION = 0.12f;
private CancellationTokenSource spawnScaleCts;
public void Bind(Gem gem) {
public void Bind(Gem gem, bool isBomb = false) {
this.gem = gem;
this.gameObject.SetActive(true);
SetupGem(isBomb);
PlaySpawnScale();
}
private void SetupGem(bool isBomb) {
this.spriteRenderer ??= GetComponent<SpriteRenderer>();
if (isBomb) {
this.spriteRenderer.color = this.gem.MatchColor switch {
GemType.Blue => Color.blue,
GemType.Green => Color.green,
GemType.Red => Color.red,
GemType.Yellow => Color.yellow,
GemType.Purple => Color.magenta,
_ => this.spriteRenderer.color
};
} else {
this.spriteRenderer.color = Color.white;
}
}
public void Unbind() {
this.spawnScaleCts?.Cancel();
this.spawnScaleCts?.Dispose();
@@ -39,27 +58,22 @@ namespace Views {
this.spawnScaleCts?.Dispose();
this.spawnScaleCts = new CancellationTokenSource();
this.transform.localScale = Vector3.one * this.spawnScaleFrom;
this.transform.localScale = Vector3.one * SPAWN_SCALE_FROM;
AnimateSpawnScaleAsync(this.spawnScaleCts.Token).Forget();
}
private async UniTask AnimateSpawnScaleAsync(CancellationToken ct) {
if (this.spawnScaleDuration <= 0f) {
this.transform.localScale = Vector3.one;
return;
}
float timer = 0f;
while (timer < this.spawnScaleDuration) {
while (timer < SPAWN_SCALE_DURATION) {
ct.ThrowIfCancellationRequested();
timer += Time.deltaTime;
float scale = Mathf.Clamp01(timer / this.spawnScaleDuration);
float scale = Mathf.Clamp01(timer / SPAWN_SCALE_DURATION);
scale = Mathf.SmoothStep(0f, 1f, scale);
this.transform.localScale = Vector3.LerpUnclamped(
Vector3.one * this.spawnScaleFrom,
Vector3.one * SPAWN_SCALE_FROM,
Vector3.one,
scale
);