Initial pass on wave spawning.
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
This commit is contained in:
9
Assets/Scripts/Wave/SpawnDistribution.cs
Normal file
9
Assets/Scripts/Wave/SpawnDistribution.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace Wave
|
||||
{
|
||||
public enum SpawnDistribution
|
||||
{
|
||||
Uniform,
|
||||
Random,
|
||||
DiskRandom
|
||||
}
|
||||
}
|
3
Assets/Scripts/Wave/SpawnDistribution.cs.meta
generated
Normal file
3
Assets/Scripts/Wave/SpawnDistribution.cs.meta
generated
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2a4df8269a27463eaf87da9ae1283dcb
|
||||
timeCreated: 1690925288
|
31
Assets/Scripts/Wave/SpawnPackSO.cs
Normal file
31
Assets/Scripts/Wave/SpawnPackSO.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using System.Collections.Generic;
|
||||
using DefaultNamespace;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Assertions;
|
||||
|
||||
namespace Wave
|
||||
{
|
||||
[CreateAssetMenu(fileName = "SpawnPack", menuName = "Spawn/Pack", order = 0)]
|
||||
public class SpawnPackSO : ScriptableObject
|
||||
{
|
||||
[SerializeField] private List<Enemy> possibleEnemies;
|
||||
[SerializeField] [Min(0)] private int minCount;
|
||||
[SerializeField] [Min(0)] private int maxCount;
|
||||
|
||||
public IReadOnlyCollection<Enemy> EnemiesToSpawn()
|
||||
{
|
||||
Assert.IsTrue(possibleEnemies.Count > 0);
|
||||
|
||||
var count = Random.Range(minCount, maxCount);
|
||||
var enemiesToSpawn = new List<Enemy>();
|
||||
|
||||
for (var i = 0; i < count; i++)
|
||||
{
|
||||
var enemyIndex = Random.Range(0, possibleEnemies.Count);
|
||||
enemiesToSpawn.Add(possibleEnemies[enemyIndex]);
|
||||
}
|
||||
|
||||
return enemiesToSpawn;
|
||||
}
|
||||
}
|
||||
}
|
3
Assets/Scripts/Wave/SpawnPackSO.cs.meta
generated
Normal file
3
Assets/Scripts/Wave/SpawnPackSO.cs.meta
generated
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 52673fed940f4a67b5a08641a55feff6
|
||||
timeCreated: 1690938542
|
27
Assets/Scripts/Wave/SpawnWaveSO.cs
Normal file
27
Assets/Scripts/Wave/SpawnWaveSO.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Serialization;
|
||||
|
||||
namespace Wave
|
||||
{
|
||||
[CreateAssetMenu(fileName = "SpawnWave", menuName = "Spawn Wave", order = 0)]
|
||||
public class SpawnWaveSO : ScriptableObject
|
||||
{
|
||||
[SerializeField] private List<SpawnPackSO> packs;
|
||||
[SerializeField] private SpawnDistribution distribution;
|
||||
[SerializeField] [Min(10)] private float radius;
|
||||
[SerializeField] [Min(0)] private float packRadius;
|
||||
[SerializeField] [Min(0)] private float timeBetweenSpawns;
|
||||
[SerializeField] [Min(0)] private float timeBetweenPacks;
|
||||
[SerializeField] [Min(0)] private float timeToComplete;
|
||||
|
||||
|
||||
public IReadOnlyCollection<SpawnPackSO> Packs => packs;
|
||||
public SpawnDistribution Distribution => distribution;
|
||||
public float Radius => radius;
|
||||
public float PackRadius => packRadius;
|
||||
public float TimeBetweenSpawns => timeBetweenSpawns;
|
||||
public float TimeBetweenPacks => timeBetweenPacks;
|
||||
public float TimeToComplete => timeToComplete;
|
||||
}
|
||||
}
|
3
Assets/Scripts/Wave/SpawnWaveSO.cs.meta
generated
Normal file
3
Assets/Scripts/Wave/SpawnWaveSO.cs.meta
generated
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 17a69c35e3e64b6d9f655533478f9772
|
||||
timeCreated: 1690856578
|
129
Assets/Scripts/Wave/WaveSpawner.cs
Normal file
129
Assets/Scripts/Wave/WaveSpawner.cs
Normal file
@@ -0,0 +1,129 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using DefaultNamespace;
|
||||
using Events;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Assertions;
|
||||
using Random = UnityEngine.Random;
|
||||
|
||||
namespace Wave
|
||||
{
|
||||
public class WaveSpawner : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private SpawnWaveSOEventChannelSO startNewSpawnWaveEventChannel;
|
||||
[SerializeField] private SpawnWaveSOEventChannelSO endSpawnWaveEventChannel;
|
||||
|
||||
[SerializeField] private EnemyRuntimeSetSO enemyRuntimeSet;
|
||||
[SerializeField] private Transform spawnCenter;
|
||||
[SerializeField] private float minimumSpawnRadius;
|
||||
[SerializeField] [Min(0)] private float timeBetweenClearedWaves;
|
||||
[SerializeField] [Min(0)] private float timeBeforeFirstWave;
|
||||
|
||||
private float _timeSinceLastSpawnWaveStarted;
|
||||
private Coroutine _handleSpawnWaves;
|
||||
|
||||
private bool HasWaveBeenCleared => enemyRuntimeSet.IsEmpty;
|
||||
|
||||
public void Begin(IEnumerable<SpawnWaveSO> spawnWaves)
|
||||
{
|
||||
Assert.IsNull(_handleSpawnWaves);
|
||||
_handleSpawnWaves = StartCoroutine(CO_HandleSpawnWaves(spawnWaves));
|
||||
}
|
||||
|
||||
public void End()
|
||||
{
|
||||
StopCoroutine(_handleSpawnWaves);
|
||||
}
|
||||
|
||||
private IEnumerator CO_HandleSpawnWaves(IEnumerable<SpawnWaveSO> spawnWaves)
|
||||
{
|
||||
yield return new WaitForSeconds(timeBeforeFirstWave);
|
||||
|
||||
foreach (var spawnWave in spawnWaves)
|
||||
{
|
||||
startNewSpawnWaveEventChannel.RaiseEvent(spawnWave);
|
||||
yield return SpawnWave(spawnWave);
|
||||
_timeSinceLastSpawnWaveStarted = 0.0f;
|
||||
|
||||
yield return new WaitUntil(() =>
|
||||
_timeSinceLastSpawnWaveStarted >= spawnWave.TimeToComplete
|
||||
|| HasWaveBeenCleared);
|
||||
|
||||
endSpawnWaveEventChannel.RaiseEvent(spawnWave);
|
||||
yield return new WaitForSeconds(timeBetweenClearedWaves);
|
||||
}
|
||||
}
|
||||
|
||||
private IEnumerator SpawnWave(SpawnWaveSO spawnWave)
|
||||
{
|
||||
var enemyPacksToSpawn = spawnWave.Packs.Select(pack => pack.EnemiesToSpawn()).ToList().AsReadOnly();
|
||||
var totalPacksToSpawn = enemyPacksToSpawn.Count;
|
||||
|
||||
|
||||
var numberPacksSpawned = 0;
|
||||
foreach (var enemyPackToSpawn in enemyPacksToSpawn)
|
||||
{
|
||||
numberPacksSpawned++;
|
||||
var waveCompletionPercentage = (float) numberPacksSpawned / (float) totalPacksToSpawn;
|
||||
|
||||
// TODO (Michael): Both the pack and enemy position samplers need to be a bit smarter. I think in most scenarios the pack circle just needs to not overlap with other pack circles.
|
||||
// The enemy spawns probably need something like a poission disk sampler so that monsters dont overlap. The enemy spawn radius at the pack's position should also probably scale with the size of the pack.
|
||||
var enemyPackSpawnCenter = CalculatePackSpawnPosition(waveCompletionPercentage, spawnWave);
|
||||
|
||||
foreach (var enemy in enemyPackToSpawn)
|
||||
{
|
||||
|
||||
var spawnLocation = CalculateEnemySpawnPosition(enemyPackSpawnCenter, spawnWave.PackRadius);
|
||||
var spawnedEnemy = Instantiate(enemy, spawnLocation, Quaternion.identity);
|
||||
enemyRuntimeSet.Add(spawnedEnemy);
|
||||
|
||||
if (spawnWave.TimeBetweenSpawns > 0.0f)
|
||||
{
|
||||
yield return new WaitForSeconds(spawnWave.TimeBetweenSpawns);
|
||||
}
|
||||
}
|
||||
|
||||
if (spawnWave.TimeBetweenPacks > 0.0f)
|
||||
{
|
||||
yield return new WaitForSeconds(spawnWave.TimeBetweenPacks);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Vector3 CalculatePackSpawnPosition(float waveCompletionPercentage, SpawnWaveSO spawnWave)
|
||||
{
|
||||
var angle = waveCompletionPercentage * 2 * MathF.PI;
|
||||
var unitCirclePosition = Random.insideUnitCircle;
|
||||
var swizzledUnitCirclePosition = new Vector3(unitCirclePosition.x, 0, unitCirclePosition.y);
|
||||
|
||||
var spawnOffset = spawnWave.Distribution switch
|
||||
{
|
||||
SpawnDistribution.Uniform => new Vector3(Mathf.Cos(angle), 0, Mathf.Sin(angle)) * spawnWave.Radius,
|
||||
SpawnDistribution.Random => swizzledUnitCirclePosition.normalized * spawnWave.Radius,
|
||||
SpawnDistribution.DiskRandom => swizzledUnitCirclePosition * spawnWave.Radius,
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(spawnWave.Distribution), spawnWave.Distribution, null)
|
||||
};
|
||||
|
||||
spawnOffset += spawnOffset.normalized * (minimumSpawnRadius + spawnWave.PackRadius);
|
||||
|
||||
return spawnCenter.position + spawnOffset;
|
||||
}
|
||||
|
||||
private Vector3 CalculateEnemySpawnPosition(Vector3 center, float radius)
|
||||
{
|
||||
var unitCirclePosition = Random.insideUnitCircle;
|
||||
var swizzledUnitCirclePosition = new Vector3(unitCirclePosition.x, 0, unitCirclePosition.y);
|
||||
|
||||
var spawnOffset = swizzledUnitCirclePosition * radius;
|
||||
|
||||
return center + spawnOffset;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
_timeSinceLastSpawnWaveStarted += Time.deltaTime;
|
||||
}
|
||||
}
|
||||
}
|
3
Assets/Scripts/Wave/WaveSpawner.cs.meta
generated
Normal file
3
Assets/Scripts/Wave/WaveSpawner.cs.meta
generated
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bb718e52728142e4a8db081fd6a36a70
|
||||
timeCreated: 1690856916
|
Reference in New Issue
Block a user