Don't spawn new actors before all RemovedFromWorld callbacks have run

This commit is contained in:
abcdefg30
2017-08-08 16:05:53 +02:00
committed by reaperrr
parent 22d7031819
commit d1ab421240

View File

@@ -53,11 +53,13 @@ namespace OpenRA.Mods.Common.Traits
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)
{
@@ -65,7 +67,7 @@ namespace OpenRA.Mods.Common.Traits
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;
@@ -79,10 +81,13 @@ namespace OpenRA.Mods.Common.Traits
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)
{
// Actor has been disposed by something else before its death (for example `Enter`).
if (self.Disposed)
if (attackingPlayer == null)
return;
var td = new TypeDictionary
@@ -96,7 +101,7 @@ namespace OpenRA.Mods.Common.Traits
if (Info.OwnerType == OwnerType.Victim)
td.Add(new OwnerInit(self.Owner));
else if (Info.OwnerType == OwnerType.Killer)
td.Add(new OwnerInit(e.Attacker.Owner));
td.Add(new OwnerInit(attackingPlayer));
else
td.Add(new OwnerInit(self.World.Players.First(p => p.InternalName == Info.InternalOwner)));
@@ -110,8 +115,7 @@ namespace OpenRA.Mods.Common.Traits
.Select(ihm => ihm.HuskActor(self))
.FirstOrDefault(a => a != null);
w.CreateActor(huskActor ?? Info.Actor, td);
});
self.World.AddFrameEndTask(w => w.CreateActor(huskActor ?? Info.Actor, td));
}
}
}