Overhaul ProductionBar:

- Is now a conditional trait
- Now respects multiple Production trait instances
- ProductionType is now required
This commit is contained in:
abcdefg30
2018-10-28 18:39:45 +00:00
committed by abcdefg30
parent 7f255a17da
commit 5e5183549c
10 changed files with 110 additions and 21 deletions

View File

@@ -595,6 +595,7 @@
<Compile Include="UpdateRules\Rules\20180923\RemovedNotifyBuildComplete.cs" />
<Compile Include="UpdateRules\Rules\20180923\ChangeTakeOffSoundAndLandingSound.cs" />
<Compile Include="UpdateRules\Rules\20180923\AddCarryableHarvester.cs" />
<Compile Include="UpdateRules\Rules\20180923\RequireProductionType.cs" />
<Compile Include="UtilityCommands\CheckYaml.cs" />
<Compile Include="UtilityCommands\ConvertPngToShpCommand.cs" />
<Compile Include="UtilityCommands\ConvertSpriteToPngCommand.cs" />

View File

@@ -17,73 +17,72 @@ using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits.Render
{
[Desc("Visualizes the remaining build time of actor produced here.")]
class ProductionBarInfo : ITraitInfo, Requires<ProductionInfo>
class ProductionBarInfo : ConditionalTraitInfo, Requires<ProductionInfo>
{
[FieldLoader.Require]
[Desc("Production queue type, for actors with multiple queues.")]
public readonly string ProductionType = null;
public readonly Color Color = Color.SkyBlue;
public object Create(ActorInitializer init) { return new ProductionBar(init.Self, this); }
public override object Create(ActorInitializer init) { return new ProductionBar(init.Self, this); }
}
class ProductionBar : ISelectionBar, ITick, INotifyCreated, INotifyOwnerChanged
class ProductionBar : ConditionalTrait<ProductionBarInfo>, ISelectionBar, ITick, INotifyOwnerChanged
{
readonly ProductionBarInfo info;
readonly Actor self;
ProductionQueue queue;
float value;
public ProductionBar(Actor self, ProductionBarInfo info)
: base(info)
{
this.self = self;
this.info = info;
}
protected override void Created(Actor self)
{
base.Created(self);
FindQueue();
}
void FindQueue()
{
var type = info.ProductionType ?? self.Info.TraitInfo<ProductionInfo>().Produces.First();
// Per-actor queue
// Note: this includes disabled queues, as each bar must bind to exactly one queue.
queue = self.TraitsImplementing<ProductionQueue>()
.FirstOrDefault(q => type == null || type == q.Info.Type);
.FirstOrDefault(q => Info.ProductionType == q.Info.Type);
if (queue == null)
{
// No queues available - check for classic production queues
queue = self.Owner.PlayerActor.TraitsImplementing<ProductionQueue>()
.FirstOrDefault(q => type == null || type == q.Info.Type);
.FirstOrDefault(q => Info.ProductionType == q.Info.Type);
}
if (queue == null)
throw new InvalidOperationException("No queues available for production type '{0}'".F(type));
}
void INotifyCreated.Created(Actor self)
{
FindQueue();
}
void ITick.Tick(Actor self)
{
if (IsTraitDisabled)
return;
var current = queue.AllQueued().Where(i => i.Started).OrderBy(i => i.RemainingTime).FirstOrDefault();
value = current != null ? 1 - (float)current.RemainingCost / current.TotalCost : 0;
}
float ISelectionBar.GetValue()
{
// only people we like should see our production status.
if (!self.Owner.IsAlliedWith(self.World.RenderPlayer))
// Only people we like should see our production status.
if (IsTraitDisabled || !self.Owner.IsAlliedWith(self.World.RenderPlayer))
return 0;
return value;
}
Color ISelectionBar.GetColor() { return info.Color; }
Color ISelectionBar.GetColor() { return Info.Color; }
bool ISelectionBar.DisplayWhenEmpty { get { return false; } }
public void OnOwnerChanged(Actor self, Player oldOwner, Player newOwner)
void INotifyOwnerChanged.OnOwnerChanged(Actor self, Player oldOwner, Player newOwner)
{
FindQueue();
}

View File

@@ -0,0 +1,63 @@
#region Copyright & License Information
/*
* Copyright 2007-2018 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version. For more
* information, see COPYING.
*/
#endregion
using System.Collections.Generic;
using System.Linq;
namespace OpenRA.Mods.Common.UpdateRules.Rules
{
public class RequireProductionType : UpdateRule
{
public override string Name { get { return "Require 'ProductionType' on 'ProductionBar'"; } }
public override string Description
{
get
{
return "The 'ProductionBar' trait now requires the 'ProductionType' to be set.\n" +
"The value will be automatically set to the first value in 'Produces' of the first 'Production' trait.";
}
}
readonly string[] productionTraits = { "Production", "ProductionAirdrop", "ProductionParadrop", "ProductionFromMapEdge" };
public override IEnumerable<string> UpdateActorNode(ModData modData, MiniYamlNode actorNode)
{
foreach (var pb in actorNode.ChildrenMatching("ProductionBar"))
{
var type = pb.LastChildMatching("ProductionType");
if (type != null)
continue;
MiniYamlNode production = null;
foreach (var trait in productionTraits)
{
if (production != null)
break;
production = actorNode.ChildrenMatching(trait).FirstOrDefault();
}
if (production == null)
continue;
var produces = production.LastChildMatching("Produces");
if (produces == null)
continue;
var toAdd = produces.NodeValue<string[]>().FirstOrDefault();
if (toAdd != null)
pb.AddNode("ProductionType", toAdd);
}
yield break;
}
}
}

View File

@@ -103,6 +103,7 @@ namespace OpenRA.Mods.Common.UpdateRules
new AddRearmable(),
new MergeAttackPlaneAndHeli(),
new RemovedDemolishLocking(),
new RequireProductionType(),
})
};