TO SQUASH: Added laser firing.

This commit is contained in:
2026-08-06 07:55:48 -04:00
parent 46198d6c44
commit 1dd8b8b786
14 changed files with 1077 additions and 8 deletions
+82
View File
@@ -0,0 +1,82 @@
using UnityEngine;
public class HorizontalLaser : MonoBehaviour
{
public GameObject horizontalLaserPrefab;
public bool canFireLeft = true;
public bool canFireRight = true;
private Bounds bounds;
private bool canFire = true;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
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)
{
if (wall.GetComponent<Collider2D>().bounds.Intersects(bounds))
{
canFire = false;
break;
}
}
}
}
// 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
+13 -8
View File
@@ -38,6 +38,10 @@ public class Player : MonoBehaviour
public GameObject verticalBarrierPrefab;
public GameObject horizontalLaserPrefab;
public GameObject verticalLaserPrefab;
public int lives = 8;
public int score = 0;
@@ -109,6 +113,7 @@ public class Player : MonoBehaviour
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;
@@ -367,14 +372,14 @@ public class Player : MonoBehaviour
muzzleAnimator.SetTrigger("firing");
laserCount--;
// if (flipped)
// {
// Instantiate(verticalBarrierPrefab, transform.position, Quaternion.identity);
// }
// else
// {
// Instantiate(horizontalBarrierPrefab, transform.position, Quaternion.identity);
// }
if (flipped)
{
Instantiate(verticalLaserPrefab, transform.position, Quaternion.identity);
}
else
{
Instantiate(horizontalLaserPrefab, transform.position, Quaternion.identity);
}
DisableLaser();
}
+82
View File
@@ -0,0 +1,82 @@
using UnityEngine;
public class VerticalLaser : MonoBehaviour
{
public GameObject verticalLaserPrefab;
public bool canFireTop = true;
public bool canFireBottom = true;
private Bounds bounds;
private bool canFire = true;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
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)
{
if (wall.GetComponent<Collider2D>().bounds.Intersects(bounds))
{
canFire = false;
break;
}
}
}
}
// 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