Throw yaml exception if WithAttackAnimation has no assigned sprite body

...or too many assigned bodies.

Also, further simplify WithAttackAnimation code.
This commit is contained in:
reaperrr
2017-05-27 00:48:42 +02:00
committed by atlimit8
parent 5fb468922e
commit fef388834e

View File

@@ -38,13 +38,22 @@ namespace OpenRA.Mods.Common.Traits.Render
public readonly string BodyName = "body"; public readonly string BodyName = "body";
public override object Create(ActorInitializer init) { return new WithAttackAnimation(init, this); } public override object Create(ActorInitializer init) { return new WithAttackAnimation(init, this); }
public override void RulesetLoaded(Ruleset rules, ActorInfo ai)
{
var matches = ai.TraitInfos<WithSpriteBodyInfo>().Count(w => BodyName == w.Name);
if (matches != 1)
throw new YamlException("WithAttackAnimation needs exactly one sprite body with matching name.");
base.RulesetLoaded(rules, ai);
}
} }
public class WithAttackAnimation : ConditionalTrait<WithAttackAnimationInfo>, ITick, INotifyAttack public class WithAttackAnimation : ConditionalTrait<WithAttackAnimationInfo>, ITick, INotifyAttack
{ {
readonly AttackBase attack; readonly AttackBase attack;
readonly Armament armament; readonly Armament armament;
readonly WithSpriteBody[] wsbs; readonly WithSpriteBody wsb;
readonly bool noAimOrReloadAnim; readonly bool noAimOrReloadAnim;
int tick; int tick;
@@ -56,19 +65,15 @@ namespace OpenRA.Mods.Common.Traits.Render
attack = init.Self.Trait<AttackBase>(); attack = init.Self.Trait<AttackBase>();
armament = init.Self.TraitsImplementing<Armament>() armament = init.Self.TraitsImplementing<Armament>()
.Single(a => a.Info.Name == Info.Armament); .Single(a => a.Info.Name == Info.Armament);
wsbs = init.Self.TraitsImplementing<WithSpriteBody>().Where(w => Info.BodyName == w.Info.Name).ToArray(); wsb = init.Self.TraitsImplementing<WithSpriteBody>().First(w => w.Info.Name == Info.BodyName);
noAimOrReloadAnim = string.IsNullOrEmpty(Info.AimSequence) && string.IsNullOrEmpty(Info.ReloadPrefix); noAimOrReloadAnim = string.IsNullOrEmpty(Info.AimSequence) && string.IsNullOrEmpty(Info.ReloadPrefix);
} }
void PlayAttackAnimation(Actor self) void PlayAttackAnimation(Actor self)
{ {
if (!IsTraitDisabled && !string.IsNullOrEmpty(Info.AttackSequence)) if (!IsTraitDisabled && !wsb.IsTraitDisabled && !string.IsNullOrEmpty(Info.AttackSequence))
{ {
var wsb = wsbs.FirstOrDefault(Exts.IsTraitEnabled);
if (wsb == null)
return;
attackAnimPlaying = true; attackAnimPlaying = true;
wsb.PlayCustomAnimation(self, Info.AttackSequence, wsb.PlayCustomAnimation(self, Info.AttackSequence,
() => { wsb.CancelCustomAnimation(self); attackAnimPlaying = false; }); () => { wsb.CancelCustomAnimation(self); attackAnimPlaying = false; });
@@ -102,11 +107,7 @@ namespace OpenRA.Mods.Common.Traits.Render
if (Info.Delay > 0 && --tick == 0) if (Info.Delay > 0 && --tick == 0)
PlayAttackAnimation(self); PlayAttackAnimation(self);
if (IsTraitDisabled || noAimOrReloadAnim || attackAnimPlaying) if (IsTraitDisabled || noAimOrReloadAnim || attackAnimPlaying || wsb.IsTraitDisabled)
return;
var wsb = wsbs.FirstOrDefault(Exts.IsTraitEnabled);
if (wsb == null)
return; return;
var sequence = wsb.Info.Sequence; var sequence = wsb.Info.Sequence;