Files
3DBlobici-WorkingTitle/3D blobici/Assets/Prefabs/Scripts/RoomHandler.cs

152 lines
4.3 KiB
C#
Raw Normal View History

2025-06-30 16:28:30 +02:00
using System.Collections.Generic;
2025-06-25 13:19:20 +02:00
using UnityEngine;
using UnityEngine.AI;
2025-06-25 13:19:20 +02:00
2025-06-25 23:54:00 +02:00
///<summary>
/// Object handling Room logic
///</summary>
///
public class RoomHandler : MonoBehaviour
2025-06-25 13:19:20 +02:00
{
2025-06-30 16:28:30 +02:00
[Header("Doors")]
2025-06-25 13:19:20 +02:00
[SerializeField] private GameObject wallNorth;
[SerializeField] private GameObject wallSouth;
[SerializeField] private GameObject wallEast;
[SerializeField] private GameObject wallWest;
2025-06-25 23:54:00 +02:00
2025-06-30 16:28:30 +02:00
public enum Side
{
North,
South,
East,
West
}
2025-08-02 09:11:23 +02:00
// Room Spawn Points
[Header("Spawn Points")]
[SerializeField] private List<GameObject> spawnPoints;
[SerializeField] private GameObject test;
2025-08-02 16:04:33 +02:00
[SerializeField] private bool allowSpawn = false;
2025-08-02 09:11:23 +02:00
2025-06-30 16:28:30 +02:00
private readonly Dictionary<Side, DoorAnimation> doors = new();
public void RegisterDoor(Side dir, DoorAnimation doorAnim) => doors[dir] = doorAnim;
2025-06-25 23:54:00 +02:00
/// <summary>
/// Creates entrances to corridors leading to other rooms
/// </summary>
/// <param name="northOpen"></param>
/// <param name="southOpen"></param>
/// <param name="eastOpen"></param>
/// <param name="westOpen"></param>
2025-06-25 13:19:20 +02:00
public void SetEntrances(bool northOpen, bool southOpen, bool eastOpen, bool westOpen)
{
wallNorth.SetActive(!northOpen);
wallSouth.SetActive(!southOpen);
wallEast.SetActive(!eastOpen);
wallWest.SetActive(!westOpen);
}
2025-06-30 16:28:30 +02:00
public void ToggleAllDoors()
{
foreach (DoorAnimation door in doors.Values)
{
door.ToggleDoor();
}
}
public void SetDoorState(Side dir, bool open)
{
if (doors.TryGetValue(dir, out DoorAnimation da))
{
da.ToggleDoor();
Debug.Log("Door " + dir + " is now " + (open ? "open" : "closed"));
}
}
2025-08-02 09:11:23 +02:00
public void SpawnEnemies(List<GameObject> enemyPrefabs)
{
if (!allowSpawn || enemyPrefabs == null || enemyPrefabs.Count == 0)
2025-08-02 09:11:23 +02:00
{
Debug.LogWarning("Enemy spawning is not allowed or no enemies to spawn.");
2025-08-02 16:04:33 +02:00
return;
}
StartCoroutine(SpawnEnemiesWithDelay(enemyPrefabs));
}
private System.Collections.IEnumerator SpawnEnemiesWithDelay(List<GameObject> enemyPrefabs)
{
// Po<50>kej a<> bude NavMesh ready
yield return new WaitUntil(() => NavMesh.CalculateTriangulation().indices.Length > 0);
for (int i = 0; i < enemyPrefabs.Count; i++)
2025-08-02 16:04:33 +02:00
{
if (spawnPoints == null || spawnPoints.Count == 0) yield break;
GameObject spawnPoint = spawnPoints[i % spawnPoints.Count];
Vector3 spawnPosition = spawnPoint.transform.position + new Vector3(0, 1, 0);
2025-08-02 09:11:23 +02:00
GameObject enemy = Instantiate(enemyPrefabs[i], spawnPosition, Quaternion.identity);
2025-08-02 09:11:23 +02:00
// Po<50>kej ne<6E> se objekt pln<6C> inicializuje
yield return new WaitForEndOfFrame();
2025-08-02 09:11:23 +02:00
// Zkus um<75>stit enemy na NavMesh
NavMeshAgent agent = enemy.GetComponent<NavMeshAgent>();
if (agent != null)
{
if (!agent.isOnNavMesh)
{
agent.Warp(spawnPosition);
}
if (agent.isOnNavMesh)
{
Debug.Log("Enemy successfully placed on NavMesh");
}
else
{
Debug.LogWarning("Enemy could not be placed on NavMesh");
}
2025-08-02 16:04:33 +02:00
}
// Mal<61> delay mezi spawny
yield return new WaitForSeconds(0.2f);
}
2025-08-02 09:11:23 +02:00
}
2025-08-02 09:11:23 +02:00
void Start()
{
int enemyCount = Random.Range(1, 4);
List<GameObject> enemyPrefabs = new List<GameObject>();
for (int i = 0; i < enemyCount; i++)
{
enemyPrefabs.Add(test);
}
StartCoroutine(DelayedSpawn(enemyPrefabs));
}
System.Collections.IEnumerator DelayedSpawn(List<GameObject> enemyPrefabs)
{
// Po<50>kej ne<6E> se m<>stnost pln<6C> inicializuje
yield return new WaitForSeconds(0.5f);
2025-08-02 09:11:23 +02:00
SpawnEnemies(enemyPrefabs);
}
System.Collections.IEnumerator SetupEnemyAfterDelay(GameObject enemy)
{
yield return new WaitForSeconds(0.1f);
NavMeshAgent agent = enemy.GetComponent<NavMeshAgent>();
if (agent != null && agent.isOnNavMesh)
{
Debug.Log("Enemy je ready");
// Enemy je ready
}
}
2025-06-25 13:19:20 +02:00
}