Working monster spawns

This commit is contained in:
2025-09-19 13:46:24 +02:00
parent 40c3867d29
commit 2ec5c3d855
4 changed files with 393 additions and 135 deletions

View File

@@ -55,7 +55,7 @@ namespace Minigames.DivingForPictures
private void Start()
{
// Subscribe to tile spawned event
TrenchTileSpawner tileSpawner = FindObjectOfType<TrenchTileSpawner>();
TrenchTileSpawner tileSpawner = FindFirstObjectByType<TrenchTileSpawner>();
if (tileSpawner != null)
{
tileSpawner.onTileSpawned.AddListener(OnTileSpawned);
@@ -104,8 +104,8 @@ namespace Minigames.DivingForPictures
// Pick a random spawn point from this tile
MonsterSpawnPoint spawnPoint = spawnPoints[UnityEngine.Random.Range(0, spawnPoints.Length)];
// Spawn the monster
SpawnMonster(spawnPoint.transform.position);
// Spawn the monster at the spawn point and parent it
SpawnMonster(spawnPoint.transform);
// Reset timer and adjust probability
lastSpawnTime = Time.time;
@@ -115,7 +115,7 @@ namespace Minigames.DivingForPictures
}
}
private void SpawnMonster(Vector3 position)
private void SpawnMonster(Transform spawnPoint)
{
if (monsterPrefabs.Length == 0)
{
@@ -126,12 +126,15 @@ namespace Minigames.DivingForPictures
// Select random monster prefab
GameObject prefab = monsterPrefabs[UnityEngine.Random.Range(0, monsterPrefabs.Length)];
// Instantiate monster
GameObject monsterObj = Instantiate(prefab, position, Quaternion.identity);
// Instantiate monster at spawn point position
GameObject monsterObj = Instantiate(prefab, spawnPoint.position, Quaternion.identity);
Monster monster = monsterObj.GetComponent<Monster>();
if (monster != null)
{
// Parent the monster to the spawn point so it moves with the tile
monsterObj.transform.SetParent(spawnPoint);
// Subscribe to monster events
monster.OnPictureTaken += OnMonsterPictureTaken;
monster.OnMonsterDespawned += OnMonsterDespawned;

View File

@@ -13,7 +13,7 @@ namespace Minigames.DivingForPictures
private void Start()
{
gameManager = FindObjectOfType<DivingGameManager>();
gameManager = FindFirstObjectByType<DivingGameManager>();
if (gameManager != null)
{

View File

@@ -61,13 +61,19 @@ namespace Minigames.DivingForPictures
if (mainCamera == null)
return false;
Vector3 viewportPoint = mainCamera.WorldToViewportPoint(transform.position);
float buffer = 0.2f; // Extra buffer outside the screen
// Get the world position (will account for parent movement)
Vector3 worldPosition = transform.position;
Vector3 viewportPoint = mainCamera.WorldToViewportPoint(worldPosition);
return viewportPoint.x > -buffer &&
viewportPoint.x < 1 + buffer &&
viewportPoint.y > -buffer &&
viewportPoint.y < 1 + buffer;
// Adjust buffer to be larger below the screen to account for downward movement
float bufferSides = 0.2f;
float bufferTop = 0.2f;
float bufferBottom = 0.5f; // Larger buffer below the screen
return viewportPoint.x > -bufferSides &&
viewportPoint.x < 1 + bufferSides &&
viewportPoint.y > -bufferBottom &&
viewportPoint.y < 1 + bufferTop;
}
private void OnTriggerEnter2D(Collider2D other)