Make With(Turret)AimAnimation support multiple AttackBase traits

This commit is contained in:
abcdefg30
2022-06-08 22:18:58 +02:00
committed by Matthias Mailänder
parent ee3c54b572
commit f2eb42a4b2
2 changed files with 37 additions and 18 deletions

View File

@@ -41,25 +41,35 @@ namespace OpenRA.Mods.Common.Traits.Render
public class WithAimAnimation : ConditionalTrait<WithAimAnimationInfo>, INotifyAiming
{
readonly AttackBase attack;
readonly AttackBase[] attackBases;
readonly WithSpriteBody wsb;
public WithAimAnimation(ActorInitializer init, WithAimAnimationInfo info)
: base(info)
{
attack = init.Self.Trait<AttackBase>();
attackBases = init.Self.TraitsImplementing<AttackBase>().ToArray();
wsb = init.Self.TraitsImplementing<WithSpriteBody>().First(w => w.Info.Name == Info.Body);
}
protected void UpdateSequence()
void UpdateSequence(bool isAiming)
{
var seq = !IsTraitDisabled && attack.IsAiming ? Info.Sequence : wsb.Info.Sequence;
var seq = !IsTraitDisabled && isAiming ? Info.Sequence : wsb.Info.Sequence;
wsb.DefaultAnimation.ReplaceAnim(seq);
}
void INotifyAiming.StartedAiming(Actor self, AttackBase ab) { UpdateSequence(); }
void INotifyAiming.StoppedAiming(Actor self, AttackBase ab) { UpdateSequence(); }
protected override void TraitEnabled(Actor self) { UpdateSequence(); }
protected override void TraitDisabled(Actor self) { UpdateSequence(); }
void INotifyAiming.StartedAiming(Actor self, AttackBase ab)
{
// We know that at least one AttackBase is aiming
UpdateSequence(true);
}
void INotifyAiming.StoppedAiming(Actor self, AttackBase ab) { UpdateSequence(attackBases.Any(a => a.IsAiming)); }
protected override void TraitEnabled(Actor self) { UpdateSequence(attackBases.Any(a => a.IsAiming)); }
protected override void TraitDisabled(Actor self)
{
// Stop regardless of any aiming AttackBases
UpdateSequence(false);
}
}
}

View File

@@ -41,26 +41,35 @@ namespace OpenRA.Mods.Common.Traits.Render
public class WithTurretAimAnimation : ConditionalTrait<WithTurretAimAnimationInfo>, INotifyAiming
{
readonly AttackBase attack;
readonly AttackBase[] attackBases;
readonly WithSpriteTurret wst;
public WithTurretAimAnimation(ActorInitializer init, WithTurretAimAnimationInfo info)
: base(info)
{
attack = init.Self.Trait<AttackBase>();
wst = init.Self.TraitsImplementing<WithSpriteTurret>()
.Single(st => st.Info.Turret == info.Turret);
attackBases = init.Self.TraitsImplementing<AttackBase>().ToArray();
wst = init.Self.TraitsImplementing<WithSpriteTurret>().Single(st => st.Info.Turret == info.Turret);
}
protected void UpdateSequence()
void UpdateSequence(bool isAiming)
{
var seq = !IsTraitDisabled && attack.IsAiming ? Info.Sequence : wst.Info.Sequence;
var seq = !IsTraitDisabled && isAiming ? Info.Sequence : wst.Info.Sequence;
wst.DefaultAnimation.ReplaceAnim(seq);
}
void INotifyAiming.StartedAiming(Actor self, AttackBase ab) { UpdateSequence(); }
void INotifyAiming.StoppedAiming(Actor self, AttackBase ab) { UpdateSequence(); }
protected override void TraitEnabled(Actor self) { UpdateSequence(); }
protected override void TraitDisabled(Actor self) { UpdateSequence(); }
void INotifyAiming.StartedAiming(Actor self, AttackBase ab)
{
// We know that at least one AttackBase is aiming
UpdateSequence(true);
}
void INotifyAiming.StoppedAiming(Actor self, AttackBase ab) { UpdateSequence(attackBases.Any(a => a.IsAiming)); }
protected override void TraitEnabled(Actor self) { UpdateSequence(attackBases.Any(a => a.IsAiming)); }
protected override void TraitDisabled(Actor self)
{
// Stop regardless of any aiming AttackBases
UpdateSequence(false);
}
}
}