TO SQUASH: Fixed collision issues with the shark.
This commit is contained in:
@@ -98,6 +98,8 @@ public class Player : MonoBehaviour
|
||||
|
||||
private float magnetTimer = 0.0f;
|
||||
|
||||
private bool killedByShark = false;
|
||||
|
||||
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
||||
void Start()
|
||||
{
|
||||
@@ -148,8 +150,11 @@ public class Player : MonoBehaviour
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
void FixedUpdate()
|
||||
{
|
||||
// Foce reset the rotation.
|
||||
transform.rotation = Quaternion.identity;
|
||||
|
||||
if (damageTimer > 0.0f)
|
||||
{
|
||||
damageTimer -= Time.fixedDeltaTime;
|
||||
@@ -233,6 +238,12 @@ public class Player : MonoBehaviour
|
||||
gameOver = false; // Reset gameOver to prevent multiple coroutine starts
|
||||
}
|
||||
|
||||
if (killedByShark)
|
||||
{
|
||||
killedByShark = false; // Reset the flag to prevent multiple sound plays.
|
||||
StartCoroutine(Revive());
|
||||
}
|
||||
|
||||
if (magnetTimer > 0.0f)
|
||||
{
|
||||
magnetTimer -= Time.deltaTime;
|
||||
@@ -275,6 +286,29 @@ public class Player : MonoBehaviour
|
||||
return false;
|
||||
}
|
||||
|
||||
private IEnumerator Revive()
|
||||
{
|
||||
yield return new WaitForSeconds(1.0f);
|
||||
if (lives > 0)
|
||||
{
|
||||
audioSource.PlayOneShot(killed);
|
||||
yield return new WaitForSeconds(1.0f);
|
||||
canFire = true;
|
||||
exploding = false;
|
||||
gunAnimator.SetTrigger("revived");
|
||||
|
||||
SpriteRenderer[] renderers = GetComponentsInChildren<SpriteRenderer>();
|
||||
foreach (SpriteRenderer renderer in renderers)
|
||||
{
|
||||
renderer.enabled = true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
gameOver = true;
|
||||
}
|
||||
}
|
||||
|
||||
private IEnumerator EndGame()
|
||||
{
|
||||
musicManager.pauseMusic();
|
||||
@@ -455,4 +489,23 @@ public class Player : MonoBehaviour
|
||||
|
||||
canFire = true; // Allow firing again after bar hit
|
||||
}
|
||||
|
||||
void OnCollisionEnter2D(Collision2D collision)
|
||||
{
|
||||
if (collision.gameObject.CompareTag("Shark"))
|
||||
{
|
||||
if (exploding)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
killedByShark = true;
|
||||
lives--;
|
||||
Explode();
|
||||
}
|
||||
else
|
||||
{
|
||||
Physics2D.IgnoreCollision(collision.collider, GetComponent<Collider2D>(), true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,13 +17,6 @@ public class PlayerGun : MonoBehaviour
|
||||
|
||||
public void AnimationFinished()
|
||||
{
|
||||
if (player.lives > 0)
|
||||
{
|
||||
animator.SetTrigger("revived");
|
||||
}
|
||||
else
|
||||
{
|
||||
spriteRenderer.enabled = false;
|
||||
}
|
||||
spriteRenderer.enabled = false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,6 +11,8 @@ public class Shark : MonoBehaviour
|
||||
|
||||
public AudioClip deathSound;
|
||||
|
||||
public AudioClip hitSound;
|
||||
|
||||
public Player player;
|
||||
|
||||
public SpriteRenderer gameBoardSpriteRenderer;
|
||||
@@ -113,10 +115,10 @@ public class Shark : MonoBehaviour
|
||||
Bounds sharkBounds = sharkSpriteRenderer.bounds;
|
||||
Bounds boardBounds = gameBoardBounds.Value;
|
||||
|
||||
float minX = boardBounds.min.x + sharkBounds.size.x;
|
||||
float maxX = boardBounds.max.x - sharkBounds.size.x;
|
||||
float minY = boardBounds.min.y + sharkBounds.size.y;
|
||||
float maxY = boardBounds.max.y - sharkBounds.size.y;
|
||||
float minX = boardBounds.min.x + (2.0f * sharkBounds.size.x);
|
||||
float maxX = boardBounds.max.x - (2.0f * sharkBounds.size.x);
|
||||
float minY = boardBounds.min.y + (2.0f * sharkBounds.size.y);
|
||||
float maxY = boardBounds.max.y - (2.0f * sharkBounds.size.y);
|
||||
|
||||
// Fallback to board center if the shrunken region collapses.
|
||||
if (minX > maxX)
|
||||
@@ -286,4 +288,30 @@ public class Shark : MonoBehaviour
|
||||
);
|
||||
Destroy(gameObject);
|
||||
}
|
||||
void OnTriggerEnter2D(Collider2D other)
|
||||
{
|
||||
if(other.gameObject.CompareTag("Bar"))
|
||||
{
|
||||
if (!audioSource.isPlaying)
|
||||
{
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user