Files
Barrack-Unity/Assets/Scripts/Game/Spawners/SharkSpawner.cs
T

68 lines
1.8 KiB
C#
Raw Normal View History

2026-08-06 09:00:43 -04:00
using UnityEngine;
2026-08-25 22:24:07 -04:00
public class SharkSpawner : Spawner
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;
2026-08-27 15:34:06 -04:00
public Player player;
2026-08-06 09:00:43 -04:00
private Bounds gameBoardBounds;
private float spawnTimer = 0f;
private Vector2 spawnPosition;
private bool isActive = true;
2026-08-25 22:24:07 -04:00
private static float spawnProbability = 0.05f;
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;
2026-08-27 15:34:06 -04:00
if (spawnTimer >= 1f && isActive && isSpawning && player.level >= 10)
2026-08-06 09:00:43 -04:00
{
spawnTimer = 0f;
if (Random.value < spawnProbability)
{
2026-08-10 22:40:00 -04:00
GameObject shark = Instantiate(sharkPrefab, spawnPosition, Quaternion.identity);
Shark sharkScript = shark.GetComponent<Shark>();
sharkScript.gameBoardSpriteRenderer = gameBoardSpriteRenderer;
2026-08-27 15:34:06 -04:00
sharkScript.player = player;
2026-08-06 09:00:43 -04:00
isActive = false;
}
}
}
public void Reset()
{
isActive = true;
spawnTimer = 0f;
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(
boardCenter.x - halfSpawnArea.x,
boardCenter.x + halfSpawnArea.x
2026-08-06 09:00:43 -04:00
),
Random.Range(
boardCenter.y - halfSpawnArea.y,
boardCenter.y + halfSpawnArea.y
2026-08-06 09:00:43 -04:00
)
);
}
}