diff --git a/OpenRa.Game/Animation.cs b/OpenRa.Game/Animation.cs index 31a4e1ca2a..715fc40853 100644 --- a/OpenRa.Game/Animation.cs +++ b/OpenRa.Game/Animation.cs @@ -10,6 +10,7 @@ namespace OpenRa.Game readonly string name; Sequence currentSequence; int frame = 0; + bool tickAlways; public Animation( string name ) { @@ -31,6 +32,7 @@ namespace OpenRa.Game public void PlayThen( string sequenceName, MethodInvoker after ) { + tickAlways = false; currentSequence = SequenceProvider.GetSequence( name, sequenceName ); frame = 0; tickFunc = delegate @@ -45,16 +47,33 @@ namespace OpenRa.Game }; } + public delegate int IndexFetchFunc(); + public void PlayFetchIndex( string sequenceName, IndexFetchFunc func ) + { + tickAlways = true; + currentSequence = SequenceProvider.GetSequence( name, sequenceName ); + frame = func(); + tickFunc = delegate + { + frame = func(); + }; + } + double timeUntilNextFrame; Action tickFunc; public void Tick( double t ) { - timeUntilNextFrame -= t; - while( timeUntilNextFrame <= 0 ) - { + if( tickAlways ) tickFunc( t ); - timeUntilNextFrame += ( 40.0 / 1000.0 ); // 25 fps == 40 ms + else + { + timeUntilNextFrame -= t; + while( timeUntilNextFrame <= 0 ) + { + tickFunc( 40.0 / 1000.0 ); + timeUntilNextFrame += ( 40.0 / 1000.0 ); // 25 fps == 40 ms + } } } } diff --git a/OpenRa.Game/Harvester.cs b/OpenRa.Game/Harvester.cs index 692055fad6..88c6c372bd 100644 --- a/OpenRa.Game/Harvester.cs +++ b/OpenRa.Game/Harvester.cs @@ -6,16 +6,59 @@ namespace OpenRa.Game { class Harvester : Unit { - static Sequence idle = SequenceProvider.GetSequence("harv", "idle"); - - public override Sprite[] CurrentImages + public Harvester( int2 cell, int palette ) + : base( "harv", cell, palette, new float2( 12, 12 ) ) { - get { return new Sprite[] { idle.GetSprite(facing) }; } } - public Harvester(int2 cell, int palette) - : base(cell, palette, new float2(12,12)) + public override IOrder Order( int2 xy ) { + if( ( fromCell == toCell || moveFraction == 0 ) && fromCell == xy ) + return new HarvestOrder( this ); + return base.Order( xy ); + } + + void AcceptHarvestOrder() + { + TickFunc order = null; + order = nextOrder = delegate + { + // TODO: check that there's actually some ore in this cell :) + + // face in one of the 8 directions + int desiredFacing = ( facing + 1 ) & 28; + if( facing != desiredFacing ) + { + Turn( desiredFacing ); + return; + } + + currentOrder = delegate { }; + if( nextOrder == null ) + nextOrder = order; + + string sequenceName = string.Format( "harvest{0}", facing / 4 ); + animation.PlayThen( sequenceName, delegate + { + currentOrder = null; + animation.PlayFetchIndex( "idle", delegate { return facing; } ); + } ); + }; + } + + public class HarvestOrder : IOrder + { + Harvester harvester; + + public HarvestOrder( Harvester harv ) + { + harvester = harv; + } + + public void Apply() + { + harvester.AcceptHarvestOrder(); + } } } } diff --git a/OpenRa.Game/Mcv.cs b/OpenRa.Game/Mcv.cs index ab71df17f4..503c141caa 100644 --- a/OpenRa.Game/Mcv.cs +++ b/OpenRa.Game/Mcv.cs @@ -9,10 +9,8 @@ namespace OpenRa.Game { class Mcv : Unit { - static Sequence sequence = SequenceProvider.GetSequence("mcv", "idle"); - public Mcv( int2 location, int palette ) - : base(location, palette, new float2(12, 12)) + : base( "mcv", location, palette, new float2( 12, 12 ) ) { } @@ -39,11 +37,6 @@ namespace OpenRa.Game }; } - public override Sprite[] CurrentImages - { - get { return new Sprite[] { sequence.GetSprite(facing) }; } - } - public override IOrder Order( int2 xy ) { if( ( fromCell == toCell || moveFraction == 0 ) && fromCell == xy ) diff --git a/OpenRa.Game/Truck.cs b/OpenRa.Game/Truck.cs index cd2611d659..49dcd56a9b 100644 --- a/OpenRa.Game/Truck.cs +++ b/OpenRa.Game/Truck.cs @@ -6,16 +6,9 @@ namespace OpenRa.Game { class Truck : Unit { - static Sequence sequence = SequenceProvider.GetSequence("truk", "idle"); - - public Truck(int2 cell, int palette) - : base(cell, palette, float2.Zero) + public Truck( int2 cell, int palette ) + : base( "truk", cell, palette, float2.Zero ) { } - - public override Sprite[] CurrentImages - { - get { return new Sprite[] { sequence.GetSprite(facing) }; } - } } } diff --git a/OpenRa.Game/Unit.cs b/OpenRa.Game/Unit.cs index db051ca093..ce661cfd6f 100644 --- a/OpenRa.Game/Unit.cs +++ b/OpenRa.Game/Unit.cs @@ -6,6 +6,8 @@ namespace OpenRa.Game { abstract class Unit : Actor, ISelectable { + protected Animation animation; + protected int facing = 0; protected int2 fromCell, toCell; protected int moveFraction, moveFractionTotal; @@ -16,11 +18,14 @@ namespace OpenRa.Game protected readonly float2 renderOffset; - public Unit( int2 cell, int palette, float2 renderOffset ) + public Unit( string name, int2 cell, int palette, float2 renderOffset ) { fromCell = toCell = cell; this.renderOffset = renderOffset; this.palette = palette; + + animation = new Animation( name ); + animation.PlayFetchIndex( "idle", delegate { return facing; } ); } static float2[] fvecs = Util.MakeArray( 32, @@ -51,6 +56,7 @@ namespace OpenRa.Game public override void Tick( World world, double t ) { + animation.Tick( t ); if( currentOrder == null && nextOrder != null ) { currentOrder = nextOrder; @@ -128,5 +134,10 @@ namespace OpenRa.Game return ( location - renderOffset ).Round(); ; } } + + public override Sprite[] CurrentImages + { + get { return animation.Images; } + } } } diff --git a/sequences.xml b/sequences.xml index b04153c7fb..7a5777bf26 100644 --- a/sequences.xml +++ b/sequences.xml @@ -64,6 +64,14 @@ + + + + + + + +