Add Delay field to KillsSelf

This commit is contained in:
reaperrr
2017-01-22 10:59:53 +01:00
parent e23a866979
commit 977cc0e367

View File

@@ -18,13 +18,21 @@ namespace OpenRA.Mods.Common.Traits
[Desc("Remove the actor from the world (and destroy it) instead of killing it.")] [Desc("Remove the actor from the world (and destroy it) instead of killing it.")]
public readonly bool RemoveInstead = false; public readonly bool RemoveInstead = false;
public override object Create(ActorInitializer init) { return new KillsSelf(this); } [Desc("The amount of time (in ticks) before the actor dies. Two values indicate a range between which a random value is chosen.")]
public readonly int[] Delay = { 250 };
public override object Create(ActorInitializer init) { return new KillsSelf(init.Self, this); }
} }
class KillsSelf : ConditionalTrait<KillsSelfInfo>, INotifyAddedToWorld class KillsSelf : ConditionalTrait<KillsSelfInfo>, INotifyAddedToWorld, ITick
{ {
public KillsSelf(KillsSelfInfo info) int lifetime;
: base(info) { }
public KillsSelf(Actor self, KillsSelfInfo info)
: base(info)
{
lifetime = Util.RandomDelay(self.World, info.Delay);
}
public void AddedToWorld(Actor self) public void AddedToWorld(Actor self)
{ {
@@ -37,10 +45,30 @@ namespace OpenRA.Mods.Common.Traits
if (self.IsDead) if (self.IsDead)
return; return;
if (lifetime > 0)
return;
if (Info.RemoveInstead || !self.Info.HasTraitInfo<HealthInfo>()) if (Info.RemoveInstead || !self.Info.HasTraitInfo<HealthInfo>())
self.Dispose(); self.Dispose();
else else
self.Kill(self); self.Kill(self);
} }
void ITick.Tick(Actor self)
{
if (self.IsDead || IsTraitDisabled)
return;
if (!self.World.Map.Contains(self.Location))
return;
if (lifetime-- == 0)
{
if (Info.RemoveInstead || !self.Info.HasTraitInfo<HealthInfo>())
self.Dispose();
else
self.Kill(self);
}
}
} }
} }