TO SQUASH: Added shooting bars.
This commit is contained in:
@@ -1,24 +1,18 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public class GlassBall : MonoBehaviour
|
||||
public class GlassBall : BallBase
|
||||
{
|
||||
private Animator animator;
|
||||
|
||||
public AudioClip spawnSound;
|
||||
|
||||
public AudioClip bounceSound;
|
||||
|
||||
public AudioClip hitSound;
|
||||
|
||||
public AudioClip breakSound;
|
||||
|
||||
private AudioSource audioSource;
|
||||
|
||||
private Vector2 speed;
|
||||
|
||||
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
|
||||
@@ -35,6 +29,11 @@ public class GlassBall : MonoBehaviour
|
||||
// Update is called once per frame
|
||||
void FixedUpdate()
|
||||
{
|
||||
if (damageTimer > 0.0f)
|
||||
{
|
||||
damageTimer -= Time.fixedDeltaTime;
|
||||
}
|
||||
|
||||
AnimatorStateInfo stateInfo = animator.GetCurrentAnimatorStateInfo(0);
|
||||
|
||||
if (stateInfo.IsName("GlassBallMove1") ||
|
||||
@@ -54,32 +53,56 @@ public class GlassBall : MonoBehaviour
|
||||
speed = Vector2.Reflect(speed, contact.normal);
|
||||
|
||||
// Glass balls take damage when colliding with other GlassBalls
|
||||
// TODO: Check for collisions with a bar.
|
||||
if (collision.gameObject.CompareTag("GlassBall"))
|
||||
{
|
||||
health--;
|
||||
|
||||
if (health < 0)
|
||||
{
|
||||
health = 0;
|
||||
}
|
||||
|
||||
animator.SetInteger("health", health);
|
||||
|
||||
if (health > 0)
|
||||
{
|
||||
audioSource.PlayOneShot(hitSound);
|
||||
} else
|
||||
{
|
||||
audioSource.PlayOneShot(breakSound);
|
||||
}
|
||||
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<Collider2D>();
|
||||
|
||||
if (collider != null)
|
||||
{
|
||||
collider.enabled = false;
|
||||
}
|
||||
|
||||
StartCoroutine(BreakCoroutine());
|
||||
}
|
||||
|
||||
@@ -89,4 +112,28 @@ public class GlassBall : MonoBehaviour
|
||||
|
||||
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<Player>().OnBarHit();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user