Add capture progress bars and blinking.

This commit is contained in:
Paul Chote
2018-10-06 19:35:43 +00:00
committed by abcdefg30
parent bab34252dd
commit 4d2f1f8942
5 changed files with 264 additions and 5 deletions

View File

@@ -312,6 +312,9 @@
<Compile Include="Traits\Buildings\RepairableBuilding.cs" />
<Compile Include="Traits\Buildings\RequiresBuildableArea.cs" />
<Compile Include="Traits\Buildings\Reservable.cs" />
<Compile Include="Traits\CapturableProgressBar.cs" />
<Compile Include="Traits\CaptureProgressBar.cs" />
<Compile Include="Traits\CaptureProgressBlink.cs" />
<Compile Include="Traits\RepairsUnits.cs" />
<Compile Include="Traits\Burns.cs" />
<Compile Include="Traits\ChangesTerrain.cs" />

View File

@@ -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<CapturesInfo>
{
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; } }
}
}

View File

@@ -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<CaptureType> allyCapturableTypes;
BitSet<CaptureType> 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<ConditionManager>();
move = self.TraitOrDefault<IMove>();
progressWatchers = self.TraitsImplementing<ICaptureProgressWatcher>().ToArray();
enabledCapturable = self.TraitsImplementing<Capturable>()
.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));
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;
}
/// <summary>
@@ -191,6 +223,15 @@ namespace OpenRA.Mods.Common.Traits
/// </summary>
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);
}
}
}

View File

@@ -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<CapturableInfo>
{
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<Actor, Pair<int, int>> progress = new Dictionary<Actor, Pair<int, int>>();
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; } }
}
}

View File

@@ -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<CapturableInfo>
{
[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<Player> captorOwners = new List<Player>();
HashSet<Actor> captors = new HashSet<Actor>();
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;
}
}
}