diff --git a/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj b/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj index e3ba2cf9bc..bb56cc9b76 100644 --- a/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj +++ b/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj @@ -312,6 +312,9 @@ + + + diff --git a/OpenRA.Mods.Common/Traits/CapturableProgressBar.cs b/OpenRA.Mods.Common/Traits/CapturableProgressBar.cs new file mode 100644 index 0000000000..feedbdb163 --- /dev/null +++ b/OpenRA.Mods.Common/Traits/CapturableProgressBar.cs @@ -0,0 +1,56 @@ +#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.Drawing; +using OpenRA.Traits; + +namespace OpenRA.Mods.Common.Traits +{ + [Desc("Visualize the progress of this actor being captured.")] + class CaptureProgressBarInfo : ITraitInfo, Requires + { + public readonly Color Color = Color.Orange; + + public object Create(ActorInitializer init) { return new CaptureProgressBar(init.Self, this); } + } + + class CaptureProgressBar : ISelectionBar, ICaptureProgressWatcher + { + readonly CaptureProgressBarInfo info; + int current; + int total; + + public CaptureProgressBar(Actor self, CaptureProgressBarInfo info) + { + this.info = info; + } + + void ICaptureProgressWatcher.Update(Actor self, Actor captor, Actor target, int current, int total) + { + if (self != captor) + return; + + this.current = current; + this.total = total; + } + + float ISelectionBar.GetValue() + { + if (total == 0) + return 0f; + + return (float)current / total; + } + + Color ISelectionBar.GetColor() { return info.Color; } + bool ISelectionBar.DisplayWhenEmpty { get { return false; } } + } +} diff --git a/OpenRA.Mods.Common/Traits/CaptureManager.cs b/OpenRA.Mods.Common/Traits/CaptureManager.cs index 486432cc33..b4c65d8908 100644 --- a/OpenRA.Mods.Common/Traits/CaptureManager.cs +++ b/OpenRA.Mods.Common/Traits/CaptureManager.cs @@ -19,6 +19,12 @@ namespace OpenRA.Mods.Common.Traits { public sealed class CaptureType { CaptureType() { } } + [RequireExplicitImplementation] + public interface ICaptureProgressWatcher + { + void Update(Actor self, Actor captor, Actor target, int progress, int total); + } + [Desc("Manages Captures and Capturable traits on an actor.")] public class CaptureManagerInfo : ITraitInfo { @@ -33,10 +39,12 @@ namespace OpenRA.Mods.Common.Traits public virtual object Create(ActorInitializer init) { return new CaptureManager(this); } } - public class CaptureManager : INotifyCreated, INotifyCapture + public class CaptureManager : INotifyCreated, INotifyCapture, ITick { readonly CaptureManagerInfo info; ConditionManager conditionManager; + IMove move; + ICaptureProgressWatcher[] progressWatchers; BitSet allyCapturableTypes; BitSet neutralCapturableTypes; @@ -50,8 +58,10 @@ namespace OpenRA.Mods.Common.Traits Actor currentTarget; CaptureManager currentTargetManager; int currentTargetDelay; + int currentTargetTotal; int capturingToken = ConditionManager.InvalidConditionToken; int beingCapturedToken = ConditionManager.InvalidConditionToken; + bool enteringCurrentTarget; public bool BeingCaptured { get; private set; } @@ -63,6 +73,8 @@ namespace OpenRA.Mods.Common.Traits void INotifyCreated.Created(Actor self) { conditionManager = self.TraitOrDefault(); + move = self.TraitOrDefault(); + progressWatchers = self.TraitsImplementing().ToArray(); enabledCapturable = self.TraitsImplementing() .ToArray() @@ -177,11 +189,31 @@ namespace OpenRA.Mods.Common.Traits targetManager.beingCapturedToken == ConditionManager.InvalidConditionToken) targetManager.beingCapturedToken = targetManager.conditionManager.GrantCondition(target, targetManager.info.BeingCapturedCondition); - foreach (var c in enabledCaptures.OrderBy(c => c.Info.CaptureDelay)) - if (targetManager.CanBeTargetedBy(target, self, c) && c.Info.CaptureDelay <= currentTargetDelay) - return true; + var captures = enabledCaptures + .OrderBy(c => c.Info.CaptureDelay) + .FirstOrDefault(c => targetManager.CanBeTargetedBy(target, self, c)); - return false; + if (captures == null) + return false; + + if (progressWatchers.Any() || targetManager.progressWatchers.Any()) + { + currentTargetTotal = captures.Info.CaptureDelay; + if (move != null) + { + var pos = target.GetTargetablePositions().PositionClosestTo(self.CenterPosition); + currentTargetTotal += move.EstimatedMoveDuration(self, self.CenterPosition, pos); + } + + foreach (var w in progressWatchers) + w.Update(self, self, target, currentTargetDelay, currentTargetTotal); + + foreach (var w in targetManager.progressWatchers) + w.Update(target, self, target, currentTargetDelay, currentTargetTotal); + } + + enteringCurrentTarget = currentTargetDelay >= captures.Info.CaptureDelay; + return enteringCurrentTarget; } /// @@ -191,6 +223,15 @@ namespace OpenRA.Mods.Common.Traits /// public void CancelCapture(Actor self, Actor target, CaptureManager targetManager) { + if (currentTarget == null) + return; + + foreach (var w in progressWatchers) + w.Update(self, self, target, 0, 0); + + foreach (var w in targetManager.progressWatchers) + w.Update(target, self, target, 0, 0); + if (capturingToken != ConditionManager.InvalidConditionToken) capturingToken = conditionManager.RevokeCondition(self, capturingToken); @@ -200,6 +241,24 @@ namespace OpenRA.Mods.Common.Traits currentTarget = null; currentTargetManager = null; currentTargetDelay = 0; + enteringCurrentTarget = false; + } + + void ITick.Tick(Actor self) + { + // TryCapture is not called once the captor starts entering the target + // so we continue ticking the progress watchers ourself + if (!enteringCurrentTarget) + return; + + if (currentTargetDelay < currentTargetTotal) + currentTargetDelay++; + + foreach (var w in progressWatchers) + w.Update(self, self, currentTarget, currentTargetDelay, currentTargetTotal); + + foreach (var w in currentTargetManager.progressWatchers) + w.Update(currentTarget, self, currentTarget, currentTargetDelay, currentTargetTotal); } } } diff --git a/OpenRA.Mods.Common/Traits/CaptureProgressBar.cs b/OpenRA.Mods.Common/Traits/CaptureProgressBar.cs new file mode 100644 index 0000000000..8cc2649bcf --- /dev/null +++ b/OpenRA.Mods.Common/Traits/CaptureProgressBar.cs @@ -0,0 +1,61 @@ +#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; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using OpenRA.Primitives; +using OpenRA.Traits; + +namespace OpenRA.Mods.Common.Traits +{ + [Desc("Visualize capture progress.")] + class CapturableProgressBarInfo : ITraitInfo, Requires + { + public readonly Color Color = Color.Orange; + + public object Create(ActorInitializer init) { return new CapturableProgressBar(init.Self, this); } + } + + class CapturableProgressBar : ISelectionBar, ICaptureProgressWatcher + { + readonly CapturableProgressBarInfo info; + Dictionary> progress = new Dictionary>(); + + public CapturableProgressBar(Actor self, CapturableProgressBarInfo info) + { + this.info = info; + } + + void ICaptureProgressWatcher.Update(Actor self, Actor captor, Actor target, int current, int total) + { + if (self != target) + return; + + if (total == 0) + progress.Remove(captor); + else + progress[captor] = Pair.New(current, total); + } + + float ISelectionBar.GetValue() + { + if (!progress.Any()) + return 0f; + + return progress.Values.Max(p => (float)p.First / p.Second); + } + + Color ISelectionBar.GetColor() { return info.Color; } + bool ISelectionBar.DisplayWhenEmpty { get { return false; } } + } +} diff --git a/OpenRA.Mods.Common/Traits/CaptureProgressBlink.cs b/OpenRA.Mods.Common/Traits/CaptureProgressBlink.cs new file mode 100644 index 0000000000..4307d68a95 --- /dev/null +++ b/OpenRA.Mods.Common/Traits/CaptureProgressBlink.cs @@ -0,0 +1,80 @@ +#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; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using OpenRA.Mods.Common.Effects; +using OpenRA.Primitives; +using OpenRA.Traits; + +namespace OpenRA.Mods.Common.Traits +{ + [Desc("Blinks the actor and captor when it is being captured.")] + class CapturableProgressBlinkInfo : ITraitInfo, Requires + { + [Desc("Number of ticks to wait between repeating blinks.")] + public readonly int Interval = 50; + + public object Create(ActorInitializer init) { return new CapturableProgressBlink(this); } + } + + class CapturableProgressBlink : ITick, ICaptureProgressWatcher + { + readonly CapturableProgressBlinkInfo info; + List captorOwners = new List(); + HashSet captors = new HashSet(); + int tick = 0; + + public CapturableProgressBlink(CapturableProgressBlinkInfo info) + { + this.info = info; + } + + void ICaptureProgressWatcher.Update(Actor self, Actor captor, Actor target, int current, int total) + { + if (self != target) + return; + + if (total == 0) + { + captors.Remove(captor); + if (!captors.Any(c => c.Owner == captor.Owner)) + captorOwners.Remove(captor.Owner); + } + else if (captors.Add(captor) && !captorOwners.Contains(captor.Owner)) + captorOwners.Add(captor.Owner); + } + + void ITick.Tick(Actor self) + { + if (!captorOwners.Any()) + { + tick = 0; + return; + } + + // Separate the blinks from different players by 4 ticks + if (tick / 4 < captorOwners.Count && tick % 4 == 0) + { + var captorOwner = captorOwners[tick / 4]; + self.World.Add(new FlashTarget(self, captorOwner)); + foreach (var captor in captors) + if (captor.Owner == captorOwner) + self.World.Add(new FlashTarget(captor, captorOwner)); + } + + if (++tick >= info.Interval) + tick = 0; + } + } +}