TO SQUASH: Added shooting bars.
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class BallBase : MonoBehaviour
|
||||
{
|
||||
public AudioClip spawnSound;
|
||||
|
||||
public AudioClip bounceSound;
|
||||
|
||||
public AudioClip hitSound;
|
||||
|
||||
protected Animator animator;
|
||||
|
||||
protected AudioSource audioSource;
|
||||
|
||||
protected Vector2 speed;
|
||||
|
||||
void OnTriggerEnter2D(Collider2D other)
|
||||
{
|
||||
if(other.gameObject.CompareTag("Bar"))
|
||||
{
|
||||
audioSource.PlayOneShot(hitSound);
|
||||
|
||||
// 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d18fc23633602a1af955e537c3ec7f6f
|
||||
@@ -1,19 +1,7 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class BasicBall : MonoBehaviour
|
||||
public class BasicBall : BallBase
|
||||
{
|
||||
private Animator animator;
|
||||
|
||||
public AudioClip spawnSound;
|
||||
|
||||
public AudioClip bounceSound;
|
||||
|
||||
public AudioClip hitSound;
|
||||
|
||||
private AudioSource audioSource;
|
||||
|
||||
private Vector2 speed;
|
||||
|
||||
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
||||
void Start()
|
||||
{
|
||||
|
||||
@@ -1,19 +1,7 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class EyeBall : MonoBehaviour
|
||||
public class EyeBall : BallBase
|
||||
{
|
||||
private Animator animator;
|
||||
|
||||
public AudioClip spawnSound;
|
||||
|
||||
public AudioClip bounceSound;
|
||||
|
||||
public AudioClip hitSound;
|
||||
|
||||
private AudioSource audioSource;
|
||||
|
||||
private Vector2 speed;
|
||||
|
||||
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
||||
void Start()
|
||||
{
|
||||
@@ -29,6 +17,8 @@ public class EyeBall : MonoBehaviour
|
||||
{
|
||||
AnimatorStateInfo stateInfo = animator.GetCurrentAnimatorStateInfo(0);
|
||||
|
||||
// TODO: Adjust eye ball direction based on the player's bar if active.
|
||||
|
||||
if (stateInfo.IsName("EyeBallMove")) {
|
||||
transform.Translate(speed);
|
||||
}
|
||||
|
||||
@@ -1,19 +1,7 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class FruitBall : MonoBehaviour
|
||||
public class FruitBall : BallBase
|
||||
{
|
||||
private Animator animator;
|
||||
|
||||
public AudioClip spawnSound;
|
||||
|
||||
public AudioClip bounceSound;
|
||||
|
||||
public AudioClip hitSound;
|
||||
|
||||
private AudioSource audioSource;
|
||||
|
||||
private Vector2 speed;
|
||||
|
||||
private float speedMultiplier = 1.0f;
|
||||
|
||||
private float speedIncreaseTimer = 0.0f;
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,29 +1,19 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class NukeBall : MonoBehaviour
|
||||
public class NukeBall : BallBase
|
||||
{
|
||||
private Animator animator;
|
||||
|
||||
public AudioClip spawnSound;
|
||||
|
||||
public AudioClip bounceSound;
|
||||
|
||||
public AudioClip hitSound;
|
||||
|
||||
public AudioClip criticalSound;
|
||||
|
||||
public AudioClip explosionSound;
|
||||
|
||||
private AudioSource audioSource;
|
||||
|
||||
private Vector2 speed;
|
||||
|
||||
// Tracks escalating danger/critical phases for audio cues.
|
||||
private int criticalState = 0;
|
||||
|
||||
private float criticalTime = 1.0f;
|
||||
|
||||
private float criticalTimer = 0.0f;
|
||||
|
||||
// Initial resting height the ball bounces back toward after a floor hit.
|
||||
private float height;
|
||||
|
||||
private Collider2D selfCollider;
|
||||
@@ -41,13 +31,17 @@ public class NukeBall : MonoBehaviour
|
||||
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
||||
void Start()
|
||||
{
|
||||
// Cache frequently used components once.
|
||||
audioSource = GetComponent<AudioSource>();
|
||||
animator = GetComponent<Animator>();
|
||||
selfCollider = GetComponent<Collider2D>();
|
||||
|
||||
// Start with a small random horizontal drift.
|
||||
speed = new Vector2(Random.Range(-0.09f, 0.09f), 0.0f);
|
||||
height = GetComponent<Transform>().position.y;
|
||||
fallTargetY = height;
|
||||
|
||||
// Find the closest valid floor point under the current position.
|
||||
UpdateFallTargetFromCurrentPosition();
|
||||
|
||||
audioSource.PlayOneShot(spawnSound);
|
||||
@@ -68,12 +62,14 @@ public class NukeBall : MonoBehaviour
|
||||
bounceTimer += Time.fixedDeltaTime;
|
||||
|
||||
float t = Mathf.Clamp01(bounceTimer / bounceDuration);
|
||||
// Ease-out ascent: fast start, gentle finish near target height.
|
||||
float easedT = 1.0f - Mathf.Pow(1.0f - t, 2.0f);
|
||||
|
||||
currentPosition.y = Mathf.Lerp(bounceStartY, height, easedT);
|
||||
|
||||
if (t >= 1.0f)
|
||||
{
|
||||
// Reached top of bounce arc; resume falling phase.
|
||||
isBouncingToHeight = false;
|
||||
bounceTimer = 0.0f;
|
||||
currentPosition.y = height;
|
||||
@@ -85,6 +81,7 @@ public class NukeBall : MonoBehaviour
|
||||
bounceTimer += Time.fixedDeltaTime;
|
||||
|
||||
float t = Mathf.Clamp01(bounceTimer / bounceDuration);
|
||||
// Ease-in descent: gentle start, faster approach to floor.
|
||||
float easedT = t * t;
|
||||
|
||||
currentPosition.y = Mathf.Lerp(height, fallTargetY, easedT);
|
||||
@@ -110,6 +107,7 @@ public class NukeBall : MonoBehaviour
|
||||
audioSource.PlayOneShot(criticalSound);
|
||||
}
|
||||
|
||||
// Restart timing window for the next critical beep.
|
||||
criticalTimer = 0.0f;
|
||||
}
|
||||
}
|
||||
@@ -120,6 +118,8 @@ public class NukeBall : MonoBehaviour
|
||||
if (collision.gameObject.CompareTag("Wall"))
|
||||
{
|
||||
ContactPoint2D contact = collision.GetContact(0);
|
||||
|
||||
// Reflect horizontal movement from wall surfaces.
|
||||
speed = Vector2.Reflect(speed, contact.normal);
|
||||
speed.y = 0.0f;
|
||||
|
||||
@@ -130,6 +130,8 @@ public class NukeBall : MonoBehaviour
|
||||
{
|
||||
isBouncingToHeight = true;
|
||||
bounceTimer = 0.0f;
|
||||
|
||||
// Use exact contact point as bounce origin so visual contact feels grounded.
|
||||
bounceStartY = contact.point.y;
|
||||
fallTargetY = bounceStartY;
|
||||
|
||||
@@ -142,8 +144,10 @@ public class NukeBall : MonoBehaviour
|
||||
|
||||
private void UpdateFallTargetFromCurrentPosition()
|
||||
{
|
||||
// Default to current y in case no valid floor is found.
|
||||
fallTargetY = transform.position.y;
|
||||
|
||||
// Probe straight down and choose nearest upward-facing wall surface.
|
||||
RaycastHit2D[] hits = Physics2D.RaycastAll(transform.position, Vector2.down, 100.0f);
|
||||
float closestDistance = float.MaxValue;
|
||||
|
||||
|
||||
@@ -1,16 +1,8 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public class OozeBall : MonoBehaviour
|
||||
public class OozeBall : BallBase
|
||||
{
|
||||
private Animator animator;
|
||||
|
||||
public AudioClip spawnSound;
|
||||
|
||||
public AudioClip bounceSound;
|
||||
|
||||
public AudioClip hitSound;
|
||||
|
||||
public AudioClip timerSound;
|
||||
|
||||
public AudioClip splitSound;
|
||||
@@ -19,12 +11,8 @@ public class OozeBall : MonoBehaviour
|
||||
|
||||
public BallSpawner spawner;
|
||||
|
||||
private AudioSource audioSource;
|
||||
|
||||
private SpriteRenderer spriteRenderer;
|
||||
|
||||
private Vector2 speed;
|
||||
|
||||
private float splitTimer = 10.0f;
|
||||
|
||||
private float tickTimer = 0.5f;
|
||||
|
||||
Reference in New Issue
Block a user