Skip to content

Commit

Permalink
Use singleton tracker, and remove Actor global extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
wallabra committed Aug 31, 2024
1 parent 5de178b commit eae8384
Showing 1 changed file with 46 additions and 9 deletions.
55 changes: 46 additions & 9 deletions wadsrc/static/zscript/actors/shared/randomspawner.zs
Original file line number Diff line number Diff line change
@@ -1,15 +1,52 @@
// Random spawner tracker singleton ----------------------------------------

// Random spawner ----------------------------------------------------------
class RandomSpawnerTracker {
static RandomSpawnerTracker instance;

protected int size;
protected Array<Class<Actor>> origin;
protected Array<Actor> dest;

static RandomSpawnerTracker Get() {
if (instance == null) {
instance = new("RandomSpawnerTracker");
instance.Init();
}
return instance;
}

virtual protected void Init() {
size = 0;
}

virtual protected Class<Actor> FindRootClass(Actor from) {
for (int i = 0; i < size; i++) {
if (dest[i] = from) {
return origin[i];
}
}
return from.class;
}

extend class Actor {
Class<Actor> originSpawner;
void RegisterSpawn(Actor from, Actor dest) {
let root = FindRootClass(from);
self.origin.Append(root);
self.dest.Append(dest);
size++;
}

/// Get class of thing that originally spawned this thing.
Class<Actor> GetOriginSpawner() {
return originSpawner != null ? originSpawner : GetClass();
Class<Actor> GetSpawnOrigin(Actor Other) {
for (int i = 0; i < size; i++) {
if (dest[i] == Other) {
return origin[i];
}
}
return null;
}
}

// Random spawner ----------------------------------------------------------

class RandomSpawner : Actor
{
const MAX_RANDOMSPAWNERS_RECURSION = 32; // Should be largely more than enough, honestly.
Expand Down Expand Up @@ -241,9 +278,6 @@ class RandomSpawner : Actor
if (newmobj is 'RandomSpawner')
newmobj.bouncecount = ++bouncecount;

// Keep track of this spawner (or a previous one) in the spawned actor.
newmobj.originSpawner = GetOriginSpawner();

// If the spawned actor has either of those flags, it's a boss.
if (newmobj.bBossDeath || newmobj.bBoss)
boss = true;
Expand All @@ -252,6 +286,9 @@ class RandomSpawner : Actor
if (rep && (rep.bBossDeath || rep.bBoss))
boss = true;

// Track random spawner activity so mods can interface with it
RandomSpawnerTracker.Get().RegisterSpawn(self, newmobj);

PostSpawn(newmobj);
}
if (boss)
Expand Down

0 comments on commit eae8384

Please sign in to comment.