-
Notifications
You must be signed in to change notification settings - Fork 1
/
EnemySpawnerSystem.cs
72 lines (59 loc) · 2.64 KB
/
EnemySpawnerSystem.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using System.Numerics;
using Walgelijk;
namespace MadnessMicroactive;
public class EnemySpawnerSystem : Walgelijk.System
{
private readonly FixedIntervalDistributor distributor = new(1);
public override void FixedUpdate()
{
var enemyFaction = Faction.AAHW;
var currentCount = Scene.GetAllComponentsOfType<CharacterComponent>().Count(c => c.IsAlive && c.Faction == enemyFaction);
var enemiesRemaining = 1;
var maxSimultaneousEnemies = 1;
var spawnXs = new Vector2(-600, 600);
if (Scene.FindAnyComponent<LevelComponent>(out var level))
{
enemiesRemaining = level.TotalEnemies;
maxSimultaneousEnemies = level.MaxEnemies;
spawnXs = level.WalkingRange;
if (Scene.FindAnyComponent<LevelProgressComponent>(out var progress))
enemiesRemaining = level.TotalEnemies - progress.Kills;
}
for (int i = 0; i < distributor.CalculateCycleCount(Time.FixedInterval); i++)
if (currentCount < maxSimultaneousEnemies && enemiesRemaining > 0)
{
var preset = new EnemyPreset(Outfits.Grunt, CharacterStats.Grunt);
var x = Utilities.PickRandom(spawnXs.X, spawnXs.Y);
if (Scene.FindAnyComponent(out level) && level.Enemies != null && level.Enemies.Length > 0)
preset = Utilities.PickRandom(level.Enemies);
SpawnEnemy(Scene, enemyFaction, level, preset, x);
currentCount++;
enemiesRemaining--;
}
}
public static CharacterComponent SpawnEnemy(Scene scene, Faction enemyFaction, LevelComponent? level, EnemyPreset preset, float x)
{
var e = Prefabs.CreateCharacter(
scene,
Utilities.RandomInt(0, 100),
new Vector2(x, LevelComponent.FloorLevel));
e.Faction = enemyFaction;
e.Stats = preset.Stats;
e.IncrementsKillsOnDeath = true;
e.SetOutfit(scene, preset.Outfit);
if (Utilities.RandomFloat() > 0.5f)
{
IWeapon weaponToSpawnWith = Utilities.PickRandom(Weapons.AllWeapons);
if (level != null)
weaponToSpawnWith = Utilities.PickRandom(level.Weapons);
if (weaponToSpawnWith is Firearm f)
{
var wpn = Prefabs.CreateWeapon(scene, f, e.BottomCenter);
e.Equip(scene, scene.GetComponentFrom<EquippableComponent>(wpn.Entity));
}//else TODO
}
scene.AttachComponent(e.Entity, new AiControllerComponent());
scene.AttachComponent(e.Entity, new DespawnWhenDeadComponent());
return e;
}
}