diff --git a/OpenRA.Mods.Common/Traits/KillsSelf.cs b/OpenRA.Mods.Common/Traits/KillsSelf.cs index 8cf55e8489..7714115366 100644 --- a/OpenRA.Mods.Common/Traits/KillsSelf.cs +++ b/OpenRA.Mods.Common/Traits/KillsSelf.cs @@ -18,13 +18,21 @@ namespace OpenRA.Mods.Common.Traits [Desc("Remove the actor from the world (and destroy it) instead of killing it.")] 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, INotifyAddedToWorld + class KillsSelf : ConditionalTrait, INotifyAddedToWorld, ITick { - public KillsSelf(KillsSelfInfo info) - : base(info) { } + int lifetime; + + public KillsSelf(Actor self, KillsSelfInfo info) + : base(info) + { + lifetime = Util.RandomDelay(self.World, info.Delay); + } public void AddedToWorld(Actor self) { @@ -37,10 +45,30 @@ namespace OpenRA.Mods.Common.Traits if (self.IsDead) return; + if (lifetime > 0) + return; + if (Info.RemoveInstead || !self.Info.HasTraitInfo()) self.Dispose(); else 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()) + self.Dispose(); + else + self.Kill(self); + } + } } }