TO SQUASH: Started shark.

This commit is contained in:
2026-08-10 22:40:00 -04:00
parent 11ce9bcb47
commit 1b10a2f144
107 changed files with 6662 additions and 309 deletions
+94 -3
View File
@@ -1,16 +1,107 @@
using UnityEngine;
using System.Collections;
public class Shark : MonoBehaviour
{
public AudioClip spawnSound;
public AudioClip angrySound;
public AudioClip tiredSound;
public AudioClip deathSound;
public Player player;
private AudioSource audioSource;
private Animator animator;
private int angle = 0;
private bool isAngry = false;
private bool isTired = false;
private bool isDead = false;
private float angryTimer = 0f;
private float angryDuration = 5f;
private float angryProbability = 0.1f;
private static int sharkKillPoints = 100;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
audioSource = GetComponent<AudioSource>();
animator = GetComponent<Animator>();
bool spawnLeft = Random.Range(0, 2) == 0;
angle = spawnLeft ? 90 : 270;
animator.SetBool("spawnLeft", spawnLeft);
audioSource.PlayOneShot(spawnSound);
}
// Update is called once per frame
void Update()
void FixedUpdate()
{
// Do stuff only if the spawn animation is finished
if (
(animator.GetCurrentAnimatorStateInfo(0).IsName("SharkSpawnRight") ||
animator.GetCurrentAnimatorStateInfo(0).IsName("SharkSpawnLeft")) &&
animator.GetCurrentAnimatorStateInfo(0).normalizedTime >= 1.0f
)
{
// TODO: Check if the shark was killed.
if (isDead)
{
animator.SetTrigger("killed");
audioSource.PlayOneShot(deathSound);
// TODO: award points to the player.
player.score += sharkKillPoints;
StartCoroutine(DestroyShark());
}
else
{
// TODO: Compute movement position and update the shark's speed and angle.
// TODO: Apply 2x movement speed if the shark is angry.
animator.SetInteger("angle", angle);
if (!isAngry && !isTired && Random.value < angryProbability)
{
isAngry = true;
angryTimer = angryDuration;
audioSource.PlayOneShot(angrySound);
}
if (isAngry && !isTired && angryTimer <= 0f)
{
isAngry = false;
isTired = true;
audioSource.PlayOneShot(tiredSound);
}
if (isAngry)
{
angryTimer -= Time.fixedDeltaTime;
}
}
}
}
private IEnumerator DestroyShark()
{
// Wait two loops of the death animation before destroying the shark object.
yield return new WaitUntil(
() => animator.GetCurrentAnimatorStateInfo(0).IsName("SharkDeath") &&
animator.GetCurrentAnimatorStateInfo(0).normalizedTime >= 2.0f
);
Destroy(gameObject);
}
}
-16
View File
@@ -1,16 +0,0 @@
using UnityEngine;
public class SharkSpawner : MonoBehaviour
{
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
+8
View File
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 9b8a711988d7bf908bb986ce809a84bb
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,60 @@
using UnityEngine;
public class SharkSpawner : MonoBehaviour
{
public GameObject sharkPrefab;
public SpriteRenderer gameBoardSpriteRenderer;
private Bounds sharkBounds;
private Bounds gameBoardBounds;
private float spawnTimer = 0f;
private Vector2 spawnPosition;
private bool isActive = true;
private static float spawnProbability = 0.1f;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
gameBoardBounds = gameBoardSpriteRenderer.bounds;
sharkBounds = sharkPrefab.GetComponent<SpriteRenderer>().bounds;
Reset();
}
// Update is called once per frame
void FixedUpdate()
{
spawnTimer += Time.fixedDeltaTime;
if (spawnTimer >= 1f && isActive)
{
spawnTimer = 0f;
if (Random.value < spawnProbability)
{
GameObject shark = Instantiate(sharkPrefab, spawnPosition, Quaternion.identity);
isActive = false;
}
}
}
public void Reset()
{
isActive = true;
spawnTimer = 0f;
spawnPosition = new Vector2(
Random.Range(
gameBoardBounds.min.x + sharkBounds.extents.x,
gameBoardBounds.max.x - sharkBounds.extents.x
),
Random.Range(
gameBoardBounds.min.y + sharkBounds.extents.y,
gameBoardBounds.max.y - sharkBounds.extents.y
)
);
}
}