TO SQUASH: Fixed issue with yummy missed sound playing multiple times.
This commit is contained in:
@@ -13,6 +13,10 @@ public class Shark : MonoBehaviour
|
||||
|
||||
public Player player;
|
||||
|
||||
public SpriteRenderer gameBoardSpriteRenderer;
|
||||
|
||||
private Bounds gameBoardBounds;
|
||||
|
||||
private AudioSource audioSource;
|
||||
|
||||
private Animator animator;
|
||||
@@ -33,12 +37,14 @@ public class Shark : MonoBehaviour
|
||||
|
||||
private static int sharkKillPoints = 100;
|
||||
|
||||
private bool spawnFinished = false;
|
||||
|
||||
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
||||
void Start()
|
||||
{
|
||||
audioSource = GetComponent<AudioSource>();
|
||||
animator = GetComponent<Animator>();
|
||||
|
||||
gameBoardBounds = gameBoardSpriteRenderer.bounds;
|
||||
bool spawnLeft = Random.Range(0, 2) == 0;
|
||||
angle = spawnLeft ? 90 : 270;
|
||||
|
||||
@@ -49,12 +55,18 @@ public class Shark : MonoBehaviour
|
||||
// Update is called once per frame
|
||||
void FixedUpdate()
|
||||
{
|
||||
// Do stuff only if the spawn animation is finished
|
||||
// Check if the spawn animation has finished.
|
||||
if (
|
||||
(animator.GetCurrentAnimatorStateInfo(0).IsName("SharkSpawnRight") ||
|
||||
animator.GetCurrentAnimatorStateInfo(0).IsName("SharkSpawnLeft")) &&
|
||||
animator.GetCurrentAnimatorStateInfo(0).normalizedTime >= 1.0f
|
||||
)
|
||||
{
|
||||
spawnFinished = true;
|
||||
}
|
||||
|
||||
// Do stuff only if the spawn animation has finished.
|
||||
if (spawnFinished)
|
||||
{
|
||||
// TODO: Check if the shark was killed.
|
||||
|
||||
|
||||
@@ -6,8 +6,6 @@ public class SharkSpawner : MonoBehaviour
|
||||
|
||||
public SpriteRenderer gameBoardSpriteRenderer;
|
||||
|
||||
private Bounds sharkBounds;
|
||||
|
||||
private Bounds gameBoardBounds;
|
||||
|
||||
private float spawnTimer = 0f;
|
||||
@@ -22,7 +20,6 @@ public class SharkSpawner : MonoBehaviour
|
||||
void Start()
|
||||
{
|
||||
gameBoardBounds = gameBoardSpriteRenderer.bounds;
|
||||
sharkBounds = sharkPrefab.GetComponent<SpriteRenderer>().bounds;
|
||||
Reset();
|
||||
}
|
||||
|
||||
@@ -37,6 +34,9 @@ public class SharkSpawner : MonoBehaviour
|
||||
if (Random.value < spawnProbability)
|
||||
{
|
||||
GameObject shark = Instantiate(sharkPrefab, spawnPosition, Quaternion.identity);
|
||||
Shark sharkScript = shark.GetComponent<Shark>();
|
||||
sharkScript.gameBoardSpriteRenderer = gameBoardSpriteRenderer;
|
||||
sharkScript.player = FindAnyObjectByType<Player>();
|
||||
isActive = false;
|
||||
}
|
||||
}
|
||||
@@ -46,14 +46,19 @@ public class SharkSpawner : MonoBehaviour
|
||||
{
|
||||
isActive = true;
|
||||
spawnTimer = 0f;
|
||||
|
||||
Vector2 boardCenter = gameBoardBounds.center;
|
||||
Vector2 spawnAreaSize = gameBoardBounds.size * (2f / 3f);
|
||||
Vector2 halfSpawnArea = spawnAreaSize * 0.5f;
|
||||
|
||||
spawnPosition = new Vector2(
|
||||
Random.Range(
|
||||
gameBoardBounds.min.x + sharkBounds.extents.x,
|
||||
gameBoardBounds.max.x - sharkBounds.extents.x
|
||||
boardCenter.x - halfSpawnArea.x,
|
||||
boardCenter.x + halfSpawnArea.x
|
||||
),
|
||||
Random.Range(
|
||||
gameBoardBounds.min.y + sharkBounds.extents.y,
|
||||
gameBoardBounds.max.y - sharkBounds.extents.y
|
||||
boardCenter.y - halfSpawnArea.y,
|
||||
boardCenter.y + halfSpawnArea.y
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public abstract class YummyBase : MonoBehaviour
|
||||
{
|
||||
@@ -20,6 +21,8 @@ public abstract class YummyBase : MonoBehaviour
|
||||
|
||||
private float missedTimer = 0f;
|
||||
|
||||
private bool isMissed = false;
|
||||
|
||||
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
||||
void Start()
|
||||
{
|
||||
@@ -32,7 +35,7 @@ public abstract class YummyBase : MonoBehaviour
|
||||
// Update is called once per frame
|
||||
void FixedUpdate()
|
||||
{
|
||||
if (isObtained)
|
||||
if (isObtained || isMissed)
|
||||
{
|
||||
return;
|
||||
}
|
||||
@@ -41,10 +44,12 @@ public abstract class YummyBase : MonoBehaviour
|
||||
|
||||
missedTimer += Time.fixedDeltaTime;
|
||||
|
||||
if (missedTimer >= 5f)
|
||||
if (missedTimer >= 5f && !isMissed)
|
||||
{
|
||||
isMissed = true;
|
||||
audioSource.PlayOneShot(missedSound);
|
||||
animator.SetTrigger("missed");
|
||||
StartCoroutine(DestroyYummy());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -85,8 +90,9 @@ public abstract class YummyBase : MonoBehaviour
|
||||
}
|
||||
}
|
||||
|
||||
public void DestroyYummy()
|
||||
public IEnumerator DestroyYummy()
|
||||
{
|
||||
yield return new WaitForSeconds(1.5f);
|
||||
Destroy(gameObject);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user