Make Capture related traits conditional.

This commit is contained in:
Mustafa Alperen Seki
2019-03-12 12:23:32 +03:00
committed by reaperrr
parent 35ad33e8be
commit 556774804f
3 changed files with 20 additions and 26 deletions

View File

@@ -17,22 +17,19 @@ using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits namespace OpenRA.Mods.Common.Traits
{ {
[Desc("Visualize capture progress.")] [Desc("Visualize capture progress.")]
class CapturableProgressBarInfo : ITraitInfo, Requires<CapturableInfo> class CapturableProgressBarInfo : ConditionalTraitInfo, Requires<CapturableInfo>
{ {
public readonly Color Color = Color.Orange; public readonly Color Color = Color.Orange;
public object Create(ActorInitializer init) { return new CapturableProgressBar(init.Self, this); } public override object Create(ActorInitializer init) { return new CapturableProgressBar(init.Self, this); }
} }
class CapturableProgressBar : ISelectionBar, ICaptureProgressWatcher class CapturableProgressBar : ConditionalTrait<CapturableProgressBarInfo>, ISelectionBar, ICaptureProgressWatcher
{ {
readonly CapturableProgressBarInfo info;
Dictionary<Actor, Pair<int, int>> progress = new Dictionary<Actor, Pair<int, int>>(); Dictionary<Actor, Pair<int, int>> progress = new Dictionary<Actor, Pair<int, int>>();
public CapturableProgressBar(Actor self, CapturableProgressBarInfo info) public CapturableProgressBar(Actor self, CapturableProgressBarInfo info)
{ : base(info) { }
this.info = info;
}
void ICaptureProgressWatcher.Update(Actor self, Actor captor, Actor target, int current, int total) void ICaptureProgressWatcher.Update(Actor self, Actor captor, Actor target, int current, int total)
{ {
@@ -47,13 +44,13 @@ namespace OpenRA.Mods.Common.Traits
float ISelectionBar.GetValue() float ISelectionBar.GetValue()
{ {
if (!progress.Any()) if (IsTraitDisabled || !progress.Any())
return 0f; return 0f;
return progress.Values.Max(p => (float)p.First / p.Second); return progress.Values.Max(p => (float)p.First / p.Second);
} }
Color ISelectionBar.GetColor() { return info.Color; } Color ISelectionBar.GetColor() { return Info.Color; }
bool ISelectionBar.DisplayWhenEmpty { get { return false; } } bool ISelectionBar.DisplayWhenEmpty { get { return false; } }
} }
} }

View File

@@ -17,25 +17,22 @@ using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits namespace OpenRA.Mods.Common.Traits
{ {
[Desc("Blinks the actor and captor when it is being captured.")] [Desc("Blinks the actor and captor when it is being captured.")]
class CapturableProgressBlinkInfo : ITraitInfo, Requires<CapturableInfo> class CapturableProgressBlinkInfo : ConditionalTraitInfo, Requires<CapturableInfo>
{ {
[Desc("Number of ticks to wait between repeating blinks.")] [Desc("Number of ticks to wait between repeating blinks.")]
public readonly int Interval = 50; public readonly int Interval = 50;
public object Create(ActorInitializer init) { return new CapturableProgressBlink(this); } public override object Create(ActorInitializer init) { return new CapturableProgressBlink(this); }
} }
class CapturableProgressBlink : ITick, ICaptureProgressWatcher class CapturableProgressBlink : ConditionalTrait<CapturableProgressBlinkInfo>, ITick, ICaptureProgressWatcher
{ {
readonly CapturableProgressBlinkInfo info;
List<Player> captorOwners = new List<Player>(); List<Player> captorOwners = new List<Player>();
HashSet<Actor> captors = new HashSet<Actor>(); HashSet<Actor> captors = new HashSet<Actor>();
int tick = 0; int tick = 0;
public CapturableProgressBlink(CapturableProgressBlinkInfo info) public CapturableProgressBlink(CapturableProgressBlinkInfo info)
{ : base(info) { }
this.info = info;
}
void ICaptureProgressWatcher.Update(Actor self, Actor captor, Actor target, int current, int total) void ICaptureProgressWatcher.Update(Actor self, Actor captor, Actor target, int current, int total)
{ {
@@ -54,6 +51,9 @@ namespace OpenRA.Mods.Common.Traits
void ITick.Tick(Actor self) void ITick.Tick(Actor self)
{ {
if (IsTraitDisabled)
return;
if (!captorOwners.Any()) if (!captorOwners.Any())
{ {
tick = 0; tick = 0;
@@ -70,7 +70,7 @@ namespace OpenRA.Mods.Common.Traits
self.World.Add(new FlashTarget(captor, captorOwner)); self.World.Add(new FlashTarget(captor, captorOwner));
} }
if (++tick >= info.Interval) if (++tick >= Info.Interval)
tick = 0; tick = 0;
} }
} }

View File

@@ -15,23 +15,20 @@ using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits namespace OpenRA.Mods.Common.Traits
{ {
[Desc("Visualize the progress of this actor being captured.")] [Desc("Visualize the progress of this actor being captured.")]
class CaptureProgressBarInfo : ITraitInfo, Requires<CapturesInfo> class CaptureProgressBarInfo : ConditionalTraitInfo, Requires<CapturesInfo>
{ {
public readonly Color Color = Color.Orange; public readonly Color Color = Color.Orange;
public object Create(ActorInitializer init) { return new CaptureProgressBar(init.Self, this); } public override object Create(ActorInitializer init) { return new CaptureProgressBar(init.Self, this); }
} }
class CaptureProgressBar : ISelectionBar, ICaptureProgressWatcher class CaptureProgressBar : ConditionalTrait<CaptureProgressBarInfo>, ISelectionBar, ICaptureProgressWatcher
{ {
readonly CaptureProgressBarInfo info;
int current; int current;
int total; int total;
public CaptureProgressBar(Actor self, CaptureProgressBarInfo info) public CaptureProgressBar(Actor self, CaptureProgressBarInfo info)
{ : base(info) { }
this.info = info;
}
void ICaptureProgressWatcher.Update(Actor self, Actor captor, Actor target, int current, int total) void ICaptureProgressWatcher.Update(Actor self, Actor captor, Actor target, int current, int total)
{ {
@@ -44,13 +41,13 @@ namespace OpenRA.Mods.Common.Traits
float ISelectionBar.GetValue() float ISelectionBar.GetValue()
{ {
if (total == 0) if (IsTraitDisabled || total == 0)
return 0f; return 0f;
return (float)current / total; return (float)current / total;
} }
Color ISelectionBar.GetColor() { return info.Color; } Color ISelectionBar.GetColor() { return Info.Color; }
bool ISelectionBar.DisplayWhenEmpty { get { return false; } } bool ISelectionBar.DisplayWhenEmpty { get { return false; } }
} }
} }