Improve ProductionBar performance.

Also adds an explicit second bar for construction yards.
This commit is contained in:
Paul Chote
2014-04-18 23:55:58 +12:00
parent abe06b24b4
commit 59da851d71
4 changed files with 54 additions and 19 deletions

View File

@@ -8,6 +8,7 @@
*/ */
#endregion #endregion
using System;
using System.Drawing; using System.Drawing;
using System.Linq; using System.Linq;
using OpenRA.FileFormats; using OpenRA.FileFormats;
@@ -18,13 +19,49 @@ namespace OpenRA.Mods.RA
[Desc("Visualizes the remaining build time of actor produced here.")] [Desc("Visualizes the remaining build time of actor produced here.")]
class ProductionBarInfo : ITraitInfo class ProductionBarInfo : ITraitInfo
{ {
public object Create(ActorInitializer init) { return new ProductionBar( init.self ); } [Desc("Production queue type, for actors with multiple queues.")]
public readonly string ProductionType = null;
public object Create(ActorInitializer init) { return new ProductionBar(init.self, this); }
} }
class ProductionBar : ISelectionBar class ProductionBar : ISelectionBar, ITick
{ {
Actor self; readonly ProductionBarInfo info;
public ProductionBar(Actor self) { this.self = self; } readonly Actor self;
ProductionQueue queue;
float value;
public ProductionBar(Actor self, ProductionBarInfo info)
{
this.self = self;
this.info = info;
}
public void Tick(Actor self)
{
if (queue == null)
{
var type = info.ProductionType ?? self.Trait<Production>().Info.Produces.First();
// Per-actor queue
queue = self.TraitsImplementing<ProductionQueue>()
.FirstOrDefault(q => type == null || type == 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);
}
if (queue == null)
throw new InvalidOperationException("No queues available for production type '{0}'".F(type));
}
var current = queue.CurrentItem();
value = current != null ? 1 - (float)current.RemainingCost / current.TotalCost : 0;
}
public float GetValue() public float GetValue()
{ {
@@ -32,18 +69,7 @@ namespace OpenRA.Mods.RA
if (!self.Owner.IsAlliedWith(self.World.RenderPlayer)) if (!self.Owner.IsAlliedWith(self.World.RenderPlayer))
return 0; return 0;
var queue = self.TraitsImplementing<ProductionQueue>().FirstOrDefault(q => q.CurrentItem() != null); return value;
if (queue == null)
{
var produces = self.Trait<Production>().Info.Produces;
queue = self.Owner.PlayerActor.TraitsImplementing<ProductionQueue>()
.FirstOrDefault(q => produces.Contains(q.Info.Type));
}
if (queue == null || queue.CurrentItem() == null)
return 0f;
return 1 - (float)queue.CurrentItem().RemainingCost / queue.CurrentItem().TotalCost;
} }
public Color GetColor() { return Color.SkyBlue; } public Color GetColor() { return Color.SkyBlue; }

View File

@@ -41,7 +41,10 @@ FACT:
QueuedAudio: Building QueuedAudio: Building
ReadyAudio: ConstructionComplete ReadyAudio: ConstructionComplete
BaseBuilding: BaseBuilding:
ProductionBar: ProductionBar@Building:
ProductionType: Building
ProductionBar@Defense:
ProductionType: Defense
BaseProvider: BaseProvider:
Cooldown: 75 Cooldown: 75
Range: 14 Range: 14

View File

@@ -810,7 +810,10 @@ FACT:
IntoActor: mcv IntoActor: mcv
Offset: 1,1 Offset: 1,1
Facing: 96 Facing: 96
ProductionBar: ProductionBar@Building:
ProductionType: Building
ProductionBar@Defense:
ProductionType: Defense
DeadBuildingState: DeadBuildingState:
BaseProvider: BaseProvider:
Range: 16 Range: 16

View File

@@ -29,7 +29,10 @@ GACNST:
IntoActor: mcv IntoActor: mcv
Offset: 1,1 Offset: 1,1
Facing: 96 Facing: 96
ProductionBar: ProductionBar@Building:
ProductionType: Building
ProductionBar@Defense:
ProductionType: Defense
-Sellable: -Sellable:
WithIdleOverlay@TOP: WithIdleOverlay@TOP:
Sequence: idle-top Sequence: idle-top