Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RequiresCondition to ProvidesPrerequisite #13842

Merged
merged 1 commit into from Sep 24, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
60 changes: 43 additions & 17 deletions OpenRA.Mods.Common/Traits/Player/ProvidesPrerequisite.cs
Expand Up @@ -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;
Expand All @@ -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<ProvidesPrerequisiteInfo>, 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<FactionInit>() ? init.Get<FactionInit, string>() : init.Self.Owner.Faction.InternalName;

Update(init.Self.Owner, faction);
faction = init.Contains<FactionInit>() ? init.Get<FactionInit, string>() : init.Self.Owner.Faction.InternalName;
}

public IEnumerable<string> ProvidesPrerequisites
Expand All @@ -62,21 +61,48 @@ public IEnumerable<string> ProvidesPrerequisites
}
}

protected override void Created(Actor self)
{
techTree = self.Owner.PlayerActor.Trait<TechTree>();

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<TechTree>();

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<TechTree>().HasPrerequisites(info.RequiresPrerequisites);
protected override void TraitEnabled(Actor self)
{
Update();
techTree.ActorChanged(self);
}

protected override void TraitDisabled(Actor self)
{
Update();
techTree.ActorChanged(self);
}
}
}