| 12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- using UnityEngine;
- [RequireComponent(typeof(Collider2D))]
- public class blockScript : MonoBehaviour
- {
- [SerializeField] private float health = 1f;
- private float _currentHealth;
- private bool _isDestroyed;
- void Awake()
- {
- _currentHealth = health;
- }
- private void OnCollisionEnter2D(Collision2D collision)
- {
- if (!collision.gameObject.CompareTag("Ball"))
- return;
- BallScript ball = collision.gameObject.GetComponent<BallScript>();
- float damage = ball != null ? ball.GetDamageAgainstBlock(this, collision) : 1f;
- ApplyDamage(damage);
- ball?.NotifyHitBlock(this, collision);
- }
- public void ApplyDamage(float amount)
- {
- if (_isDestroyed)
- return;
- float clampedAmount = Mathf.Max(0f, amount);
- _currentHealth -= clampedAmount;
- if (_currentHealth <= 0f)
- {
- _isDestroyed = true;
- Destroy(gameObject);
- GameManager.Instance?.OnBlockDestroyed();
- }
- }
- }
|