diff --git a/OpenRA.Game/Graphics/Animation.cs b/OpenRA.Game/Graphics/Animation.cs index f2f51e4918..6637e4b1ac 100644 --- a/OpenRA.Game/Graphics/Animation.cs +++ b/OpenRA.Game/Graphics/Animation.cs @@ -10,6 +10,8 @@ using System; using System.Collections.Generic; +using System.Linq; +using OpenRA.Support; namespace OpenRA.Graphics { @@ -199,5 +201,10 @@ namespace OpenRA.Graphics { return sequenceProvider.GetSequence(name, sequenceName); } + + public string GetRandomExistingSequence(string[] sequences, MersenneTwister random) + { + return sequences.Where(s => HasSequence(s)).RandomOrDefault(random); + } } } diff --git a/OpenRA.Mods.Common/Traits/Render/WithInfantryBody.cs b/OpenRA.Mods.Common/Traits/Render/WithInfantryBody.cs index 15067206ee..a2628bfa24 100644 --- a/OpenRA.Mods.Common/Traits/Render/WithInfantryBody.cs +++ b/OpenRA.Mods.Common/Traits/Render/WithInfantryBody.cs @@ -70,12 +70,19 @@ namespace OpenRA.Mods.Common.Traits DefaultAnimation = new Animation(init.World, rs.GetImage(self), RenderSprites.MakeFacingFunc(self)); rs.Add(new AnimationWithOffset(DefaultAnimation, null, () => IsTraitDisabled)); + PlayStandAnimation(self); - DefaultAnimation.PlayFetchIndex(NormalizeInfantrySequence(init.Self, info.StandSequences.Random(Game.CosmeticRandom)), () => 0); state = AnimationState.Waiting; move = init.Self.Trait(); } + public void PlayStandAnimation(Actor self) + { + var sequence = DefaultAnimation.GetRandomExistingSequence(Info.StandSequences, Game.CosmeticRandom); + if (sequence != null) + DefaultAnimation.PlayFetchIndex(NormalizeInfantrySequence(self, sequence), () => 0); + } + public void Created(Actor self) { rsm = self.TraitOrDefault(); @@ -123,7 +130,7 @@ namespace OpenRA.Mods.Common.Traits if ((state == AnimationState.Moving || dirty) && !move.IsMoving) { state = AnimationState.Waiting; - DefaultAnimation.PlayFetchIndex(NormalizeInfantrySequence(self, Info.StandSequences.Random(Game.CosmeticRandom)), () => 0); + PlayStandAnimation(self); } else if ((state != AnimationState.Moving || dirty) && move.IsMoving) { @@ -138,7 +145,7 @@ namespace OpenRA.Mods.Common.Traits { if (state != AnimationState.Idle && state != AnimationState.IdleAnimating && state != AnimationState.Attacking) { - DefaultAnimation.PlayFetchIndex(NormalizeInfantrySequence(self, Info.StandSequences.Random(Game.CosmeticRandom)), () => 0); + PlayStandAnimation(self); state = AnimationState.Idle; if (Info.IdleSequences.Length > 0) @@ -156,14 +163,14 @@ namespace OpenRA.Mods.Common.Traits state = AnimationState.IdleAnimating; DefaultAnimation.PlayThen(idleSequence, () => { - DefaultAnimation.PlayRepeating(NormalizeInfantrySequence(self, Info.StandSequences.Random(Game.CosmeticRandom))); + PlayStandAnimation(self); state = AnimationState.Waiting; }); } } else { - DefaultAnimation.PlayRepeating(NormalizeInfantrySequence(self, Info.StandSequences.Random(Game.CosmeticRandom))); + PlayStandAnimation(self); state = AnimationState.Waiting; } } diff --git a/OpenRA.Mods.RA/Traits/Render/WithDisguisingInfantryBody.cs b/OpenRA.Mods.RA/Traits/Render/WithDisguisingInfantryBody.cs index 9362c10c76..b7366ec704 100644 --- a/OpenRA.Mods.RA/Traits/Render/WithDisguisingInfantryBody.cs +++ b/OpenRA.Mods.RA/Traits/Render/WithDisguisingInfantryBody.cs @@ -39,7 +39,9 @@ namespace OpenRA.Mods.RA.Traits if (disguise.AsSprite != intendedSprite) { intendedSprite = disguise.AsSprite; - DefaultAnimation.ChangeImage(intendedSprite ?? rs.GetImage(self), info.StandSequences.Random(Game.CosmeticRandom)); + var sequence = DefaultAnimation.GetRandomExistingSequence(info.StandSequences, Game.CosmeticRandom); + if (sequence != null) + DefaultAnimation.ChangeImage(intendedSprite ?? rs.GetImage(self), sequence); rs.UpdatePalette(); }