Files
Barrack-Unity/Assets/Scripts/Game/Balls/OozeBall.cs
T

106 lines
2.6 KiB
C#
Raw Normal View History

2026-08-03 00:45:40 -04:00
using UnityEngine;
2026-08-03 02:37:41 -04:00
using System.Collections;
2026-08-03 00:45:40 -04:00
2026-08-05 01:23:47 -04:00
public class OozeBall : BallBase
2026-08-03 00:45:40 -04:00
{
public AudioClip timerSound;
public AudioClip splitSound;
public AudioClip stoppedSound;
2026-08-03 02:37:41 -04:00
public BallSpawner spawner;
private SpriteRenderer spriteRenderer;
private float splitTimer = 10.0f;
2026-08-03 00:45:40 -04:00
2026-08-03 02:37:41 -04:00
private float tickTimer = 0.5f;
2026-08-03 00:45:40 -04:00
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
audioSource = GetComponent<AudioSource>();
animator = GetComponent<Animator>();
2026-08-03 02:37:41 -04:00
spriteRenderer = GetComponent<SpriteRenderer>();
2026-08-04 00:44:32 -04:00
speed = new Vector2(Random.Range(-0.05f, 0.05f), Random.Range(-0.05f, 0.05f));
2026-08-03 00:45:40 -04:00
audioSource.PlayOneShot(spawnSound);
}
// Update is called once per frame
2026-08-04 00:44:32 -04:00
void FixedUpdate()
2026-08-03 00:45:40 -04:00
{
AnimatorStateInfo stateInfo = animator.GetCurrentAnimatorStateInfo(0);
2026-08-03 02:37:41 -04:00
if (stateInfo.IsName("OozeBallMove") || stateInfo.IsName("OozeBallSplit")) {
2026-08-03 00:45:40 -04:00
transform.Translate(speed);
}
splitTimer -= Time.deltaTime;
tickTimer -= Time.deltaTime;
if (splitTimer <= 0.0f)
{
2026-08-03 02:37:41 -04:00
animator.SetTrigger("split");
2026-08-03 00:45:40 -04:00
}
if (tickTimer <= 0.0f)
{
2026-08-03 02:37:41 -04:00
tickTimer = 0.5f;
2026-08-03 00:45:40 -04:00
audioSource.PlayOneShot(timerSound);
}
// TODO: Implement logic to halt the OozeBall's splitting behavior.
}
void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Wall"))
{
ContactPoint2D contact = collision.GetContact(0);
speed = Vector2.Reflect(speed, contact.normal);
}
}
2026-08-04 02:10:52 -04:00
public void Split()
2026-08-03 00:45:40 -04:00
{
2026-08-03 02:37:41 -04:00
StartCoroutine(SplitCoroutine());
}
private IEnumerator SplitCoroutine()
{
spriteRenderer.enabled = false;
2026-08-03 00:45:40 -04:00
audioSource.PlayOneShot(splitSound);
2026-08-03 02:37:41 -04:00
for (int i = 0; i < Random.Range(5, 8); i++)
{
Vector3 spawnPosition = transform.position +
new Vector3(Random.Range(-0.12f, 0.12f), Random.Range(-0.12f, 0.12f), 0);
spawner.SpawnBallOfType("BasicBall", spawnPosition);
}
spawner.FixCollisions();
yield return new WaitForSeconds(1.0f);
Destroy(gameObject);
2026-08-03 00:45:40 -04:00
}
2026-08-04 02:10:52 -04:00
public void Halt()
2026-08-03 00:45:40 -04:00
{
2026-08-04 02:10:52 -04:00
StartCoroutine(HaltCoroutine());
}
private IEnumerator HaltCoroutine()
{
spriteRenderer.enabled = false;
2026-08-03 00:45:40 -04:00
audioSource.PlayOneShot(stoppedSound);
2026-08-04 02:10:52 -04:00
spawner.SpawnBallOfType("BasicBall", transform.position);
yield return new WaitForSeconds(0.53f);
Destroy(gameObject);
2026-08-03 00:45:40 -04:00
}
}