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;
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class HorizontalBar : MonoBehaviour
|
||||
{
|
||||
public float firingSpeed = 0.03f;
|
||||
|
||||
public GameObject horizontalBarrierPrefab;
|
||||
|
||||
public bool canFireLeft = true;
|
||||
|
||||
public bool canFireRight = true;
|
||||
|
||||
private float firingTimer = 0.0f;
|
||||
|
||||
private Bounds bounds;
|
||||
|
||||
private bool canFire = true;
|
||||
|
||||
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
||||
void Start()
|
||||
{
|
||||
SpriteRenderer spriteRenderer = GetComponent<SpriteRenderer>();
|
||||
bounds = spriteRenderer.bounds;
|
||||
|
||||
var walls = GameObject.FindGameObjectsWithTag("Wall");
|
||||
|
||||
if (walls.Length > 0)
|
||||
{
|
||||
foreach (var wall in walls)
|
||||
{
|
||||
if (wall.GetComponent<Collider2D>().bounds.Intersects(bounds))
|
||||
{
|
||||
canFire = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void FixedUpdate()
|
||||
{
|
||||
if (canFire)
|
||||
{
|
||||
firingTimer += Time.fixedDeltaTime;
|
||||
|
||||
if (firingTimer >= firingSpeed)
|
||||
{
|
||||
if (canFireLeft)
|
||||
{
|
||||
Vector3 leftSide = new Vector3(
|
||||
bounds.min.x - (bounds.size.x / 2.0f),
|
||||
bounds.center.y,
|
||||
bounds.center.z
|
||||
);
|
||||
|
||||
var left = Instantiate(horizontalBarrierPrefab, leftSide, Quaternion.identity);
|
||||
var leftBarScript = left.GetComponent<HorizontalBar>();
|
||||
leftBarScript.firingSpeed = firingSpeed;
|
||||
leftBarScript.canFireRight = false;
|
||||
|
||||
Physics2D.IgnoreCollision(
|
||||
left.GetComponent<Collider2D>(),
|
||||
GetComponent<Collider2D>(),
|
||||
true
|
||||
);
|
||||
}
|
||||
|
||||
if (canFireRight)
|
||||
{
|
||||
Vector3 rightSide = new Vector3(
|
||||
bounds.max.x + (bounds.size.x / 2.0f),
|
||||
bounds.center.y,
|
||||
bounds.center.z
|
||||
);
|
||||
var right = Instantiate(horizontalBarrierPrefab, rightSide, Quaternion.identity);
|
||||
var rightBarScript = right.GetComponent<HorizontalBar>();
|
||||
rightBarScript.firingSpeed = firingSpeed;
|
||||
rightBarScript.canFireLeft = false;
|
||||
|
||||
Physics2D.IgnoreCollision(
|
||||
right.GetComponent<Collider2D>(),
|
||||
GetComponent<Collider2D>(),
|
||||
true
|
||||
);
|
||||
}
|
||||
|
||||
canFire = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3f0f68050bf7251f7b0cac4505293fdc
|
||||
@@ -30,12 +30,18 @@ public class Player : MonoBehaviour
|
||||
|
||||
public Fader fader;
|
||||
|
||||
public GameObject horizontalBarrierPrefab;
|
||||
|
||||
public GameObject verticalBarrierPrefab;
|
||||
|
||||
private bool flipped = false;
|
||||
|
||||
private bool gameOver = false;
|
||||
|
||||
private AudioSource audioSource;
|
||||
|
||||
private bool canFire = true;
|
||||
|
||||
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
||||
void Start()
|
||||
{
|
||||
@@ -65,8 +71,8 @@ public class Player : MonoBehaviour
|
||||
Vector3 mouseWorld = cam.ScreenToWorldPoint(new Vector3(mouseScreen.x, mouseScreen.y, screenZ));
|
||||
|
||||
Vector3 extents = playerBounds.extents;
|
||||
float clampedX = Mathf.Clamp(mouseWorld.x, boardBounds.min.x + extents.x, boardBounds.max.x - extents.x);
|
||||
float clampedY = Mathf.Clamp(mouseWorld.y, boardBounds.min.y + extents.y, boardBounds.max.y - extents.y);
|
||||
float clampedX = Mathf.Clamp(mouseWorld.x, boardBounds.min.x + extents.x / 2.0f, boardBounds.max.x - extents.x / 2.0f);
|
||||
float clampedY = Mathf.Clamp(mouseWorld.y, boardBounds.min.y + extents.y / 2.0f, boardBounds.max.y - extents.y / 2.0f);
|
||||
|
||||
transform.position = new Vector3(clampedX, clampedY, transform.position.z);
|
||||
|
||||
@@ -85,6 +91,21 @@ public class Player : MonoBehaviour
|
||||
}
|
||||
}
|
||||
|
||||
if (Mouse.current.leftButton.wasPressedThisFrame && canFire)
|
||||
{
|
||||
canFire = false;
|
||||
audioSource.PlayOneShot(normalShoot);
|
||||
|
||||
if (flipped)
|
||||
{
|
||||
Instantiate(verticalBarrierPrefab, transform.position, Quaternion.identity);
|
||||
}
|
||||
else
|
||||
{
|
||||
Instantiate(horizontalBarrierPrefab, transform.position, Quaternion.identity);
|
||||
}
|
||||
}
|
||||
|
||||
if (gameOver)
|
||||
{
|
||||
StartCoroutine(EndGame());
|
||||
@@ -126,4 +147,15 @@ public class Player : MonoBehaviour
|
||||
yield return new WaitForSeconds(1.3f);
|
||||
fader.ResetFader();
|
||||
}
|
||||
|
||||
public void BarrierFinished()
|
||||
{
|
||||
canFire = true;
|
||||
}
|
||||
|
||||
public void OnBarHit()
|
||||
{
|
||||
// TODO: Lose a life or end the game
|
||||
canFire = true; // Allow firing again after bar hit
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class VerticalBar : MonoBehaviour
|
||||
{
|
||||
public float firingSpeed = 0.03f;
|
||||
|
||||
public GameObject verticalBarrierPrefab;
|
||||
|
||||
public bool canFireTop = true;
|
||||
|
||||
public bool canFireBottom = true;
|
||||
|
||||
private float firingTimer = 0.0f;
|
||||
|
||||
private Bounds bounds;
|
||||
|
||||
private bool canFire = true;
|
||||
|
||||
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
||||
void Start()
|
||||
{
|
||||
SpriteRenderer spriteRenderer = GetComponent<SpriteRenderer>();
|
||||
bounds = spriteRenderer.bounds;
|
||||
|
||||
var walls = GameObject.FindGameObjectsWithTag("Wall");
|
||||
|
||||
if (walls.Length > 0)
|
||||
{
|
||||
foreach (var wall in walls)
|
||||
{
|
||||
if (wall.GetComponent<Collider2D>().bounds.Intersects(bounds))
|
||||
{
|
||||
canFire = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void FixedUpdate()
|
||||
{
|
||||
if (canFire)
|
||||
{
|
||||
firingTimer += Time.fixedDeltaTime;
|
||||
|
||||
if (firingTimer >= firingSpeed)
|
||||
{
|
||||
if (canFireTop)
|
||||
{
|
||||
Vector3 topSide = new Vector3(
|
||||
bounds.center.x,
|
||||
bounds.max.y + (bounds.size.y / 2.0f),
|
||||
bounds.center.z
|
||||
);
|
||||
|
||||
var top = Instantiate(verticalBarrierPrefab, topSide, Quaternion.identity);
|
||||
var topBarScript = top.GetComponent<VerticalBar>();
|
||||
topBarScript.firingSpeed = firingSpeed;
|
||||
topBarScript.canFireBottom = false;
|
||||
|
||||
Physics2D.IgnoreCollision(
|
||||
top.GetComponent<Collider2D>(),
|
||||
GetComponent<Collider2D>(),
|
||||
true
|
||||
);
|
||||
}
|
||||
|
||||
if (canFireBottom)
|
||||
{
|
||||
Vector3 bottomSide = new Vector3(
|
||||
bounds.center.x,
|
||||
bounds.min.y - (bounds.size.y / 2.0f),
|
||||
bounds.center.z
|
||||
);
|
||||
var bottom = Instantiate(verticalBarrierPrefab, bottomSide, Quaternion.identity);
|
||||
var bottomBarScript = bottom.GetComponent<VerticalBar>();
|
||||
bottomBarScript.firingSpeed = firingSpeed;
|
||||
bottomBarScript.canFireTop = false;
|
||||
|
||||
Physics2D.IgnoreCollision(
|
||||
bottom.GetComponent<Collider2D>(),
|
||||
GetComponent<Collider2D>(),
|
||||
true
|
||||
);
|
||||
}
|
||||
|
||||
canFire = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6dc29ecf320c579669972d171a926ff4
|
||||
Reference in New Issue
Block a user