TO SQUASH: Started map covering.

This commit is contained in:
2026-08-12 00:52:57 -04:00
parent 59e6e73b20
commit 1cf68b32a1
64 changed files with 1275 additions and 488 deletions
+64
View File
@@ -0,0 +1,64 @@
using UnityEngine;
public class BarBase : MonoBehaviour
{
public float firingSpeed = 0.03f;
public bool canFireLeft = true;
public bool canFireRight = true;
public bool canFireTop = true;
public bool canFireBottom = true;
protected float firingTimer = 0.0f;
protected Bounds bounds;
protected bool canFire = true;
protected void Initialize()
{
SpriteRenderer spriteRenderer = GetComponent<SpriteRenderer>();
bounds = spriteRenderer.bounds;
var walls = GameObject.FindGameObjectsWithTag("Wall");
if (walls.Length > 0)
{
// Cap the firing ability if the bar is already intersecting with a wall.
foreach (var wall in walls)
{
var wallBounds = wall.GetComponent<SpriteRenderer>().bounds;
if (wallBounds.Intersects(bounds))
{
canFire = false;
// Register the intersection with the WorldFiller.
var worldFiller = GameObject.FindAnyObjectByType<WorldFiller>();
if (worldFiller != null)
{
var intersectionBounds = GetIntersectionBounds(wallBounds, bounds);
worldFiller.SetWallPosition(intersectionBounds.center);
}
break;
}
}
}
}
protected Bounds GetIntersectionBounds(Bounds wallBounds, Bounds barBounds)
{
Bounds intersectionBounds = new Bounds();
intersectionBounds.SetMinMax(
Vector3.Max(wallBounds.min, barBounds.min),
Vector3.Min(wallBounds.max, barBounds.max)
);
return intersectionBounds;
}
}
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: cc658019d9b712031b48788b92920756
@@ -0,0 +1,65 @@
using UnityEngine;
public class HorizontalBar : BarBase
{
public GameObject horizontalBarrierPrefab;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
Initialize();
}
// Update is called once per frame
void FixedUpdate()
{
if (canFire)
{
firingTimer += Time.fixedDeltaTime;
if (firingTimer >= firingSpeed)
{
if (canFireLeft)
{
Vector3 leftSide = new Vector3(
bounds.min.x - (bounds.size.x / 2.0f),
bounds.center.y,
bounds.center.z
);
var left = Instantiate(horizontalBarrierPrefab, leftSide, Quaternion.identity);
var leftBarScript = left.GetComponent<HorizontalBar>();
leftBarScript.firingSpeed = firingSpeed;
leftBarScript.canFireRight = false;
Physics2D.IgnoreCollision(
left.GetComponent<Collider2D>(),
GetComponent<Collider2D>(),
true
);
}
if (canFireRight)
{
Vector3 rightSide = new Vector3(
bounds.max.x + (bounds.size.x / 2.0f),
bounds.center.y,
bounds.center.z
);
var right = Instantiate(horizontalBarrierPrefab, rightSide, Quaternion.identity);
var rightBarScript = right.GetComponent<HorizontalBar>();
rightBarScript.firingSpeed = firingSpeed;
rightBarScript.canFireLeft = false;
Physics2D.IgnoreCollision(
right.GetComponent<Collider2D>(),
GetComponent<Collider2D>(),
true
);
}
canFire = false;
}
}
}
}
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 3f0f68050bf7251f7b0cac4505293fdc
@@ -0,0 +1,58 @@
using UnityEngine;
public class HorizontalLaser : BarBase
{
public GameObject horizontalLaserPrefab;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
Initialize();
}
// Update is called once per frame
void FixedUpdate()
{
if (canFire)
{
if (canFireLeft)
{
Vector3 leftSide = new Vector3(
bounds.min.x - (bounds.size.x / 2.0f),
bounds.center.y,
bounds.center.z
);
var left = Instantiate(horizontalLaserPrefab, leftSide, Quaternion.identity);
var leftLaserScript = left.GetComponent<HorizontalLaser>();
leftLaserScript.canFireRight = false;
Physics2D.IgnoreCollision(
left.GetComponent<Collider2D>(),
GetComponent<Collider2D>(),
true
);
}
if (canFireRight)
{
Vector3 rightSide = new Vector3(
bounds.max.x + (bounds.size.x / 2.0f),
bounds.center.y,
bounds.center.z
);
var right = Instantiate(horizontalLaserPrefab, rightSide, Quaternion.identity);
var rightLaserScript = right.GetComponent<HorizontalLaser>();
rightLaserScript.canFireLeft = false;
Physics2D.IgnoreCollision(
right.GetComponent<Collider2D>(),
GetComponent<Collider2D>(),
true
);
}
canFire = false;
}
}
}
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: c66cc5abc472175528412b7935b3ccbd
+520
View File
@@ -0,0 +1,520 @@
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 AudioClip denied;
public AudioClip victory;
public MusicManager musicManager;
public Fader fader;
public GameObject horizontalBarrierPrefab;
public GameObject verticalBarrierPrefab;
public GameObject horizontalLaserPrefab;
public GameObject verticalLaserPrefab;
public int lives = 8;
public int score = 0;
public int bonus = 3100;
public Animator gunAnimator;
public Animator muzzleAnimator;
public SpriteRenderer muzzleRenderer;
public GameObject debrisPrefab;
public int magnetCount = 0;
public int laserCount = 0;
public float barPower = 0f;
public GameObject magnetLeft;
public GameObject magnetRight;
public GameObject magnetTop;
public GameObject magnetBottom;
public BallSpawner ballSpawner;
public int percentCovered = 0;
private bool flipped = false;
private bool gameOver = false;
private AudioSource audioSource;
private bool canFire = true;
private float damageTimer = 0.0f;
private float damageCooldown = 0.5f;
private bool exploding = false;
private Bounds playerBounds;
private Bounds boardBounds;
private bool laserActive = false;
private bool magnetActive = false;
private float magnetDuration = 5.0f;
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()
{
audioSource = GetComponent<AudioSource>();
Cursor.visible = false;
if (!TryGetWorldBounds(gameObject, out playerBounds))
{
Debug.LogWarning("Could not get world bounds for player.");
}
if (!TryGetWorldBounds(gameBoard, out boardBounds))
{
Debug.LogWarning("Could not get world bounds for gameBoard.");
return;
}
// TODO: Generalize this to work with subset of the board bounds.
// Initialize the magnets.
magnetLeft.GetComponent<SpriteRenderer>().enabled = false;
magnetRight.GetComponent<SpriteRenderer>().enabled = false;
magnetTop.GetComponent<SpriteRenderer>().enabled = false;
magnetBottom.GetComponent<SpriteRenderer>().enabled = false;
magnetLeft.transform.position = new Vector3(
boardBounds.min.x + magnetLeft.GetComponent<SpriteRenderer>().bounds.extents.x,
transform.position.y,
transform.position.z
);
magnetRight.transform.position = new Vector3(
boardBounds.max.x - magnetRight.GetComponent<SpriteRenderer>().bounds.extents.x,
transform.position.y,
transform.position.z
);
magnetTop.transform.position = new Vector3(
transform.position.x,
boardBounds.max.y - magnetTop.GetComponent<SpriteRenderer>().bounds.extents.y,
transform.position.z
);
magnetBottom.transform.position = new Vector3(
transform.position.x,
boardBounds.min.y + magnetBottom.GetComponent<SpriteRenderer>().bounds.extents.y,
transform.position.z
);
}
// Update is called once per frame
void Update()
{
// Foce reset the rotation.
transform.rotation = Quaternion.identity;
transform.eulerAngles = new Vector3(0, 0, flipped ? 90 : 0);
// Update the damage timer.
if (damageTimer > 0.0f)
{
damageTimer -= Time.deltaTime;
}
// Player movement is based on the mouse position, clamped to the game board bounds.
if (!exploding)
{
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 / 2.0f, boardBounds.max.x - extents.x / 2.0f);
float clampedY = Mathf.Clamp(mouseWorld.y, boardBounds.min.y + extents.y / 2.0f, boardBounds.max.y - extents.y / 2.0f);
transform.position = new Vector3(clampedX, clampedY, transform.position.z);
}
// Handle keyboard input for flipping, game over, and toggling laser/magnet modes.
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 (Keyboard.current.wKey.wasPressedThisFrame)
{
if (laserActive)
{
DisableLaser();
}
else
{
EnableLaser();
}
}
if (Keyboard.current.dKey.wasPressedThisFrame)
{
if (magnetActive)
{
DisableMagnet();
}
else
{
EnableMagnet();
}
}
}
// Mouse input handling for firing lasers, magnets, or normal barriers.
if (Mouse.current.leftButton.wasPressedThisFrame && canFire && !exploding)
{
if (laserActive)
{
FireLaser();
}
else if (magnetActive)
{
FireMagnet();
}
else
{
FireNormal();
}
}
// Check for game over or revival conditions.
if (gameOver)
{
StartCoroutine(EndGame());
gameOver = false; // Reset gameOver to prevent multiple coroutine starts
}
if (killedByShark)
{
killedByShark = false; // Reset the flag to prevent multiple sound plays.
StartCoroutine(Revive());
}
// Handle magnet timer countdown and deactivation.
if (magnetTimer > 0.0f)
{
magnetTimer -= Time.deltaTime;
if (magnetTimer <= 0.0f)
{
magnetLeft.GetComponent<SpriteRenderer>().enabled = false;
magnetRight.GetComponent<SpriteRenderer>().enabled = false;
magnetTop.GetComponent<SpriteRenderer>().enabled = false;
magnetBottom.GetComponent<SpriteRenderer>().enabled = false;
ballSpawner.MagnetInactive();
}
}
}
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 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();
audioSource.PlayOneShot(defeat);
yield return new WaitForSeconds(1.3f);
fader.ResetFader();
}
public void BarrierFinished()
{
canFire = true;
}
public IEnumerator SetGameOver()
{
yield return new WaitForSeconds(3.0f);
gameOver = true;
}
private void EnableLaser()
{
if (laserCount > 0)
{
laserActive = true;
magnetActive = false;
audioSource.PlayOneShot(laserCharge);
gunAnimator.SetBool("laserActivated", true);
gunAnimator.SetBool("magnetActivated", false);
}
else
{
if (!audioSource.isPlaying)
{
audioSource.PlayOneShot(denied);
}
}
}
private void EnableMagnet()
{
if (magnetCount > 0)
{
laserActive = false;
magnetActive = true;
audioSource.PlayOneShot(magnetIdle);
gunAnimator.SetBool("laserActivated", false);
gunAnimator.SetBool("magnetActivated", true);
}
else
{
if (!audioSource.isPlaying)
{
audioSource.PlayOneShot(denied);
}
}
}
private void DisableLaser()
{
if (laserActive)
{
laserActive = false;
audioSource.PlayOneShot(laserDischarge);
gunAnimator.SetBool("laserActivated", false);
}
}
private void DisableMagnet()
{
if (magnetActive)
{
magnetActive = false;
gunAnimator.SetBool("magnetActivated", false);
}
}
private void FireNormal()
{
canFire = false;
audioSource.PlayOneShot(normalShoot);
muzzleAnimator.SetTrigger("firing");
if (flipped)
{
Instantiate(verticalBarrierPrefab, transform.position, Quaternion.identity);
}
else
{
Instantiate(horizontalBarrierPrefab, transform.position, Quaternion.identity);
}
}
private void FireLaser()
{
canFire = false;
audioSource.PlayOneShot(laserShoot);
muzzleAnimator.SetTrigger("firing");
laserCount--;
if (flipped)
{
Instantiate(verticalLaserPrefab, transform.position, Quaternion.identity);
}
else
{
Instantiate(horizontalLaserPrefab, transform.position, Quaternion.identity);
}
DisableLaser();
}
private void FireMagnet()
{
audioSource.PlayOneShot(magnetShoot);
muzzleAnimator.SetTrigger("firing");
magnetCount--;
magnetTimer = magnetDuration;
if (flipped)
{
magnetTop.GetComponent<SpriteRenderer>().enabled = true;
magnetBottom.GetComponent<SpriteRenderer>().enabled = true;
}
else
{
magnetLeft.GetComponent<SpriteRenderer>().enabled = true;
magnetRight.GetComponent<SpriteRenderer>().enabled = true;
}
ballSpawner.MagnetActive(flipped);
DisableMagnet();
}
private void Explode()
{
if (exploding)
{
return;
}
exploding = true;
gunAnimator.SetTrigger("playerKilled");
muzzleRenderer.enabled = false;
audioSource.PlayOneShot(death);
for (int i = 0; i < 8; i++)
{
SpawnDebris();
}
}
private void SpawnDebris()
{
Vector2 randomOffset = Random.insideUnitCircle * playerBounds.extents.magnitude;
Instantiate(
debrisPrefab,
transform.position + new Vector3(randomOffset.x, randomOffset.y, 0),
Quaternion.identity
);
}
public void OnBarHit()
{
if (damageTimer > 0.0f)
{
return;
}
lives--;
damageTimer = damageCooldown;
if (lives <= 0)
{
Explode();
StartCoroutine(SetGameOver());
}
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);
}
}
}
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 74c92b00befb9d18ea7fbbbda06eb860
+22
View File
@@ -0,0 +1,22 @@
using UnityEngine;
public class PlayerGun : MonoBehaviour
{
public Player player;
private Animator animator;
private SpriteRenderer spriteRenderer;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
spriteRenderer = GetComponent<SpriteRenderer>();
animator = GetComponent<Animator>();
}
public void AnimationFinished()
{
spriteRenderer.enabled = false;
}
}
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: d8f50aeaa35e8b0e7bd0bb61282f92bd
+65
View File
@@ -0,0 +1,65 @@
using UnityEngine;
public class VerticalBar : BarBase
{
public GameObject verticalBarrierPrefab;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
Initialize();
}
// Update is called once per frame
void FixedUpdate()
{
if (canFire)
{
firingTimer += Time.fixedDeltaTime;
if (firingTimer >= firingSpeed)
{
if (canFireTop)
{
Vector3 topSide = new Vector3(
bounds.center.x,
bounds.max.y + (bounds.size.y / 2.0f),
bounds.center.z
);
var top = Instantiate(verticalBarrierPrefab, topSide, Quaternion.identity);
var topBarScript = top.GetComponent<VerticalBar>();
topBarScript.firingSpeed = firingSpeed;
topBarScript.canFireBottom = false;
Physics2D.IgnoreCollision(
top.GetComponent<Collider2D>(),
GetComponent<Collider2D>(),
true
);
}
if (canFireBottom)
{
Vector3 bottomSide = new Vector3(
bounds.center.x,
bounds.min.y - (bounds.size.y / 2.0f),
bounds.center.z
);
var bottom = Instantiate(verticalBarrierPrefab, bottomSide, Quaternion.identity);
var bottomBarScript = bottom.GetComponent<VerticalBar>();
bottomBarScript.firingSpeed = firingSpeed;
bottomBarScript.canFireTop = false;
Physics2D.IgnoreCollision(
bottom.GetComponent<Collider2D>(),
GetComponent<Collider2D>(),
true
);
}
canFire = false;
}
}
}
}
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 6dc29ecf320c579669972d171a926ff4
@@ -0,0 +1,58 @@
using UnityEngine;
public class VerticalLaser : BarBase
{
public GameObject verticalLaserPrefab;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
Initialize();
}
// Update is called once per frame
void FixedUpdate()
{
if (canFire)
{
if (canFireTop)
{
Vector3 topSide = new Vector3(
bounds.center.x,
bounds.max.y + (bounds.size.y / 2.0f),
bounds.center.z
);
var top = Instantiate(verticalLaserPrefab, topSide, Quaternion.identity);
var topLaserScript = top.GetComponent<VerticalLaser>();
topLaserScript.canFireBottom = false;
Physics2D.IgnoreCollision(
top.GetComponent<Collider2D>(),
GetComponent<Collider2D>(),
true
);
}
if (canFireBottom)
{
Vector3 bottomSide = new Vector3(
bounds.center.x,
bounds.min.y - (bounds.size.y / 2.0f),
bounds.center.z
);
var bottom = Instantiate(verticalLaserPrefab, bottomSide, Quaternion.identity);
var bottomLaserScript = bottom.GetComponent<VerticalLaser>();
bottomLaserScript.canFireTop = false;
Physics2D.IgnoreCollision(
bottom.GetComponent<Collider2D>(),
GetComponent<Collider2D>(),
true
);
}
canFire = false;
}
}
}
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: e6ef48767c03d5b62b0e336a5691a187