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 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 string faction;
readonly bool enabled; readonly bool enabled;
Player attackingPlayer;
public SpawnActorOnDeath(ActorInitializer init, SpawnActorOnDeathInfo info) public SpawnActorOnDeath(ActorInitializer init, SpawnActorOnDeathInfo info)
: base(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; 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) if (!enabled || IsTraitDisabled)
return; return;
@@ -79,39 +81,41 @@ namespace OpenRA.Mods.Common.Traits
if (Info.DeathType != null && !e.Damage.DamageTypes.Contains(Info.DeathType)) if (Info.DeathType != null && !e.Damage.DamageTypes.Contains(Info.DeathType))
return; 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`). new ParentActorInit(self),
if (self.Disposed) new LocationInit(self.Location + Info.Offset),
return; new CenterPositionInit(self.CenterPosition),
new FactionInit(faction)
};
var td = new TypeDictionary if (Info.OwnerType == OwnerType.Victim)
{ td.Add(new OwnerInit(self.Owner));
new ParentActorInit(self), else if (Info.OwnerType == OwnerType.Killer)
new LocationInit(self.Location + Info.Offset), td.Add(new OwnerInit(attackingPlayer));
new CenterPositionInit(self.CenterPosition), else
new FactionInit(faction) td.Add(new OwnerInit(self.World.Players.First(p => p.InternalName == Info.InternalOwner)));
};
if (Info.OwnerType == OwnerType.Victim) if (Info.SkipMakeAnimations)
td.Add(new OwnerInit(self.Owner)); td.Add(new SkipMakeAnimsInit());
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) foreach (var modifier in self.TraitsImplementing<IDeathActorInitModifier>())
td.Add(new SkipMakeAnimsInit()); modifier.ModifyDeathActorInit(self, td);
foreach (var modifier in self.TraitsImplementing<IDeathActorInitModifier>()) var huskActor = self.TraitsImplementing<IHuskModifier>()
modifier.ModifyDeathActorInit(self, td); .Select(ihm => ihm.HuskActor(self))
.FirstOrDefault(a => a != null);
var huskActor = self.TraitsImplementing<IHuskModifier>() self.World.AddFrameEndTask(w => w.CreateActor(huskActor ?? Info.Actor, td));
.Select(ihm => ihm.HuskActor(self))
.FirstOrDefault(a => a != null);
w.CreateActor(huskActor ?? Info.Actor, td);
});
} }
} }
} }