TO SQUASH: Added player gun and pause.

This commit is contained in:
2026-07-28 00:44:11 -04:00
parent 787d5394c7
commit 7a6c2802f4
47 changed files with 5893 additions and 6 deletions
+15 -1
View File
@@ -14,6 +14,8 @@ public class MusicManager : MonoBehaviour
private Queue<AudioClip> clipsQueue;
private bool isPaused = false;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
@@ -28,7 +30,7 @@ public class MusicManager : MonoBehaviour
// Update is called once per frame
void FixedUpdate()
{
if (audioSource.isPlaying == false)
if (audioSource.isPlaying == false && !isPaused)
{
audioSource.clip = clipsQueue.Dequeue();
audioSource.Play();
@@ -48,4 +50,16 @@ public class MusicManager : MonoBehaviour
clipsQueue.Enqueue(loop);
clipsQueue.Enqueue(outro);
}
public void pauseMusic()
{
audioSource.Pause();
isPaused = true;
}
public void resumeMusic()
{
audioSource.UnPause();
isPaused = false;
}
}
+49
View File
@@ -0,0 +1,49 @@
using UnityEngine;
using UnityEngine.InputSystem;
public class Pause : MonoBehaviour
{
public SpriteRenderer pauseBacgkround;
public SpriteRenderer pauseLabel;
public AudioClip pauseSound;
public MusicManager musicManager;
private bool isPaused = false;
private AudioSource audioSource;
private static Color colorBlackTransparent = new Color(0.0f, 0.0f, 0.0f, 0.0f);
private static Color colorBlackOpaque = new Color(0.0f, 0.0f, 0.0f, 1.0f);
private static Color colorWhiteTransparent = new Color(1.0f, 1.0f, 1.0f, 0.0f);
private static Color colorWhiteOpaque = new Color(1.0f, 1.0f, 1.0f, 1.0f);
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
audioSource = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update()
{
if (Keyboard.current != null && Keyboard.current.capsLockKey.wasPressedThisFrame)
{
isPaused = !isPaused;
pauseBacgkround.color = isPaused ? colorBlackOpaque : colorBlackTransparent;
pauseLabel.color = isPaused ? colorWhiteOpaque : colorWhiteTransparent;
if (isPaused)
{
audioSource.PlayOneShot(pauseSound);
musicManager.pauseMusic();
}
else
{
musicManager.resumeMusic();
}
}
}
}
+2
View File
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 42288f9d549717e7d8e85311a5725dc3
+127
View File
@@ -0,0 +1,127 @@
using UnityEngine;
using UnityEngine.InputSystem;
using System.Collections;
public class Player : MonoBehaviour
{
public GameObject gameBoard;
public AudioClip flip;
public AudioClip normalShoot;
public AudioClip laserCharge;
public AudioClip laserDischarge;
public AudioClip laserShoot;
public AudioClip magnetIdle;
public AudioClip magnetShoot;
public AudioClip death;
public AudioClip killed;
public AudioClip defeat;
public MusicManager musicManager;
private bool flipped = false;
private bool gameOver = false;
private AudioSource audioSource;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
audioSource = GetComponent<AudioSource>();
Cursor.visible = false;
}
// Update is called once per frame
void Update()
{
if (gameBoard == null || Camera.main == null || Mouse.current == null)
{
return;
}
if (!TryGetWorldBounds(gameBoard, out Bounds boardBounds) || !TryGetWorldBounds(gameObject, out Bounds playerBounds))
{
Debug.LogWarning("Could not get world bounds for gameBoard or player.");
return;
}
Vector2 mouseScreen = Mouse.current.position.ReadValue();
Camera cam = Camera.main;
// Keep movement on the same depth plane as the current player position.
float screenZ = cam.WorldToScreenPoint(transform.position).z;
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);
transform.position = new Vector3(clampedX, clampedY, transform.position.z);
if (Keyboard.current != null)
{
if (Keyboard.current.spaceKey.wasPressedThisFrame)
{
flipped = !flipped;
transform.eulerAngles = new Vector3(0, 0, flipped ? 90 : 0);
audioSource.PlayOneShot(flip);
}
if (Keyboard.current.escapeKey.wasPressedThisFrame)
{
gameOver = true;
}
}
if (gameOver)
{
StartCoroutine(EndGame());
gameOver = false; // Reset gameOver to prevent multiple coroutine starts
}
}
private static bool TryGetWorldBounds(GameObject target, out Bounds bounds)
{
Collider2D collider2D = target.GetComponent<Collider2D>();
if (collider2D != null)
{
bounds = collider2D.bounds;
return true;
}
Renderer renderer = target.GetComponent<Renderer>();
if (renderer != null)
{
bounds = renderer.bounds;
return true;
}
Collider collider3D = target.GetComponent<Collider>();
if (collider3D != null)
{
bounds = collider3D.bounds;
return true;
}
bounds = default;
return false;
}
private IEnumerator EndGame()
{
musicManager.pauseMusic();
audioSource.PlayOneShot(defeat);
yield return new WaitForSeconds(1.3f);
UnityEngine.SceneManagement.SceneManager.LoadScene("GameOver");
}
}
+2
View File
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 74c92b00befb9d18ea7fbbbda06eb860