Skip to content

Commit

Permalink
Make SpawnActorOnDeath conditional
Browse files Browse the repository at this point in the history
  • Loading branch information
jrb0001 authored and atlimit8 committed Jul 2, 2017
1 parent 9e13817 commit 2e4cd8d
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions OpenRA.Mods.Common/Traits/SpawnActorOnDeath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace OpenRA.Mods.Common.Traits
public enum OwnerType { Victim, Killer, InternalName }

[Desc("Spawn another actor immediately upon death.")]
public class SpawnActorOnDeathInfo : ITraitInfo
public class SpawnActorOnDeathInfo : ConditionalTraitInfo
{
[ActorReference, FieldLoader.Require]
[Desc("Actor to spawn on death.")]
Expand Down Expand Up @@ -50,34 +50,33 @@ public class SpawnActorOnDeathInfo : ITraitInfo
"lead to unexpected behaviour.")]
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 bool enabled;

public SpawnActorOnDeath(ActorInitializer init, SpawnActorOnDeathInfo info)
: base(info)
{
this.info = 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)
{
if (!enabled)
if (!enabled || IsTraitDisabled)
return;

if (!self.IsInWorld)
return;

if (self.World.SharedRandom.Next(100) > info.Probability)
if (self.World.SharedRandom.Next(100) > Info.Probability)
return;

if (info.DeathType != null && !e.Damage.DamageTypes.Contains(info.DeathType))
if (Info.DeathType != null && !e.Damage.DamageTypes.Contains(Info.DeathType))
return;

self.World.AddFrameEndTask(w =>
Expand All @@ -89,19 +88,19 @@ public void Killed(Actor self, AttackInfo e)
var td = new TypeDictionary
{
new ParentActorInit(self),
new LocationInit(self.Location + info.Offset),
new LocationInit(self.Location + Info.Offset),
new CenterPositionInit(self.CenterPosition),
new FactionInit(faction)
};
if (info.OwnerType == OwnerType.Victim)
if (Info.OwnerType == OwnerType.Victim)
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));
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());
foreach (var modifier in self.TraitsImplementing<IDeathActorInitModifier>())
Expand All @@ -111,7 +110,7 @@ public void Killed(Actor self, AttackInfo e)
.Select(ihm => ihm.HuskActor(self))
.FirstOrDefault(a => a != null);
w.CreateActor(huskActor ?? info.Actor, td);
w.CreateActor(huskActor ?? Info.Actor, td);
});
}
}
Expand Down

0 comments on commit 2e4cd8d

Please sign in to comment.