Skip to content

Commit

Permalink
Fix AttackGarrisonedSP not uncloak when attacking
Browse files Browse the repository at this point in the history
  • Loading branch information
dnqbob committed Oct 7, 2023
1 parent d879561 commit 8485716
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions OpenRA.Mods.Sp/Traits/AttackGarrisonedSP.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public sealed class AttackGarrisonedSP : AttackFollow, INotifyPassengerEntered,
readonly Lazy<BodyOrientation> coords;
readonly Dictionary<Actor, FirePortSP> ports = new();
readonly Dictionary<AnimationWithOffset, string> muzzles = new();
INotifyAttack[] notifyAttacksForUncloak;

public AttackGarrisonedSP(Actor self, AttackGarrisonedSPInfo info)
: base(self, info)
Expand All @@ -60,6 +61,15 @@ public AttackGarrisonedSP(Actor self, AttackGarrisonedSPInfo info)
coords = Exts.Lazy(() => self.Trait<BodyOrientation>());
}

protected override void Created(Actor self)
{
// HACK: we only reveal cloak at AttackGarrisonedSP, because we cannot really
// apply other actors' Armament to all the INotifyAttack belongs to transport, which
// will leads to crash or bug.
notifyAttacksForUncloak = self.TraitsImplementing<INotifyAttack>().Where(t => t is Cloak).ToArray();
base.Created(self);
}

protected override Func<IEnumerable<Armament>> InitializeGetArmaments(Actor self)
{
return () =>
Expand Down Expand Up @@ -114,27 +124,27 @@ public override void DoAttack(Actor self, in Target target)
var pos = self.CenterPosition;
var targetedPosition = GetTargetPosition(pos, target);
var targetYaw = (targetedPosition - pos).Yaw;
var hasNotifiedAttack = false;

var passengers = ports.Keys;
foreach (var pass in passengers)
foreach (var seat in ports.Keys)
{
var port = ports[pass];
var port = ports[seat];

foreach (var arm in port.Armaments)
{
if (arm.IsTraitDisabled)
continue;

port.PaxFacing.Facing = targetYaw;
port.PaxPos.SetCenterPosition(pass, pos + PortOffset(self, port));
port.PaxPos.SetCenterPosition(seat, pos + PortOffset(self, port));

if (!arm.CheckFire(pass, facing, target, true))
if (!arm.CheckFire(seat, facing, target, true))
continue;

if (arm.Info.MuzzleSequence != null)
{
// Muzzle facing is fixed once the firing starts
var muzzleAnim = new Animation(self.World, port.PaxRender.GetImage(pass), () => targetYaw);
var muzzleAnim = new Animation(self.World, port.PaxRender.GetImage(seat), () => targetYaw);
var sequence = arm.Info.MuzzleSequence;
var muzzleFlash = new AnimationWithOffset(muzzleAnim,
() => PortOffset(self, port),
Expand All @@ -144,6 +154,13 @@ public override void DoAttack(Actor self, in Target target)
muzzles[muzzleFlash] = arm.Info.MuzzlePalette;
muzzleAnim.PlayThen(sequence, () => muzzles.Remove(muzzleFlash));
}

if (!hasNotifiedAttack)
{
hasNotifiedAttack = true;
foreach (var npa in notifyAttacksForUncloak)
npa.Attacking(self, target, arm, null);
}
}
}
}
Expand Down

0 comments on commit 8485716

Please sign in to comment.