Merge pull request #8663 from Mailaender/disguise-stand2-crash

Fixed a crash when disguising the spy as infantry without stand2
This commit is contained in:
abcdefg30
2015-07-13 13:39:19 +02:00
3 changed files with 22 additions and 6 deletions

View File

@@ -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);
}
}
}

View File

@@ -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<IMove>();
}
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<IRenderInfantrySequenceModifier>();
@@ -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;
}
}

View File

@@ -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();
}