Convert PrimaryBuilding to conditions.
This commit is contained in:
@@ -26,10 +26,11 @@ namespace OpenRA.Mods.Common.Traits
|
||||
}
|
||||
|
||||
[Desc("Used together with ClassicProductionQueue.")]
|
||||
public class PrimaryBuildingInfo : ITraitInfo, Requires<UpgradeManagerInfo>
|
||||
public class PrimaryBuildingInfo : ITraitInfo
|
||||
{
|
||||
[UpgradeGrantedReference, Desc("The upgrades to grant while the primary building.")]
|
||||
public readonly string[] Upgrades = { "primary" };
|
||||
[UpgradeGrantedReference]
|
||||
[Desc("The condition to grant to self while this is the primary building.")]
|
||||
public readonly string PrimaryCondition = null;
|
||||
|
||||
[Desc("The speech notification to play when selecting a primary building.")]
|
||||
public readonly string SelectionNotification = "PrimaryBuildingSelected";
|
||||
@@ -37,25 +38,30 @@ namespace OpenRA.Mods.Common.Traits
|
||||
public object Create(ActorInitializer init) { return new PrimaryBuilding(init.Self, this); }
|
||||
}
|
||||
|
||||
public class PrimaryBuilding : IIssueOrder, IResolveOrder
|
||||
public class PrimaryBuilding : INotifyCreated, IIssueOrder, IResolveOrder
|
||||
{
|
||||
readonly PrimaryBuildingInfo info;
|
||||
readonly UpgradeManager manager;
|
||||
UpgradeManager um;
|
||||
int primaryToken = UpgradeManager.InvalidConditionToken;
|
||||
|
||||
public bool IsPrimary { get; private set; }
|
||||
|
||||
public PrimaryBuilding(Actor self, PrimaryBuildingInfo info)
|
||||
{
|
||||
this.info = info;
|
||||
manager = self.Trait<UpgradeManager>();
|
||||
}
|
||||
|
||||
public IEnumerable<IOrderTargeter> Orders
|
||||
void INotifyCreated.Created(Actor self)
|
||||
{
|
||||
um = self.TraitOrDefault<UpgradeManager>();
|
||||
}
|
||||
|
||||
IEnumerable<IOrderTargeter> IIssueOrder.Orders
|
||||
{
|
||||
get { yield return new DeployOrderTargeter("PrimaryProducer", 1); }
|
||||
}
|
||||
|
||||
public Order IssueOrder(Actor self, IOrderTargeter order, Target target, bool queued)
|
||||
Order IIssueOrder.IssueOrder(Actor self, IOrderTargeter order, Target target, bool queued)
|
||||
{
|
||||
if (order.OrderID == "PrimaryProducer")
|
||||
return new Order(order.OrderID, self, false);
|
||||
@@ -63,41 +69,39 @@ namespace OpenRA.Mods.Common.Traits
|
||||
return null;
|
||||
}
|
||||
|
||||
public void ResolveOrder(Actor self, Order order)
|
||||
void IResolveOrder.ResolveOrder(Actor self, Order order)
|
||||
{
|
||||
if (order.OrderString == "PrimaryProducer")
|
||||
SetPrimaryProducer(self, !IsPrimary);
|
||||
}
|
||||
|
||||
public void SetPrimaryProducer(Actor self, bool state)
|
||||
public void SetPrimaryProducer(Actor self, bool isPrimary)
|
||||
{
|
||||
if (state == false)
|
||||
IsPrimary = isPrimary;
|
||||
|
||||
if (isPrimary)
|
||||
{
|
||||
IsPrimary = false;
|
||||
foreach (var up in info.Upgrades)
|
||||
manager.RevokeUpgrade(self, up, this);
|
||||
return;
|
||||
// Cancel existing primaries
|
||||
// TODO: THIS IS SHIT
|
||||
foreach (var p in self.Info.TraitInfo<ProductionInfo>().Produces)
|
||||
{
|
||||
foreach (var b in self.World
|
||||
.ActorsWithTrait<PrimaryBuilding>()
|
||||
.Where(a =>
|
||||
a.Actor != self &&
|
||||
a.Actor.Owner == self.Owner &&
|
||||
a.Trait.IsPrimary &&
|
||||
a.Actor.Info.TraitInfo<ProductionInfo>().Produces.Contains(p)))
|
||||
b.Trait.SetPrimaryProducer(b.Actor, false);
|
||||
}
|
||||
|
||||
if (um != null && primaryToken == UpgradeManager.InvalidConditionToken && !string.IsNullOrEmpty(info.PrimaryCondition))
|
||||
primaryToken = um.GrantCondition(self, info.PrimaryCondition);
|
||||
|
||||
Game.Sound.PlayNotification(self.World.Map.Rules, self.Owner, "Speech", info.SelectionNotification, self.Owner.Faction.InternalName);
|
||||
}
|
||||
|
||||
// TODO: THIS IS SHIT
|
||||
// Cancel existing primaries
|
||||
foreach (var p in self.Info.TraitInfo<ProductionInfo>().Produces)
|
||||
{
|
||||
var productionType = p; // benign closure hazard
|
||||
foreach (var b in self.World
|
||||
.ActorsWithTrait<PrimaryBuilding>()
|
||||
.Where(a =>
|
||||
a.Actor.Owner == self.Owner &&
|
||||
a.Trait.IsPrimary &&
|
||||
a.Actor.Info.TraitInfo<ProductionInfo>().Produces.Contains(productionType)))
|
||||
b.Trait.SetPrimaryProducer(b.Actor, false);
|
||||
}
|
||||
|
||||
IsPrimary = true;
|
||||
foreach (var up in info.Upgrades)
|
||||
manager.GrantUpgrade(self, up, this);
|
||||
|
||||
Game.Sound.PlayNotification(self.World.Map.Rules, self.Owner, "Speech", info.SelectionNotification, self.Owner.Faction.InternalName);
|
||||
else if (primaryToken != UpgradeManager.InvalidConditionToken)
|
||||
primaryToken = um.RevokeCondition(self, primaryToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user