Files
Barrack-Unity/Assets/Scripts/Game/WorldFiller.cs
T

76 lines
2.0 KiB
C#

using UnityEngine;
public class WorldFiller : MonoBehaviour
{
public Player player;
public SpriteRenderer gameBoardSpriteRenderer;
public GameObject wallPrefab;
private Bounds gameBoardBounds;
private Vector2?[] wallPositions;
private int wallCount = 0;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
gameBoardBounds = gameBoardSpriteRenderer.bounds;
ClearWalls();
}
public void ClearWalls()
{
wallPositions = new Vector2?[2];
wallPositions[0] = null;
wallPositions[1] = null;
wallCount = 0;
}
public void SetWallPosition(Vector2 position)
{
if (wallCount >= wallPositions.Length)
{
Debug.LogError("Index out of bounds for wall positions.");
return;
}
wallPositions[wallCount] = position;
wallCount++;
SpawnWall();
}
public void SpawnWall()
{
// If both wall positions are set, spawn a wall.
if (wallPositions[0] != null && wallPositions[1] != null)
{
Debug.Log($"Spawning wall with wall line: ({wallPositions[0]}, {wallPositions[1]})");
// Get all the balls in the scene.
var ballSpawner = FindAnyObjectByType<BallSpawner>();
var balls = ballSpawner.GetBalls();
// There are three possible scenarios for the wall positions:
// 1. Only one side of the wall line is empty (either left or right).
// 2. All sides of the wall line are occupied.
// 3. All sides of the wall line are empty.
// Destroy all bar segments after we are done.
GameObject[] barSegments = GameObject.FindGameObjectsWithTag("Bar");
foreach (GameObject barSegment in barSegments)
{
Destroy(barSegment);
}
// Clear the pending wall line.
ClearWalls();
}
}
}