74 lines
1.9 KiB
C#
74 lines
1.9 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class Wave : MonoBehaviour
|
|
{
|
|
public WaveSettings settings;
|
|
|
|
const float DISTANCE = 30f;
|
|
|
|
float ticksPerSpawn;
|
|
int spawnsPer;
|
|
int ticks;
|
|
int spawns;
|
|
|
|
void Start()
|
|
{
|
|
var dur = settings.duration;
|
|
if (dur <= Time.fixedDeltaTime)
|
|
{
|
|
dur = Time.fixedDeltaTime;
|
|
}
|
|
ticksPerSpawn = dur / settings.count / Time.fixedDeltaTime;
|
|
spawnsPer = Mathf.CeilToInt(settings.count * Time.fixedDeltaTime / dur);
|
|
ticks = spawns = 0;
|
|
}
|
|
|
|
void FixedUpdate()
|
|
{
|
|
var groups = Mathf.FloorToInt(ticks / ticksPerSpawn);
|
|
ticks++;
|
|
if (Mathf.FloorToInt(ticks / ticksPerSpawn) > groups)
|
|
{
|
|
doSpawn();
|
|
if (spawns >= settings.count)
|
|
{
|
|
Destroy(gameObject);
|
|
}
|
|
}
|
|
}
|
|
|
|
void doSpawn()
|
|
{
|
|
var n = spawnsPer;
|
|
if (n > settings.count - spawns)
|
|
{
|
|
n = settings.count - spawns;
|
|
}
|
|
for (int i = 0; i < n; i++)
|
|
{
|
|
var k = Random.Range(0, settings.enemies.Count);
|
|
var p = nextLoc(spawns);
|
|
Instantiate(settings.enemies[k], p, Quaternion.identity);
|
|
spawns++;
|
|
}
|
|
}
|
|
|
|
Vector3 nextLoc(int i)
|
|
{
|
|
switch (settings.strategy)
|
|
{
|
|
case WaveSettings.SpawnStrategy.Uniform:
|
|
var angle = (float)i / (float)settings.count * 2*Mathf.PI;
|
|
return new Vector3(Mathf.Cos(angle), Mathf.Sin(angle)) * DISTANCE;
|
|
case WaveSettings.SpawnStrategy.Random:
|
|
return Random.insideUnitCircle.normalized * DISTANCE;
|
|
case WaveSettings.SpawnStrategy.DiskRandom:
|
|
return Random.insideUnitCircle * DISTANCE;
|
|
default:
|
|
throw new System.Exception("unknown strategy");
|
|
}
|
|
}
|
|
}
|