From db810105dae9bc574ad1e176cdbff2764d674bee Mon Sep 17 00:00:00 2001 From: Chris Forbes Date: Sun, 1 Nov 2009 14:18:58 +1300 Subject: [PATCH] slightly nicer infantry behavior --- OpenRa.Game/Graphics/Animation.cs | 7 ++++++ OpenRa.Game/MainWindow.cs | 1 + OpenRa.Game/Traits/InfantrySquad.cs | 33 ++++++++++++++++++----------- 3 files changed, 29 insertions(+), 12 deletions(-) diff --git a/OpenRa.Game/Graphics/Animation.cs b/OpenRa.Game/Graphics/Animation.cs index 55c98fc8c4..a1025befbe 100644 --- a/OpenRa.Game/Graphics/Animation.cs +++ b/OpenRa.Game/Graphics/Animation.cs @@ -43,6 +43,13 @@ namespace OpenRa.Game.Graphics PlayThen( sequenceName, () => PlayRepeating( sequenceName ) ); } + public void PlayRepeatingPreservingPosition(string sequenceName) + { + var f = frame; + PlayThen(sequenceName, () => PlayRepeating(sequenceName)); + frame = f % CurrentSequence.Length; + } + public void PlayThen( string sequenceName, Action after ) { backwards = false; diff --git a/OpenRa.Game/MainWindow.cs b/OpenRa.Game/MainWindow.cs index 98f88e07ea..a90b3c2e20 100755 --- a/OpenRa.Game/MainWindow.cs +++ b/OpenRa.Game/MainWindow.cs @@ -64,6 +64,7 @@ namespace OpenRa.Game Game.world.Add( new Actor( "jeep", Game.map.Offset + new int2( 9, 15 ), Game.players[ 1 ] ) ); Game.world.Add( new Actor( "3tnk", Game.map.Offset + new int2( 12, 7 ), Game.players[ 1 ] ) ); Game.world.Add(new Actor("ca", Game.map.Offset + new int2(40, 7), Game.players[1])); + Game.world.Add(new Actor("e1", Game.map.Offset + new int2(9, 13), Game.players[1])); sidebar = new Sidebar(renderer, Game.LocalPlayer); diff --git a/OpenRa.Game/Traits/InfantrySquad.cs b/OpenRa.Game/Traits/InfantrySquad.cs index 6f04a50d9f..ed22898f96 100644 --- a/OpenRa.Game/Traits/InfantrySquad.cs +++ b/OpenRa.Game/Traits/InfantrySquad.cs @@ -32,7 +32,7 @@ namespace OpenRa.Game.Traits { for (int i = 0; i < elements.Count; i++) elements[i].Tick( - self.CenterLocation.ToInt2() + elementOffsets[elements.Count][i]); + self.CenterLocation.ToInt2() + elementOffsets[elements.Count][i], self); } public IEnumerable> Render(Actor self) @@ -49,18 +49,27 @@ namespace OpenRa.Game.Traits public float2 location; string name; int facing = 128; + float speed; string currentSequence; + static int QuantizeFacingNicely(int facing, int n) + { + var step = 256 / n; + var a = facing; + return a / step; + } + void PlaySequence(string seq, bool isFacing) { if (currentSequence == seq) return; - currentSequence = seq; if (isFacing) - anim.PlayFetchIndex(seq, () => facing / (256 / anim.CurrentSequence.Length)); + anim.PlayFetchIndex(seq, () => QuantizeFacingNicely(facing, anim.CurrentSequence.Length)); else - anim.PlayRepeating(seq); + anim.PlayRepeatingPreservingPosition(seq); + + currentSequence = seq; } public Soldier(string type, int2 initialLocation) @@ -70,25 +79,25 @@ namespace OpenRa.Game.Traits anim.PlayFetchIndex("stand", () => facing / (256 / anim.CurrentSequence.Length) ); location = initialLocation; + speed = ((UnitInfo.InfantryInfo)Rules.UnitInfo[name]).Speed / 2; } - public void Tick( int2 desiredLocation ) + public void Tick( int2 desiredLocation, Actor self ) { anim.Tick(); var d = (desiredLocation - location); - var speed = ((UnitInfo.InfantryInfo)Rules.UnitInfo[name]).Speed / 2; - facing = Util.GetFacing(d, facing); + facing = self.traits.Get().facing; if (float2.WithinEpsilon(d, float2.Zero, .1f)) PlaySequence("stand", true); else - PlaySequence("run-" + (facing / 32), false); + PlaySequence("run-" + QuantizeFacingNicely(facing, 8), false); - if (d.Length > speed) - d = (d.Length / speed) * d; - - location += d; + if (d.Length <= speed) + location = desiredLocation; + else + location += (speed / d.Length) * d; } } }