2026-01-22 13:18:27 +00:00
|
|
|
using UnityEngine;
|
|
|
|
|
|
2026-01-26 09:08:38 +00:00
|
|
|
public class DestructibleBlock : Entity
|
2026-01-22 13:18:27 +00:00
|
|
|
{
|
2026-01-27 12:06:26 +00:00
|
|
|
[SerializeField] private GameObject[] debrisPrefabs;
|
|
|
|
|
[SerializeField] private int debrisCount = 5;
|
|
|
|
|
[SerializeField] private float explosionForce = 5f;
|
|
|
|
|
[SerializeField] private float debrisLifetime = 2f;
|
2026-01-22 13:18:27 +00:00
|
|
|
|
2026-01-27 12:06:26 +00:00
|
|
|
protected override void Die()
|
2026-01-22 13:18:27 +00:00
|
|
|
{
|
2026-01-27 12:06:26 +00:00
|
|
|
SpawnDebris();
|
|
|
|
|
base.Die();
|
2026-01-26 09:08:38 +00:00
|
|
|
}
|
2026-01-22 13:18:27 +00:00
|
|
|
|
2026-01-27 12:06:26 +00:00
|
|
|
private void OnCollisionEnter2D(Collision2D collision)
|
2026-01-26 09:08:38 +00:00
|
|
|
{
|
2026-01-27 12:06:26 +00:00
|
|
|
if (collision.gameObject.CompareTag("Player"))
|
|
|
|
|
{
|
|
|
|
|
Player player = collision.gameObject.GetComponent<Player>();
|
|
|
|
|
if (player != null && player.IsDashing())
|
|
|
|
|
{
|
|
|
|
|
Die();
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-01-22 13:18:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SpawnDebris()
|
|
|
|
|
{
|
2026-01-27 12:06:26 +00:00
|
|
|
if (debrisPrefabs == null || debrisPrefabs.Length == 0)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-22 13:18:27 +00:00
|
|
|
for (int i = 0; i < debrisCount; i++)
|
|
|
|
|
{
|
2026-01-27 12:06:26 +00:00
|
|
|
Vector2 spawnPos = (Vector2)transform.position + Random.insideUnitCircle * 0.5f;
|
2026-01-22 13:18:27 +00:00
|
|
|
GameObject prefab = debrisPrefabs[Random.Range(0, debrisPrefabs.Length)];
|
2026-01-27 12:06:26 +00:00
|
|
|
GameObject debris = Instantiate(prefab, spawnPos, Quaternion.identity);
|
2026-01-22 13:18:27 +00:00
|
|
|
Rigidbody2D rb = debris.GetComponent<Rigidbody2D>();
|
|
|
|
|
if (rb != null)
|
|
|
|
|
{
|
2026-01-27 12:06:26 +00:00
|
|
|
Vector2 dir = (spawnPos - (Vector2)transform.position).normalized;
|
|
|
|
|
dir += Random.insideUnitCircle * 0.5f;
|
2026-01-22 13:18:27 +00:00
|
|
|
rb.AddForce(dir.normalized * explosionForce, ForceMode2D.Impulse);
|
2026-01-27 12:06:26 +00:00
|
|
|
rb.AddTorque(Random.Range(-180f, 180f));
|
2026-01-22 13:18:27 +00:00
|
|
|
}
|
|
|
|
|
|
2026-01-27 12:06:26 +00:00
|
|
|
Destroy(debris, debrisLifetime);
|
2026-01-22 13:18:27 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|