Skip to content

Commit

Permalink
Don't spawn new actors before all RemovedFromWorld callbacks have run
Browse files Browse the repository at this point in the history
  • Loading branch information
abcdefg30 authored and reaperrr committed Aug 10, 2017
1 parent 22d7031 commit d1ab421
Showing 1 changed file with 38 additions and 34 deletions.
72 changes: 38 additions & 34 deletions OpenRA.Mods.Common/Traits/SpawnActorOnDeath.cs
Expand Up @@ -53,19 +53,21 @@ public class SpawnActorOnDeathInfo : ConditionalTraitInfo
public override object Create(ActorInitializer init) { return new SpawnActorOnDeath(init, this); }
}

public class SpawnActorOnDeath : ConditionalTrait<SpawnActorOnDeathInfo>, INotifyKilled
public class SpawnActorOnDeath : ConditionalTrait<SpawnActorOnDeathInfo>, INotifyKilled, INotifyRemovedFromWorld
{
readonly string faction;
readonly bool enabled;

Player attackingPlayer;

public SpawnActorOnDeath(ActorInitializer init, SpawnActorOnDeathInfo info)
: base(info)
{
enabled = !info.RequiresLobbyCreeps || init.Self.World.WorldActor.Trait<MapCreeps>().Enabled;
faction = init.Contains<FactionInit>() ? init.Get<FactionInit, string>() : init.Self.Owner.Faction.InternalName;
}

public void Killed(Actor self, AttackInfo e)
void INotifyKilled.Killed(Actor self, AttackInfo e)
{
if (!enabled || IsTraitDisabled)
return;
Expand All @@ -79,39 +81,41 @@ public void Killed(Actor self, AttackInfo e)
if (Info.DeathType != null && !e.Damage.DamageTypes.Contains(Info.DeathType))
return;

self.World.AddFrameEndTask(w =>
attackingPlayer = e.Attacker.Owner;
}

// Don't add the new actor to the world before all RemovedFromWorld callbacks have run
void INotifyRemovedFromWorld.RemovedFromWorld(Actor self)
{
if (attackingPlayer == null)
return;

var td = new TypeDictionary
{
// Actor has been disposed by something else before its death (for example `Enter`).
if (self.Disposed)
return;
var td = new TypeDictionary
{
new ParentActorInit(self),
new LocationInit(self.Location + Info.Offset),
new CenterPositionInit(self.CenterPosition),
new FactionInit(faction)
};
if (Info.OwnerType == OwnerType.Victim)
td.Add(new OwnerInit(self.Owner));
else if (Info.OwnerType == OwnerType.Killer)
td.Add(new OwnerInit(e.Attacker.Owner));
else
td.Add(new OwnerInit(self.World.Players.First(p => p.InternalName == Info.InternalOwner)));
if (Info.SkipMakeAnimations)
td.Add(new SkipMakeAnimsInit());
foreach (var modifier in self.TraitsImplementing<IDeathActorInitModifier>())
modifier.ModifyDeathActorInit(self, td);
var huskActor = self.TraitsImplementing<IHuskModifier>()
.Select(ihm => ihm.HuskActor(self))
.FirstOrDefault(a => a != null);
w.CreateActor(huskActor ?? Info.Actor, td);
});
new ParentActorInit(self),
new LocationInit(self.Location + Info.Offset),
new CenterPositionInit(self.CenterPosition),
new FactionInit(faction)
};

if (Info.OwnerType == OwnerType.Victim)
td.Add(new OwnerInit(self.Owner));
else if (Info.OwnerType == OwnerType.Killer)
td.Add(new OwnerInit(attackingPlayer));
else
td.Add(new OwnerInit(self.World.Players.First(p => p.InternalName == Info.InternalOwner)));

if (Info.SkipMakeAnimations)
td.Add(new SkipMakeAnimsInit());

foreach (var modifier in self.TraitsImplementing<IDeathActorInitModifier>())
modifier.ModifyDeathActorInit(self, td);

var huskActor = self.TraitsImplementing<IHuskModifier>()
.Select(ihm => ihm.HuskActor(self))
.FirstOrDefault(a => a != null);

self.World.AddFrameEndTask(w => w.CreateActor(huskActor ?? Info.Actor, td));
}
}
}

0 comments on commit d1ab421

Please sign in to comment.