using UnityEngine; using System.Collections; public class GlassBall : BallBase { public AudioClip breakSound; private int health = 3; private bool isBroken = false; private float damageTimer = 0.0f; private float damageCooldown = 0.5f; private SpriteRenderer spriteRenderer; // Start is called once before the first execution of Update after the MonoBehaviour is created void Start() { audioSource = GetComponent(); animator = GetComponent(); spriteRenderer = GetComponent(); speed = new Vector2(Random.Range(-0.05f, 0.05f), Random.Range(-0.05f, 0.05f)); audioSource.PlayOneShot(spawnSound); } // Update is called once per frame void FixedUpdate() { if (damageTimer > 0.0f) { damageTimer -= Time.fixedDeltaTime; } AnimatorStateInfo stateInfo = animator.GetCurrentAnimatorStateInfo(0); if (stateInfo.IsName("GlassBallMove1") || stateInfo.IsName("GlassBallMove2") || stateInfo.IsName("GlassBallMove3")) { transform.Translate(speed); } } void OnCollisionEnter2D(Collision2D collision) { if (collision.gameObject.CompareTag("Wall") || collision.gameObject.CompareTag("GlassBall")) { ContactPoint2D contact = collision.GetContact(0); speed = Vector2.Reflect(speed, contact.normal); // Glass balls take damage when colliding with other GlassBalls if (collision.gameObject.CompareTag("GlassBall")) { Damage(); } } } private void Damage() { if (isBroken || damageTimer > 0.0f) { return; } health--; damageTimer = damageCooldown; if (health < 0) { health = 0; } animator.SetInteger("health", health); if (health > 0) { audioSource.PlayOneShot(hitSound); } else { audioSource.PlayOneShot(breakSound); } } public void Break() { if (isBroken) { return; } isBroken = true; spriteRenderer.enabled = false; var collider = GetComponent(); if (collider != null) { collider.enabled = false; } StartCoroutine(BreakCoroutine()); } private IEnumerator BreakCoroutine() { yield return new WaitForSeconds(0.6f); Destroy(gameObject); } void OnTriggerEnter2D(Collider2D other) { if(other.gameObject.CompareTag("Bar")) { Damage(); // Destroy all bar segments when the ball hits a bar. var barSegments = GameObject.FindGameObjectsWithTag("Bar"); foreach (var barSegment in barSegments) { Destroy(barSegment); } // Notify the player that the bar has been hit. var player = GameObject.FindGameObjectWithTag("Player"); if (player != null) { player.GetComponent().OnBarHit(); } } } }