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

61 lines
1.5 KiB
C#
Raw Normal View History

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;
2026-08-10 22:40:00 -04:00
private Bounds sharkBounds;
2026-08-06 09:00:43 -04:00
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;
2026-08-10 22:40:00 -04:00
sharkBounds = sharkPrefab.GetComponent<SpriteRenderer>().bounds;
2026-08-06 09:00:43 -04:00
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-06 09:00:43 -04:00
isActive = false;
}
}
}
public void Reset()
{
isActive = true;
spawnTimer = 0f;
spawnPosition = new Vector2(
Random.Range(
2026-08-10 22:40:00 -04:00
gameBoardBounds.min.x + sharkBounds.extents.x,
gameBoardBounds.max.x - sharkBounds.extents.x
2026-08-06 09:00:43 -04:00
),
Random.Range(
2026-08-10 22:40:00 -04:00
gameBoardBounds.min.y + sharkBounds.extents.y,
gameBoardBounds.max.y - sharkBounds.extents.y
2026-08-06 09:00:43 -04:00
)
);
}
}