diff --git a/OpenRA.Mods.Common/Traits/Player/ProvidesPrerequisite.cs b/OpenRA.Mods.Common/Traits/Player/ProvidesPrerequisite.cs index b25c07285c60..5cce4c346d41 100644 --- a/OpenRA.Mods.Common/Traits/Player/ProvidesPrerequisite.cs +++ b/OpenRA.Mods.Common/Traits/Player/ProvidesPrerequisite.cs @@ -15,7 +15,7 @@ namespace OpenRA.Mods.Common.Traits { - public class ProvidesPrerequisiteInfo : ITechTreePrerequisiteInfo + public class ProvidesPrerequisiteInfo : ConditionalTraitInfo, ITechTreePrerequisiteInfo { [Desc("The prerequisite type that this provides. If left empty it defaults to the actor's name.")] public readonly string Prerequisite = null; @@ -28,27 +28,26 @@ public class ProvidesPrerequisiteInfo : ITechTreePrerequisiteInfo [Desc("Should it recheck everything when it is captured?")] public readonly bool ResetOnOwnerChange = false; - public object Create(ActorInitializer init) { return new ProvidesPrerequisite(init, this); } + public override object Create(ActorInitializer init) { return new ProvidesPrerequisite(init, this); } } - public class ProvidesPrerequisite : ITechTreePrerequisite, INotifyOwnerChanged + public class ProvidesPrerequisite : ConditionalTrait, ITechTreePrerequisite, INotifyOwnerChanged, INotifyCreated { - readonly ProvidesPrerequisiteInfo info; readonly string prerequisite; - bool enabled = true; + bool enabled; + TechTree techTree; + string faction; public ProvidesPrerequisite(ActorInitializer init, ProvidesPrerequisiteInfo info) + : base(info) { - this.info = info; prerequisite = info.Prerequisite; if (string.IsNullOrEmpty(prerequisite)) prerequisite = init.Self.Info.Name; - var faction = init.Contains() ? init.Get() : init.Self.Owner.Faction.InternalName; - - Update(init.Self.Owner, faction); + faction = init.Contains() ? init.Get() : init.Self.Owner.Faction.InternalName; } public IEnumerable ProvidesPrerequisites @@ -62,21 +61,48 @@ public IEnumerable ProvidesPrerequisites } } + protected override void Created(Actor self) + { + techTree = self.Owner.PlayerActor.Trait(); + + Update(); + + base.Created(self); + } + public void OnOwnerChanged(Actor self, Player oldOwner, Player newOwner) { - if (info.ResetOnOwnerChange) - Update(newOwner, newOwner.Faction.InternalName); + techTree = newOwner.PlayerActor.Trait(); + + if (Info.ResetOnOwnerChange) + faction = newOwner.Faction.InternalName; + + Update(); } - void Update(Player owner, string faction) + void Update() { - enabled = true; + enabled = !IsTraitDisabled; + if (IsTraitDisabled) + return; + + if (Info.Factions.Any()) + enabled = Info.Factions.Contains(faction); - if (info.Factions.Any()) - enabled = info.Factions.Contains(faction); + if (Info.RequiresPrerequisites.Any() && enabled) + enabled = techTree.HasPrerequisites(Info.RequiresPrerequisites); + } - if (info.RequiresPrerequisites.Any() && enabled) - enabled = owner.PlayerActor.Trait().HasPrerequisites(info.RequiresPrerequisites); + protected override void TraitEnabled(Actor self) + { + Update(); + techTree.ActorChanged(self); + } + + protected override void TraitDisabled(Actor self) + { + Update(); + techTree.ActorChanged(self); } } }