Make SupportPowerChargeBar conditional

This commit is contained in:
Mustafa Alperen Seki
2018-01-14 13:17:33 +03:00
committed by abcdefg30
parent ce63dd33db
commit 99eedfd0e7
2 changed files with 11 additions and 8 deletions

View File

@@ -16,43 +16,45 @@ using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits.Render namespace OpenRA.Mods.Common.Traits.Render
{ {
[Desc("Display the time remaining until the super weapon attached to the actor is ready.")] [Desc("Display the time remaining until the super weapon attached to the actor is ready.")]
class SupportPowerChargeBarInfo : ITraitInfo class SupportPowerChargeBarInfo : ConditionalTraitInfo
{ {
[Desc("Defines to which players the bar is to be shown.")] [Desc("Defines to which players the bar is to be shown.")]
public readonly Stance DisplayStances = Stance.Ally; public readonly Stance DisplayStances = Stance.Ally;
public readonly Color Color = Color.Magenta; public readonly Color Color = Color.Magenta;
public object Create(ActorInitializer init) { return new SupportPowerChargeBar(init.Self, this); } public override object Create(ActorInitializer init) { return new SupportPowerChargeBar(init.Self, this); }
} }
class SupportPowerChargeBar : ISelectionBar, INotifyOwnerChanged class SupportPowerChargeBar : ConditionalTrait<SupportPowerChargeBarInfo>, ISelectionBar, INotifyOwnerChanged
{ {
readonly Actor self; readonly Actor self;
readonly SupportPowerChargeBarInfo info;
SupportPowerManager spm; SupportPowerManager spm;
public SupportPowerChargeBar(Actor self, SupportPowerChargeBarInfo info) public SupportPowerChargeBar(Actor self, SupportPowerChargeBarInfo info)
: base(info)
{ {
this.self = self; this.self = self;
this.info = info;
spm = self.Owner.PlayerActor.Trait<SupportPowerManager>(); spm = self.Owner.PlayerActor.Trait<SupportPowerManager>();
} }
float ISelectionBar.GetValue() float ISelectionBar.GetValue()
{ {
if (IsTraitDisabled)
return 0;
var power = spm.GetPowersForActor(self).FirstOrDefault(sp => !sp.Disabled); var power = spm.GetPowersForActor(self).FirstOrDefault(sp => !sp.Disabled);
if (power == null) if (power == null)
return 0; return 0;
var viewer = self.World.RenderPlayer ?? self.World.LocalPlayer; var viewer = self.World.RenderPlayer ?? self.World.LocalPlayer;
if (viewer != null && !info.DisplayStances.HasStance(self.Owner.Stances[viewer])) if (viewer != null && !Info.DisplayStances.HasStance(self.Owner.Stances[viewer]))
return 0; return 0;
return 1 - (float)power.RemainingTime / power.TotalTime; return 1 - (float)power.RemainingTime / power.TotalTime;
} }
Color ISelectionBar.GetColor() { return info.Color; } Color ISelectionBar.GetColor() { return Info.Color; }
bool ISelectionBar.DisplayWhenEmpty { get { return false; } } bool ISelectionBar.DisplayWhenEmpty { get { return false; } }
void INotifyOwnerChanged.OnOwnerChanged(Actor self, Player oldOwner, Player newOwner) void INotifyOwnerChanged.OnOwnerChanged(Actor self, Player oldOwner, Player newOwner)

View File

@@ -124,7 +124,8 @@ namespace OpenRA.Mods.Common.Traits
return NoInstances; return NoInstances;
return a.TraitsImplementing<SupportPower>() return a.TraitsImplementing<SupportPower>()
.Select(t => Powers[MakeKey(t)]); .Select(t => Powers[MakeKey(t)])
.Where(p => p.Instances.Any(i => !i.IsTraitDisabled && i.Self == a));
} }
public void PrerequisitesAvailable(string key) public void PrerequisitesAvailable(string key)