Make SpawnActorOnDeath conditional

This commit is contained in:
Jean-Rémy Buchs
2017-07-01 20:50:18 +02:00
committed by atlimit8
parent 9e138178ad
commit 2e4cd8d820

View File

@@ -19,7 +19,7 @@ namespace OpenRA.Mods.Common.Traits
public enum OwnerType { Victim, Killer, InternalName } public enum OwnerType { Victim, Killer, InternalName }
[Desc("Spawn another actor immediately upon death.")] [Desc("Spawn another actor immediately upon death.")]
public class SpawnActorOnDeathInfo : ITraitInfo public class SpawnActorOnDeathInfo : ConditionalTraitInfo
{ {
[ActorReference, FieldLoader.Require] [ActorReference, FieldLoader.Require]
[Desc("Actor to spawn on death.")] [Desc("Actor to spawn on death.")]
@@ -50,34 +50,33 @@ namespace OpenRA.Mods.Common.Traits
"lead to unexpected behaviour.")] "lead to unexpected behaviour.")]
public readonly CVec Offset = CVec.Zero; public readonly CVec Offset = CVec.Zero;
public object Create(ActorInitializer init) { return new SpawnActorOnDeath(init, this); } public override object Create(ActorInitializer init) { return new SpawnActorOnDeath(init, this); }
} }
public class SpawnActorOnDeath : INotifyKilled public class SpawnActorOnDeath : ConditionalTrait<SpawnActorOnDeathInfo>, INotifyKilled
{ {
readonly SpawnActorOnDeathInfo info;
readonly string faction; readonly string faction;
readonly bool enabled; readonly bool enabled;
public SpawnActorOnDeath(ActorInitializer init, SpawnActorOnDeathInfo info) public SpawnActorOnDeath(ActorInitializer init, SpawnActorOnDeathInfo info)
: base(info)
{ {
this.info = info;
enabled = !info.RequiresLobbyCreeps || init.Self.World.WorldActor.Trait<MapCreeps>().Enabled; enabled = !info.RequiresLobbyCreeps || init.Self.World.WorldActor.Trait<MapCreeps>().Enabled;
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) public void Killed(Actor self, AttackInfo e)
{ {
if (!enabled) if (!enabled || IsTraitDisabled)
return; return;
if (!self.IsInWorld) if (!self.IsInWorld)
return; return;
if (self.World.SharedRandom.Next(100) > info.Probability) if (self.World.SharedRandom.Next(100) > Info.Probability)
return; return;
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 => self.World.AddFrameEndTask(w =>
@@ -89,19 +88,19 @@ namespace OpenRA.Mods.Common.Traits
var td = new TypeDictionary var td = new TypeDictionary
{ {
new ParentActorInit(self), new ParentActorInit(self),
new LocationInit(self.Location + info.Offset), new LocationInit(self.Location + Info.Offset),
new CenterPositionInit(self.CenterPosition), new CenterPositionInit(self.CenterPosition),
new FactionInit(faction) new FactionInit(faction)
}; };
if (info.OwnerType == OwnerType.Victim) if (Info.OwnerType == OwnerType.Victim)
td.Add(new OwnerInit(self.Owner)); td.Add(new OwnerInit(self.Owner));
else if (info.OwnerType == OwnerType.Killer) else if (Info.OwnerType == OwnerType.Killer)
td.Add(new OwnerInit(e.Attacker.Owner)); td.Add(new OwnerInit(e.Attacker.Owner));
else else
td.Add(new OwnerInit(self.World.Players.First(p => p.InternalName == info.InternalOwner))); td.Add(new OwnerInit(self.World.Players.First(p => p.InternalName == Info.InternalOwner)));
if (info.SkipMakeAnimations) if (Info.SkipMakeAnimations)
td.Add(new SkipMakeAnimsInit()); td.Add(new SkipMakeAnimsInit());
foreach (var modifier in self.TraitsImplementing<IDeathActorInitModifier>()) foreach (var modifier in self.TraitsImplementing<IDeathActorInitModifier>())
@@ -111,7 +110,7 @@ namespace OpenRA.Mods.Common.Traits
.Select(ihm => ihm.HuskActor(self)) .Select(ihm => ihm.HuskActor(self))
.FirstOrDefault(a => a != null); .FirstOrDefault(a => a != null);
w.CreateActor(huskActor ?? info.Actor, td); w.CreateActor(huskActor ?? Info.Actor, td);
}); });
} }
} }