Reworked cloaking behaviour
This commit is contained in:
@@ -17,6 +17,18 @@ using OpenRA.Traits;
|
||||
|
||||
namespace OpenRA.Mods.Common.Traits
|
||||
{
|
||||
[Flags]
|
||||
public enum UncloakType
|
||||
{
|
||||
None = 0,
|
||||
Attack = 1,
|
||||
Move = 2,
|
||||
Unload = 4,
|
||||
Infiltrate = 8,
|
||||
Demolish = 16,
|
||||
Damage = 32
|
||||
}
|
||||
|
||||
[Desc("This unit can cloak and uncloak in specific situations.")]
|
||||
public class CloakInfo : UpgradableTraitInfo
|
||||
{
|
||||
@@ -26,11 +38,9 @@ namespace OpenRA.Mods.Common.Traits
|
||||
[Desc("Measured in game ticks.")]
|
||||
public readonly int CloakDelay = 30;
|
||||
|
||||
public readonly bool UncloakOnAttack = true;
|
||||
public readonly bool UncloakOnMove = false;
|
||||
public readonly bool UncloakOnUnload = true;
|
||||
public readonly bool UncloakOnInfiltrate = true;
|
||||
public readonly bool UncloakOnDemolish = true;
|
||||
[Desc("Events leading to the actor getting uncloaked. Possible values are: Attack, Move, Unload, Infiltrate, Demolish and Damage")]
|
||||
public readonly UncloakType UncloakOn = UncloakType.Attack
|
||||
| UncloakType.Unload | UncloakType.Infiltrate | UncloakType.Demolish;
|
||||
|
||||
public readonly string CloakSound = null;
|
||||
public readonly string UncloakSound = null;
|
||||
@@ -44,70 +54,48 @@ namespace OpenRA.Mods.Common.Traits
|
||||
[Desc("The upgrades to grant to self while cloaked.")]
|
||||
public readonly string[] WhileCloakedUpgrades = { };
|
||||
|
||||
public override object Create(ActorInitializer init) { return new Cloak(init.Self, this); }
|
||||
public override object Create(ActorInitializer init) { return new Cloak(this); }
|
||||
}
|
||||
|
||||
public class Cloak : UpgradableTrait<CloakInfo>, IRenderModifier, INotifyDamageStateChanged, INotifyAttack, ITick, IVisibilityModifier, IRadarColorModifier, INotifyCreated
|
||||
public class Cloak : UpgradableTrait<CloakInfo>, IRenderModifier, INotifyDamageStateChanged,
|
||||
INotifyAttack, ITick, IVisibilityModifier, IRadarColorModifier, INotifyCreated
|
||||
{
|
||||
[Sync] int remainingTime;
|
||||
[Sync] bool damageDisabled;
|
||||
UpgradeManager upgradeManager;
|
||||
|
||||
Actor self;
|
||||
CPos? lastPos;
|
||||
bool wasCloaked = false;
|
||||
|
||||
public Cloak(Actor self, CloakInfo info)
|
||||
public Cloak(CloakInfo info)
|
||||
: base(info)
|
||||
{
|
||||
this.self = self;
|
||||
|
||||
remainingTime = info.InitialDelay;
|
||||
}
|
||||
|
||||
public void Created(Actor self)
|
||||
void INotifyCreated.Created(Actor self)
|
||||
{
|
||||
upgradeManager = self.TraitOrDefault<UpgradeManager>();
|
||||
if (remainingTime == 0)
|
||||
{
|
||||
if (upgradeManager != null)
|
||||
foreach (var u in Info.WhileCloakedUpgrades)
|
||||
upgradeManager.GrantUpgrade(self, u, this);
|
||||
}
|
||||
}
|
||||
|
||||
protected override void UpgradeDisabled(Actor self)
|
||||
{
|
||||
Uncloak();
|
||||
remainingTime = Info.InitialDelay;
|
||||
}
|
||||
|
||||
public void Uncloak() { Uncloak(Info.CloakDelay); }
|
||||
|
||||
public void Uncloak(int time)
|
||||
{
|
||||
if (Cloaked)
|
||||
{
|
||||
Game.Sound.Play(Info.UncloakSound, self.CenterPosition);
|
||||
if (upgradeManager != null)
|
||||
foreach (var u in Info.WhileCloakedUpgrades)
|
||||
upgradeManager.RevokeUpgrade(self, u, this);
|
||||
}
|
||||
|
||||
remainingTime = Math.Max(remainingTime, time);
|
||||
GrantUpgrades(self);
|
||||
}
|
||||
|
||||
public void Attacking(Actor self, Target target, Armament a, Barrel barrel) { if (Info.UncloakOnAttack) Uncloak(); }
|
||||
|
||||
public bool Cloaked { get { return !IsTraitDisabled && remainingTime <= 0; } }
|
||||
|
||||
public void DamageStateChanged(Actor self, AttackInfo e)
|
||||
public void Uncloak() { Uncloak(Info.CloakDelay); }
|
||||
|
||||
public void Uncloak(int time) { remainingTime = Math.Max(remainingTime, time); }
|
||||
|
||||
void INotifyAttack.Attacking(Actor self, Target target, Armament a, Barrel barrel) { if (Info.UncloakOn.HasFlag(UncloakType.Attack)) Uncloak(); }
|
||||
|
||||
void INotifyDamageStateChanged.DamageStateChanged(Actor self, AttackInfo e)
|
||||
{
|
||||
damageDisabled = e.DamageState >= DamageState.Critical;
|
||||
if (damageDisabled)
|
||||
if (damageDisabled || Info.UncloakOn.HasFlag(UncloakType.Damage))
|
||||
Uncloak();
|
||||
}
|
||||
|
||||
public IEnumerable<IRenderable> ModifyRender(Actor self, WorldRenderer wr, IEnumerable<IRenderable> r)
|
||||
IEnumerable<IRenderable> IRenderModifier.ModifyRender(Actor self, WorldRenderer wr, IEnumerable<IRenderable> r)
|
||||
{
|
||||
if (remainingTime > 0 || IsTraitDisabled)
|
||||
return r;
|
||||
@@ -124,27 +112,38 @@ namespace OpenRA.Mods.Common.Traits
|
||||
return SpriteRenderable.None;
|
||||
}
|
||||
|
||||
public void Tick(Actor self)
|
||||
void ITick.Tick(Actor self)
|
||||
{
|
||||
if (IsTraitDisabled)
|
||||
return;
|
||||
|
||||
if (remainingTime > 0 && !IsTraitDisabled && !damageDisabled && --remainingTime <= 0)
|
||||
if (!IsTraitDisabled)
|
||||
{
|
||||
Game.Sound.Play(Info.CloakSound, self.CenterPosition);
|
||||
if (upgradeManager != null)
|
||||
foreach (var u in Info.WhileCloakedUpgrades)
|
||||
upgradeManager.GrantUpgrade(self, u, this);
|
||||
if (remainingTime > 0 && !damageDisabled)
|
||||
remainingTime--;
|
||||
|
||||
if (self.IsDisabled())
|
||||
Uncloak();
|
||||
|
||||
if (Info.UncloakOn.HasFlag(UncloakType.Move) && (lastPos == null || lastPos.Value != self.Location))
|
||||
{
|
||||
Uncloak();
|
||||
lastPos = self.Location;
|
||||
}
|
||||
}
|
||||
|
||||
if (self.IsDisabled())
|
||||
Uncloak();
|
||||
|
||||
if (Info.UncloakOnMove && (lastPos == null || lastPos.Value != self.Location))
|
||||
var isCloaked = Cloaked;
|
||||
if (isCloaked && !wasCloaked)
|
||||
{
|
||||
Uncloak();
|
||||
lastPos = self.Location;
|
||||
GrantUpgrades(self);
|
||||
if (!self.TraitsImplementing<Cloak>().Any(a => a != this && a.Cloaked))
|
||||
Game.Sound.Play(Info.CloakSound, self.CenterPosition);
|
||||
}
|
||||
else if (!isCloaked && wasCloaked)
|
||||
{
|
||||
RevokeUpgrades(self);
|
||||
if (!self.TraitsImplementing<Cloak>().Any(a => a != this && a.Cloaked))
|
||||
Game.Sound.Play(Info.UncloakSound, self.CenterPosition);
|
||||
}
|
||||
|
||||
wasCloaked = isCloaked;
|
||||
}
|
||||
|
||||
public bool IsVisible(Actor self, Player viewer)
|
||||
@@ -157,12 +156,26 @@ namespace OpenRA.Mods.Common.Traits
|
||||
&& (self.CenterPosition - a.Actor.CenterPosition).LengthSquared <= a.Trait.Info.Range.LengthSquared);
|
||||
}
|
||||
|
||||
public Color RadarColorOverride(Actor self)
|
||||
Color IRadarColorModifier.RadarColorOverride(Actor self)
|
||||
{
|
||||
var c = self.Owner.Color.RGB;
|
||||
if (self.Owner == self.World.LocalPlayer && Cloaked)
|
||||
c = Color.FromArgb(128, c);
|
||||
return c;
|
||||
}
|
||||
|
||||
void GrantUpgrades(Actor self)
|
||||
{
|
||||
if (upgradeManager != null)
|
||||
foreach (var u in Info.WhileCloakedUpgrades)
|
||||
upgradeManager.GrantUpgrade(self, u, this);
|
||||
}
|
||||
|
||||
void RevokeUpgrades(Actor self)
|
||||
{
|
||||
if (upgradeManager != null)
|
||||
foreach (var u in Info.WhileCloakedUpgrades)
|
||||
upgradeManager.RevokeUpgrade(self, u, this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user