Make AppearsOnRadar trait conditional

This commit is contained in:
atlimit8
2017-05-29 03:58:16 -05:00
committed by reaperrr
parent 8a825f2999
commit 43880ea7b8

View File

@@ -18,38 +18,44 @@ using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits.Radar
{
public class AppearsOnRadarInfo : ITraitInfo
public class AppearsOnRadarInfo : ConditionalTraitInfo
{
public readonly bool UseLocation = false;
public object Create(ActorInitializer init) { return new AppearsOnRadar(this); }
[Desc("Player stances who can view this actor on radar.")]
public readonly Stance ValidStances = Stance.Ally | Stance.Neutral | Stance.Enemy;
public override object Create(ActorInitializer init) { return new AppearsOnRadar(this); }
}
public class AppearsOnRadar : IRadarSignature, INotifyCreated
public class AppearsOnRadar : ConditionalTrait<AppearsOnRadarInfo>, IRadarSignature
{
readonly AppearsOnRadarInfo info;
static readonly IEnumerable<Pair<CPos, Color>> NoCells = Enumerable.Empty<Pair<CPos, Color>>();
IRadarColorModifier modifier;
Color currentColor = Color.Transparent;
Func<Pair<CPos, SubCell>, Pair<CPos, Color>> cellToSignature;
public AppearsOnRadar(AppearsOnRadarInfo info)
{
this.info = info;
}
: base(info) { }
public void Created(Actor self)
protected override void Created(Actor self)
{
base.Created(self);
modifier = self.TraitsImplementing<IRadarColorModifier>().FirstOrDefault();
}
public IEnumerable<Pair<CPos, Color>> RadarSignatureCells(Actor self)
{
var viewer = self.World.RenderPlayer ?? self.World.LocalPlayer;
if (IsTraitDisabled || (viewer != null && !Info.ValidStances.HasStance(self.Owner.Stances[viewer])))
return NoCells;
var color = Game.Settings.Game.UsePlayerStanceColors ? self.Owner.PlayerStanceColor(self) : self.Owner.Color.RGB;
if (modifier != null)
color = modifier.RadarColorOverride(self, color);
if (info.UseLocation)
if (Info.UseLocation)
return new[] { Pair.New(self.Location, color) };
// PERF: Cache cellToSignature delegate to avoid allocations as color does not change often.