Pathfinding
Remade prefabs, added scripts for pathfinding through navmesh, rewrote enemy spawn, roomGen and parts of handler
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.AI;
|
||||
|
||||
///<summary>
|
||||
/// Object handling Room logic
|
||||
@@ -65,33 +66,58 @@ public class RoomHandler : MonoBehaviour
|
||||
|
||||
public void SpawnEnemies(List<GameObject> enemyPrefabs)
|
||||
{
|
||||
if (!allowSpawn)
|
||||
if (!allowSpawn || enemyPrefabs == null || enemyPrefabs.Count == 0)
|
||||
{
|
||||
Debug.LogWarning("Enemy spawning is not allowed in this room.");
|
||||
Debug.LogWarning("Enemy spawning is not allowed or no enemies to spawn.");
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
int i = 0;
|
||||
List<GameObject> enemyPrefabsLocal = new List<GameObject>(enemyPrefabs);
|
||||
while (enemyPrefabsLocal.Count > 0)
|
||||
{
|
||||
// Spawns enemy and removes it from the list
|
||||
GameObject enemyPrefab = enemyPrefabsLocal[0];
|
||||
enemyPrefabsLocal.RemoveAt(0);
|
||||
|
||||
// Select a spawn point with round-robin
|
||||
Debug.Log("Ammount of spawn points: " + spawnPoints.Count);
|
||||
GameObject spawnPoint = spawnPoints[i % spawnPoints.Count];
|
||||
|
||||
Instantiate(enemyPrefab, spawnPoint.transform.position + new Vector3(0, 1, 0), Quaternion.identity);
|
||||
Debug.Log("Spawned enemy: " + enemyPrefab.name + " at " + spawnPoint.transform.position);
|
||||
|
||||
i++;
|
||||
}
|
||||
}
|
||||
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++)
|
||||
{
|
||||
if (spawnPoints == null || spawnPoints.Count == 0) yield break;
|
||||
|
||||
GameObject spawnPoint = spawnPoints[i % spawnPoints.Count];
|
||||
Vector3 spawnPosition = spawnPoint.transform.position + new Vector3(0, 1, 0);
|
||||
|
||||
GameObject enemy = Instantiate(enemyPrefabs[i], spawnPosition, Quaternion.identity);
|
||||
|
||||
// Po<50>kej ne<6E> se objekt pln<6C> inicializuje
|
||||
yield return new WaitForEndOfFrame();
|
||||
|
||||
// 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");
|
||||
}
|
||||
}
|
||||
|
||||
// Mal<61> delay mezi spawny
|
||||
yield return new WaitForSeconds(0.2f);
|
||||
}
|
||||
}
|
||||
|
||||
void Start()
|
||||
{
|
||||
int enemyCount = Random.Range(1, 4);
|
||||
@@ -101,6 +127,25 @@ public class RoomHandler : MonoBehaviour
|
||||
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);
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user