git-svn-id: svn://svn.ijw.co.nz/svn/OpenRa@1313 993157c7-ee19-0410-b2c4-bb4e9862e678

This commit is contained in:
(no author)
2007-07-20 07:14:30 +00:00
parent db17062511
commit 434ec200c9
6 changed files with 95 additions and 28 deletions

View File

@@ -10,6 +10,7 @@ namespace OpenRa.Game
readonly string name; readonly string name;
Sequence currentSequence; Sequence currentSequence;
int frame = 0; int frame = 0;
bool tickAlways;
public Animation( string name ) public Animation( string name )
{ {
@@ -31,6 +32,7 @@ namespace OpenRa.Game
public void PlayThen( string sequenceName, MethodInvoker after ) public void PlayThen( string sequenceName, MethodInvoker after )
{ {
tickAlways = false;
currentSequence = SequenceProvider.GetSequence( name, sequenceName ); currentSequence = SequenceProvider.GetSequence( name, sequenceName );
frame = 0; frame = 0;
tickFunc = delegate tickFunc = delegate
@@ -45,17 +47,34 @@ 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; double timeUntilNextFrame;
Action<double> tickFunc; Action<double> tickFunc;
public void Tick( double t ) public void Tick( double t )
{
if( tickAlways )
tickFunc( t );
else
{ {
timeUntilNextFrame -= t; timeUntilNextFrame -= t;
while( timeUntilNextFrame <= 0 ) while( timeUntilNextFrame <= 0 )
{ {
tickFunc( t ); tickFunc( 40.0 / 1000.0 );
timeUntilNextFrame += ( 40.0 / 1000.0 ); // 25 fps == 40 ms timeUntilNextFrame += ( 40.0 / 1000.0 ); // 25 fps == 40 ms
} }
} }
} }
}
} }

View File

@@ -6,16 +6,59 @@ namespace OpenRa.Game
{ {
class Harvester : Unit class Harvester : Unit
{ {
static Sequence idle = SequenceProvider.GetSequence("harv", "idle"); public Harvester( int2 cell, int palette )
: base( "harv", cell, palette, new float2( 12, 12 ) )
public override Sprite[] CurrentImages
{ {
get { return new Sprite[] { idle.GetSprite(facing) }; }
} }
public Harvester(int2 cell, int palette) public override IOrder Order( int2 xy )
: base(cell, palette, new float2(12,12))
{ {
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();
}
} }
} }
} }

View File

@@ -9,10 +9,8 @@ namespace OpenRa.Game
{ {
class Mcv : Unit class Mcv : Unit
{ {
static Sequence sequence = SequenceProvider.GetSequence("mcv", "idle");
public Mcv( int2 location, int palette ) 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 ) public override IOrder Order( int2 xy )
{ {
if( ( fromCell == toCell || moveFraction == 0 ) && fromCell == xy ) if( ( fromCell == toCell || moveFraction == 0 ) && fromCell == xy )

View File

@@ -6,16 +6,9 @@ namespace OpenRa.Game
{ {
class Truck : Unit class Truck : Unit
{ {
static Sequence sequence = SequenceProvider.GetSequence("truk", "idle"); public Truck( int2 cell, int palette )
: base( "truk", cell, palette, float2.Zero )
public Truck(int2 cell, int palette)
: base(cell, palette, float2.Zero)
{ {
} }
public override Sprite[] CurrentImages
{
get { return new Sprite[] { sequence.GetSprite(facing) }; }
}
} }
} }

View File

@@ -6,6 +6,8 @@ namespace OpenRa.Game
{ {
abstract class Unit : Actor, ISelectable abstract class Unit : Actor, ISelectable
{ {
protected Animation animation;
protected int facing = 0; protected int facing = 0;
protected int2 fromCell, toCell; protected int2 fromCell, toCell;
protected int moveFraction, moveFractionTotal; protected int moveFraction, moveFractionTotal;
@@ -16,11 +18,14 @@ namespace OpenRa.Game
protected readonly float2 renderOffset; 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; fromCell = toCell = cell;
this.renderOffset = renderOffset; this.renderOffset = renderOffset;
this.palette = palette; this.palette = palette;
animation = new Animation( name );
animation.PlayFetchIndex( "idle", delegate { return facing; } );
} }
static float2[] fvecs = Util.MakeArray<float2>( 32, static float2[] fvecs = Util.MakeArray<float2>( 32,
@@ -51,6 +56,7 @@ namespace OpenRa.Game
public override void Tick( World world, double t ) public override void Tick( World world, double t )
{ {
animation.Tick( t );
if( currentOrder == null && nextOrder != null ) if( currentOrder == null && nextOrder != null )
{ {
currentOrder = nextOrder; currentOrder = nextOrder;
@@ -128,5 +134,10 @@ namespace OpenRa.Game
return ( location - renderOffset ).Round(); ; return ( location - renderOffset ).Round(); ;
} }
} }
public override Sprite[] CurrentImages
{
get { return animation.Images; }
}
} }
} }

View File

@@ -64,6 +64,14 @@
<unit name="harv"> <unit name="harv">
<sequence name="idle" start="0" length="32"/> <sequence name="idle" start="0" length="32"/>
<sequence name="harvest0" start="32" length="8"/>
<sequence name="harvest1" start="40" length="8"/>
<sequence name="harvest2" start="48" length="8"/>
<sequence name="harvest3" start="56" length="8"/>
<sequence name="harvest4" start="64" length="8"/>
<sequence name="harvest5" start="72" length="8"/>
<sequence name="harvest6" start="80" length="8"/>
<sequence name="harvest7" start="88" length="8"/>
</unit> </unit>
</sequences> </sequences>