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-26 09:08:38 +00:00
|
|
|
[SerializeField] private GameObject[] debrisPrefabs;
|
|
|
|
|
[SerializeField] private int debrisCount = 5;
|
|
|
|
|
[SerializeField] private float explosionForce = 5f;
|
2026-01-22 13:18:27 +00:00
|
|
|
|
2026-01-26 09:08:38 +00:00
|
|
|
public override void TakeDamage(int damage)
|
2026-01-22 13:18:27 +00:00
|
|
|
{
|
2026-01-26 09:08:38 +00:00
|
|
|
base.TakeDamage(damage);
|
|
|
|
|
}
|
2026-01-22 13:18:27 +00:00
|
|
|
|
2026-01-26 09:08:38 +00:00
|
|
|
private void OnDestroy()
|
|
|
|
|
{
|
2026-01-22 13:18:27 +00:00
|
|
|
SpawnDebris();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SpawnDebris()
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < debrisCount; i++)
|
|
|
|
|
{
|
|
|
|
|
GameObject prefab = debrisPrefabs[Random.Range(0, debrisPrefabs.Length)];
|
|
|
|
|
Vector2 randomPos = (Vector2)transform.position + Random.insideUnitCircle * 0.5f;
|
|
|
|
|
GameObject debris = Instantiate(prefab, randomPos, Quaternion.identity);
|
|
|
|
|
Rigidbody2D rb = debris.GetComponent<Rigidbody2D>();
|
2026-01-26 09:08:38 +00:00
|
|
|
|
2026-01-22 13:18:27 +00:00
|
|
|
if (rb != null)
|
|
|
|
|
{
|
|
|
|
|
Vector2 dir = (randomPos - (Vector2)transform.position).normalized;
|
2026-01-26 09:08:38 +00:00
|
|
|
dir += Random.insideUnitCircle * 0.5f;
|
2026-01-22 13:18:27 +00:00
|
|
|
rb.AddForce(dir.normalized * explosionForce, ForceMode2D.Impulse);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Destroy(debris, 2f);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|