TO SQUASH: Started shark.
This commit is contained in:
@@ -0,0 +1,203 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public class BallSpawner : MonoBehaviour
|
||||
{
|
||||
public GameObject basicBallPrefab;
|
||||
|
||||
public GameObject nuclearBallPrefab;
|
||||
|
||||
public GameObject glassBallPrefab;
|
||||
|
||||
public GameObject eyeBallPrefab;
|
||||
|
||||
public GameObject oozeBallPrefab;
|
||||
|
||||
public GameObject fruitBallPrefab;
|
||||
|
||||
public GameObject gameBoard;
|
||||
|
||||
private static WaitForSeconds _waitForSeconds0_5 = new WaitForSeconds(0.5f);
|
||||
|
||||
private Queue<System.Action> ballQueue = new Queue<System.Action>();
|
||||
|
||||
private Queue<GameObject> balls = new Queue<GameObject>();
|
||||
|
||||
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
||||
void Start()
|
||||
{
|
||||
int currentLevel = PlayerPrefs.GetInt("CurrentLevel");
|
||||
|
||||
ballQueue.Enqueue(() => SpawnBall(basicBallPrefab));
|
||||
ballQueue.Enqueue(() => SpawnBall(basicBallPrefab));
|
||||
// ballQueue.Enqueue(() => SpawnBall(basicBallPrefab));
|
||||
// ballQueue.Enqueue(() => SpawnBall(fruitBallPrefab));
|
||||
// ballQueue.Enqueue(() => SpawnBall(fruitBallPrefab));
|
||||
// ballQueue.Enqueue(() => SpawnBall(oozeBallPrefab));
|
||||
// ballQueue.Enqueue(() => SpawnBall(nuclearBallPrefab));
|
||||
// ballQueue.Enqueue(() => SpawnBall(glassBallPrefab));
|
||||
// ballQueue.Enqueue(() => SpawnBall(glassBallPrefab));
|
||||
// ballQueue.Enqueue(() => SpawnBall(glassBallPrefab));
|
||||
// ballQueue.Enqueue(() => SpawnBall(glassBallPrefab));
|
||||
// ballQueue.Enqueue(() => SpawnBall(glassBallPrefab));
|
||||
// ballQueue.Enqueue(() => SpawnBall(eyeBallPrefab));
|
||||
|
||||
StartCoroutine(SpawnBalls());
|
||||
}
|
||||
|
||||
public void SpawnBallOfType(string ballType, Vector2? position = null)
|
||||
{
|
||||
switch (ballType)
|
||||
{
|
||||
case "BasicBall":
|
||||
SpawnBall(basicBallPrefab, position);
|
||||
break;
|
||||
case "NuclearBall":
|
||||
SpawnBall(nuclearBallPrefab, position);
|
||||
break;
|
||||
case "GlassBall":
|
||||
SpawnBall(glassBallPrefab, position);
|
||||
break;
|
||||
case "EyeBall":
|
||||
SpawnBall(eyeBallPrefab, position);
|
||||
break;
|
||||
case "OozeBall":
|
||||
SpawnBall(oozeBallPrefab, position);
|
||||
break;
|
||||
case "FruitBall":
|
||||
SpawnBall(fruitBallPrefab, position);
|
||||
break;
|
||||
default:
|
||||
Debug.LogWarning($"Unknown ball type: {ballType}");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void SpawnBall(GameObject ballPrefab, Vector2? position = null)
|
||||
{
|
||||
if (ballPrefab == null || gameBoard == null)
|
||||
{
|
||||
Debug.LogWarning("BallSpawner: Missing ballPrefab or gameBoard reference.");
|
||||
return;
|
||||
}
|
||||
|
||||
Vector3 spawnPosition = position.HasValue ?
|
||||
new Vector3(position.Value.x, position.Value.y, gameBoard.transform.position.z) :
|
||||
GetRandomPositionInsideGameBoard();
|
||||
var newBall = Instantiate(ballPrefab, spawnPosition, Quaternion.identity);
|
||||
|
||||
if (ballPrefab == oozeBallPrefab)
|
||||
{
|
||||
var oozeBallScript = newBall.GetComponent<OozeBall>();
|
||||
if (oozeBallScript != null)
|
||||
{
|
||||
oozeBallScript.spawner = this;
|
||||
}
|
||||
}
|
||||
|
||||
balls.Enqueue(newBall);
|
||||
|
||||
FixCollisions();
|
||||
}
|
||||
|
||||
private Vector3 GetRandomPositionInsideGameBoard()
|
||||
{
|
||||
Bounds bounds;
|
||||
|
||||
Collider2D collider2D = gameBoard.GetComponent<Collider2D>();
|
||||
if (collider2D != null)
|
||||
{
|
||||
bounds = collider2D.bounds;
|
||||
}
|
||||
else
|
||||
{
|
||||
Renderer boardRenderer = gameBoard.GetComponent<Renderer>();
|
||||
if (boardRenderer == null)
|
||||
{
|
||||
Debug.LogWarning("BallSpawner: gameBoard needs a Collider2D or Renderer to read bounds.");
|
||||
return gameBoard.transform.position;
|
||||
}
|
||||
|
||||
bounds = boardRenderer.bounds;
|
||||
}
|
||||
|
||||
const float edgeInset = 0.1f;
|
||||
float randomX = Random.Range(bounds.min.x + edgeInset, bounds.max.x - edgeInset);
|
||||
float randomY = Random.Range(bounds.min.y + edgeInset, bounds.max.y - edgeInset);
|
||||
|
||||
return new Vector3(randomX, randomY, gameBoard.transform.position.z);
|
||||
}
|
||||
|
||||
public void FixCollisions() {
|
||||
// Disable collision between existing balls of certain types.
|
||||
// Only basic balls and fruit balls will collide with each other, while ooze,
|
||||
// nuclear, glass, and eye balls will not collide with any other balls.
|
||||
// As an exception, glass balls will collide with other glass balls.
|
||||
foreach (var ball in balls)
|
||||
{
|
||||
if (ball == null) continue;
|
||||
|
||||
foreach (var otherBall in balls)
|
||||
{
|
||||
if (otherBall == null) continue;
|
||||
if (ball == otherBall) continue;
|
||||
|
||||
bool ballIsBasicOrFruit = ball.CompareTag("BasicBall") || ball.CompareTag("FruitBall");
|
||||
bool otherIsBasicOrFruit = otherBall.CompareTag("BasicBall") || otherBall.CompareTag("FruitBall");
|
||||
bool bothAreGlass = ball.CompareTag("GlassBall") && otherBall.CompareTag("GlassBall");
|
||||
|
||||
bool shouldCollide = (ballIsBasicOrFruit && otherIsBasicOrFruit) || bothAreGlass;
|
||||
|
||||
if (!shouldCollide)
|
||||
{
|
||||
Physics2D.IgnoreCollision(
|
||||
ball.GetComponent<Collider2D>(),
|
||||
otherBall.GetComponent<Collider2D>(),
|
||||
true
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private IEnumerator SpawnBalls()
|
||||
{
|
||||
while (ballQueue.Count > 0)
|
||||
{
|
||||
System.Action spawnAction = ballQueue.Dequeue();
|
||||
spawnAction.Invoke();
|
||||
yield return _waitForSeconds0_5;
|
||||
}
|
||||
|
||||
FixCollisions();
|
||||
}
|
||||
|
||||
public void MagnetActive(bool flipped)
|
||||
{
|
||||
foreach (var ball in balls)
|
||||
{
|
||||
if (ball == null) continue;
|
||||
|
||||
var ballScript = ball.GetComponent<BallBase>();
|
||||
if (ballScript != null)
|
||||
{
|
||||
ballScript.MagnetActive(flipped);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void MagnetInactive()
|
||||
{
|
||||
foreach (var ball in balls)
|
||||
{
|
||||
if (ball == null) continue;
|
||||
|
||||
var ballScript = ball.GetComponent<BallBase>();
|
||||
if (ballScript != null)
|
||||
{
|
||||
ballScript.MagnetInactive();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b45a86a9500699daf8af02a61c756b2b
|
||||
@@ -0,0 +1,60 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class MultiplierSpawner : MonoBehaviour
|
||||
{
|
||||
public GameObject multiplierPrefab;
|
||||
|
||||
public SpriteRenderer gameBoardSpriteRenderer;
|
||||
|
||||
private Bounds multiplierBounds;
|
||||
|
||||
private Bounds gameBoardBounds;
|
||||
|
||||
private float spawnTimer = 0f;
|
||||
|
||||
private Vector2 spawnPosition;
|
||||
|
||||
private bool isActive = true;
|
||||
|
||||
private static float spawnProbability = 0.1f;
|
||||
|
||||
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
||||
void Start()
|
||||
{
|
||||
gameBoardBounds = gameBoardSpriteRenderer.bounds;
|
||||
multiplierBounds = multiplierPrefab.GetComponent<SpriteRenderer>().bounds;
|
||||
Reset();
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void FixedUpdate()
|
||||
{
|
||||
spawnTimer += Time.fixedDeltaTime;
|
||||
|
||||
if (spawnTimer >= 1f && isActive)
|
||||
{
|
||||
spawnTimer = 0f;
|
||||
if (Random.value < spawnProbability)
|
||||
{
|
||||
GameObject multiplier = Instantiate(multiplierPrefab, spawnPosition, Quaternion.identity);
|
||||
isActive = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
isActive = true;
|
||||
spawnTimer = 0f;
|
||||
spawnPosition = new Vector2(
|
||||
Random.Range(
|
||||
gameBoardBounds.min.x + multiplierBounds.extents.x,
|
||||
gameBoardBounds.max.x - multiplierBounds.extents.x
|
||||
),
|
||||
Random.Range(
|
||||
gameBoardBounds.min.y + multiplierBounds.extents.y,
|
||||
gameBoardBounds.max.y - multiplierBounds.extents.y
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2e3452dfae854c9ec802a4e8fce2813c
|
||||
@@ -0,0 +1,60 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class SharkSpawner : MonoBehaviour
|
||||
{
|
||||
public GameObject sharkPrefab;
|
||||
|
||||
public SpriteRenderer gameBoardSpriteRenderer;
|
||||
|
||||
private Bounds sharkBounds;
|
||||
|
||||
private Bounds gameBoardBounds;
|
||||
|
||||
private float spawnTimer = 0f;
|
||||
|
||||
private Vector2 spawnPosition;
|
||||
|
||||
private bool isActive = true;
|
||||
|
||||
private static float spawnProbability = 0.1f;
|
||||
|
||||
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
||||
void Start()
|
||||
{
|
||||
gameBoardBounds = gameBoardSpriteRenderer.bounds;
|
||||
sharkBounds = sharkPrefab.GetComponent<SpriteRenderer>().bounds;
|
||||
Reset();
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void FixedUpdate()
|
||||
{
|
||||
spawnTimer += Time.fixedDeltaTime;
|
||||
|
||||
if (spawnTimer >= 1f && isActive)
|
||||
{
|
||||
spawnTimer = 0f;
|
||||
if (Random.value < spawnProbability)
|
||||
{
|
||||
GameObject shark = Instantiate(sharkPrefab, spawnPosition, Quaternion.identity);
|
||||
isActive = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
isActive = true;
|
||||
spawnTimer = 0f;
|
||||
spawnPosition = new Vector2(
|
||||
Random.Range(
|
||||
gameBoardBounds.min.x + sharkBounds.extents.x,
|
||||
gameBoardBounds.max.x - sharkBounds.extents.x
|
||||
),
|
||||
Random.Range(
|
||||
gameBoardBounds.min.y + sharkBounds.extents.y,
|
||||
gameBoardBounds.max.y - sharkBounds.extents.y
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 95d73a7ad911de521a39ec73860fe954
|
||||
Reference in New Issue
Block a user