2026-08-06 09:00:43 -04:00
|
|
|
using UnityEngine;
|
|
|
|
|
|
2026-08-10 22:40:00 -04:00
|
|
|
public class SharkSpawner : MonoBehaviour
|
2026-08-06 09:00:43 -04:00
|
|
|
{
|
2026-08-10 22:40:00 -04:00
|
|
|
public GameObject sharkPrefab;
|
2026-08-06 09:00:43 -04:00
|
|
|
|
|
|
|
|
public SpriteRenderer gameBoardSpriteRenderer;
|
|
|
|
|
|
|
|
|
|
private Bounds gameBoardBounds;
|
|
|
|
|
|
|
|
|
|
private float spawnTimer = 0f;
|
|
|
|
|
|
|
|
|
|
private Vector2 spawnPosition;
|
|
|
|
|
|
|
|
|
|
private bool isActive = true;
|
|
|
|
|
|
2026-08-12 00:52:57 -04:00
|
|
|
private static float spawnProbability = 0.1f;
|
2026-08-06 09:00:43 -04:00
|
|
|
|
|
|
|
|
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
|
|
|
|
void Start()
|
|
|
|
|
{
|
|
|
|
|
gameBoardBounds = gameBoardSpriteRenderer.bounds;
|
|
|
|
|
Reset();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update is called once per frame
|
|
|
|
|
void FixedUpdate()
|
|
|
|
|
{
|
|
|
|
|
spawnTimer += Time.fixedDeltaTime;
|
|
|
|
|
|
|
|
|
|
if (spawnTimer >= 1f && isActive)
|
|
|
|
|
{
|
|
|
|
|
spawnTimer = 0f;
|
|
|
|
|
if (Random.value < spawnProbability)
|
|
|
|
|
{
|
2026-08-10 22:40:00 -04:00
|
|
|
GameObject shark = Instantiate(sharkPrefab, spawnPosition, Quaternion.identity);
|
2026-08-10 22:58:50 -04:00
|
|
|
Shark sharkScript = shark.GetComponent<Shark>();
|
|
|
|
|
sharkScript.gameBoardSpriteRenderer = gameBoardSpriteRenderer;
|
|
|
|
|
sharkScript.player = FindAnyObjectByType<Player>();
|
2026-08-06 09:00:43 -04:00
|
|
|
isActive = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Reset()
|
|
|
|
|
{
|
|
|
|
|
isActive = true;
|
|
|
|
|
spawnTimer = 0f;
|
2026-08-10 22:58:50 -04:00
|
|
|
|
|
|
|
|
Vector2 boardCenter = gameBoardBounds.center;
|
|
|
|
|
Vector2 spawnAreaSize = gameBoardBounds.size * (2f / 3f);
|
|
|
|
|
Vector2 halfSpawnArea = spawnAreaSize * 0.5f;
|
|
|
|
|
|
2026-08-06 09:00:43 -04:00
|
|
|
spawnPosition = new Vector2(
|
|
|
|
|
Random.Range(
|
2026-08-10 22:58:50 -04:00
|
|
|
boardCenter.x - halfSpawnArea.x,
|
|
|
|
|
boardCenter.x + halfSpawnArea.x
|
2026-08-06 09:00:43 -04:00
|
|
|
),
|
|
|
|
|
Random.Range(
|
2026-08-10 22:58:50 -04:00
|
|
|
boardCenter.y - halfSpawnArea.y,
|
|
|
|
|
boardCenter.y + halfSpawnArea.y
|
2026-08-06 09:00:43 -04:00
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|