Moved from swarm to NavMesh - need to fix the enemy navigation! Review the animation controller to see if anything is being triggered.
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Jobs;
|
||||
using Unity.Jobs;
|
||||
@@ -25,6 +26,7 @@ public class WaveDefinition
|
||||
public class EnemySwarmManager : MonoBehaviour
|
||||
{
|
||||
public static EnemySwarmManager Instance;
|
||||
private List<EnemyAI> activeEnemiesList = new List<EnemyAI>();
|
||||
|
||||
[Header("Swarm Settings")]
|
||||
public float enemySpeed = 3.5f;
|
||||
@@ -34,7 +36,7 @@ public class EnemySwarmManager : MonoBehaviour
|
||||
public Transform[] spawnPoints;
|
||||
public float timeBetweenWaves = 5.0f;
|
||||
[Tooltip("Delay between individual enemy spawns to prevent lag spikes")]
|
||||
public float spawnStagger = 0.05f;
|
||||
public float spawnStagger = 0.05f;
|
||||
|
||||
private int currentWaveIndex = 0;
|
||||
private int activeEnemyCount = 0;
|
||||
@@ -49,8 +51,8 @@ public class EnemySwarmManager : MonoBehaviour
|
||||
void Awake()
|
||||
{
|
||||
Instance = this;
|
||||
transformAccessArray = new TransformAccessArray(1000);
|
||||
targetPositions = new NativeArray<float3>(1, Allocator.Persistent);
|
||||
transformAccessArray = new TransformAccessArray(1000);
|
||||
//targetPositions = new NativeArray<float3>(1, Allocator.Persistent);
|
||||
}
|
||||
|
||||
void Start()
|
||||
@@ -73,9 +75,9 @@ public class EnemySwarmManager : MonoBehaviour
|
||||
{
|
||||
isSpawning = true; // Prevent accidental wave-clears during cooldown
|
||||
Debug.Log($"<color=cyan>Next wave starting in {timeBetweenWaves} seconds...</color>");
|
||||
|
||||
|
||||
yield return new WaitForSeconds(timeBetweenWaves);
|
||||
|
||||
|
||||
StartCoroutine(SpawnWaveRoutine(waves[currentWaveIndex]));
|
||||
}
|
||||
|
||||
@@ -91,11 +93,11 @@ public class EnemySwarmManager : MonoBehaviour
|
||||
{
|
||||
// Pick a random spawn point from the array
|
||||
Transform randomSpawnPoint = spawnPoints[UnityEngine.Random.Range(0, spawnPoints.Length)];
|
||||
|
||||
|
||||
// Add a tiny random offset so they don't spawn perfectly inside each other
|
||||
Vector3 spawnPos = randomSpawnPoint.position + new Vector3(
|
||||
UnityEngine.Random.Range(-2f, 2f),
|
||||
0f,
|
||||
UnityEngine.Random.Range(-2f, 2f),
|
||||
0f,
|
||||
UnityEngine.Random.Range(-2f, 2f)
|
||||
);
|
||||
|
||||
@@ -107,35 +109,38 @@ public class EnemySwarmManager : MonoBehaviour
|
||||
}
|
||||
|
||||
isSpawning = false;
|
||||
|
||||
|
||||
// Failsafe: Check if the player somehow killed them all before the spawning even finished
|
||||
CheckWaveClear();
|
||||
}
|
||||
|
||||
public void RegisterEnemy(Transform enemyTransform)
|
||||
public void RegisterEnemy(EnemyAI enemy) // <-- Changed parameter from Transform to EnemyAI
|
||||
{
|
||||
movementJobHandle.Complete();
|
||||
transformAccessArray.Add(enemyTransform);
|
||||
|
||||
transformAccessArray.Add(enemy.transform);
|
||||
activeEnemiesList.Add(enemy); // Keep a parallel list on the main thread
|
||||
|
||||
activeEnemyCount++;
|
||||
if (UIManager.Instance != null) UIManager.Instance.UpdateEnemiesRemaining(activeEnemyCount);
|
||||
}
|
||||
|
||||
public void RemoveEnemy(Transform enemyTransform)
|
||||
public void RemoveEnemy(EnemyAI enemy) // <-- Changed parameter from Transform to EnemyAI
|
||||
{
|
||||
movementJobHandle.Complete();
|
||||
for (int i = 0; i < transformAccessArray.length; i++)
|
||||
int index = activeEnemiesList.IndexOf(enemy);
|
||||
|
||||
if (index >= 0)
|
||||
{
|
||||
if (transformAccessArray[i] == enemyTransform)
|
||||
{
|
||||
transformAccessArray.RemoveAtSwapBack(i);
|
||||
|
||||
activeEnemyCount--;
|
||||
if (UIManager.Instance != null) UIManager.Instance.UpdateEnemiesRemaining(activeEnemyCount);
|
||||
|
||||
CheckWaveClear();
|
||||
break;
|
||||
}
|
||||
// Swap-back deletion keeps arrays perfectly aligned and highly performant
|
||||
transformAccessArray.RemoveAtSwapBack(index);
|
||||
|
||||
activeEnemiesList[index] = activeEnemiesList[activeEnemiesList.Count - 1];
|
||||
activeEnemiesList.RemoveAt(activeEnemiesList.Count - 1);
|
||||
|
||||
activeEnemyCount--;
|
||||
if (UIManager.Instance != null) UIManager.Instance.UpdateEnemiesRemaining(activeEnemyCount);
|
||||
|
||||
CheckWaveClear();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -166,11 +171,19 @@ public class EnemySwarmManager : MonoBehaviour
|
||||
|
||||
// --- BURST COMPILER MOVEMENT ---
|
||||
|
||||
void Update()
|
||||
/*void Update()
|
||||
{
|
||||
if (SamplePlayerAnimationController.Instance == null || transformAccessArray.length == 0) return;
|
||||
|
||||
targetPositions[0] = SamplePlayerAnimationController.Instance.transform.position;
|
||||
// --- NEW TARGETING LOGIC ---
|
||||
// Feed every single enemy's unique target into the NativeArray before the Job runs
|
||||
for (int i = 0; i < activeEnemiesList.Count; i++)
|
||||
{
|
||||
if (activeEnemiesList[i].currentTarget != null)
|
||||
targetPositions[i] = activeEnemiesList[i].currentTarget.position;
|
||||
else
|
||||
targetPositions[i] = SamplePlayerAnimationController.Instance.transform.position; // Fallback
|
||||
}
|
||||
|
||||
SwarmJob swarmJob = new SwarmJob
|
||||
{
|
||||
@@ -180,21 +193,9 @@ public class EnemySwarmManager : MonoBehaviour
|
||||
};
|
||||
|
||||
movementJobHandle = swarmJob.Schedule(transformAccessArray);
|
||||
}
|
||||
}*/
|
||||
|
||||
void LateUpdate()
|
||||
{
|
||||
movementJobHandle.Complete();
|
||||
}
|
||||
|
||||
void OnDestroy()
|
||||
{
|
||||
movementJobHandle.Complete();
|
||||
if (transformAccessArray.isCreated) transformAccessArray.Dispose();
|
||||
if (targetPositions.IsCreated) targetPositions.Dispose();
|
||||
}
|
||||
|
||||
[BurstCompile]
|
||||
/*[BurstCompile]
|
||||
struct SwarmJob : IJobParallelForTransform
|
||||
{
|
||||
[ReadOnly] public NativeArray<float3> targetPos;
|
||||
@@ -204,7 +205,8 @@ public class EnemySwarmManager : MonoBehaviour
|
||||
public void Execute(int index, TransformAccess transform)
|
||||
{
|
||||
float3 currentPos = transform.position;
|
||||
float3 dir = targetPos[0] - currentPos;
|
||||
// The Job now looks up the SPECIFIC target for this specific index!
|
||||
float3 dir = targetPos[index] - currentPos;
|
||||
|
||||
if (math.lengthsq(dir) > 4.0f)
|
||||
{
|
||||
@@ -215,7 +217,42 @@ public class EnemySwarmManager : MonoBehaviour
|
||||
transform.rotation = targetRot;
|
||||
}
|
||||
}
|
||||
}
|
||||
}*/
|
||||
|
||||
/*void LateUpdate()
|
||||
{
|
||||
movementJobHandle.Complete();
|
||||
}*/
|
||||
|
||||
/*void OnDestroy()
|
||||
{
|
||||
movementJobHandle.Complete();
|
||||
if (transformAccessArray.isCreated) transformAccessArray.Dispose();
|
||||
if (targetPositions.IsCreated) targetPositions.Dispose();
|
||||
}*/
|
||||
|
||||
/* [BurstCompile]
|
||||
struct SwarmJob : IJobParallelForTransform
|
||||
{
|
||||
[ReadOnly] public NativeArray<float3> targetPos;
|
||||
public float speed;
|
||||
public float dt;
|
||||
|
||||
public void Execute(int index, TransformAccess transform)
|
||||
{
|
||||
float3 currentPos = transform.position;
|
||||
float3 dir = targetPos[0] - currentPos;
|
||||
|
||||
if (math.lengthsq(dir) > 4.0f)
|
||||
{
|
||||
dir = math.normalize(dir);
|
||||
transform.position = currentPos + (dir * speed * dt);
|
||||
|
||||
quaternion targetRot = quaternion.LookRotationSafe(dir, math.up());
|
||||
transform.rotation = targetRot;
|
||||
}
|
||||
}
|
||||
}*/
|
||||
public void PauseEnemyMovement(Transform enemyTransform)
|
||||
{
|
||||
movementJobHandle.Complete();
|
||||
|
||||
Reference in New Issue
Block a user