splitting traits into own files

This commit is contained in:
Chris Forbes
2009-10-13 21:11:21 +13:00
parent 9af7b09e1d
commit 3b1558d678
15 changed files with 435 additions and 328 deletions

View File

@@ -154,332 +154,4 @@ namespace OpenRa.Game
}
}
}
namespace Traits
{
interface ITick
{
void Tick( Actor self, Game game, int dt );
}
interface IRender
{
IEnumerable<Pair<Sprite, float2>> Render( Actor self );
}
interface IOrder
{
Order Order( Actor self, Game game, int2 xy );
}
abstract class RenderSimple : IRender, ITick
{
public Animation anim;
public RenderSimple( Actor self )
{
anim = new Animation( self.unitInfo.Image ?? self.unitInfo.Name );
}
public abstract IEnumerable<Pair<Sprite, float2>> Render( Actor self );
public virtual void Tick( Actor self, Game game, int dt )
{
anim.Tick( dt );
}
}
class RenderBuilding : RenderSimple
{
public RenderBuilding( Actor self )
: base( self )
{
anim.PlayThen( "make", () => anim.PlayRepeating( "idle" ) );
}
public override IEnumerable<Pair<Sprite, float2>> Render( Actor self )
{
yield return Pair.New( anim.Image, 24f * (float2)self.Location );
}
}
class RenderBuildingTurreted : RenderBuilding
{
public RenderBuildingTurreted( Actor self )
: base( self )
{
anim.PlayThen( "make", () => anim.PlayFetchIndex( "idle", () => self.traits.Get<Turreted>().turretFacing ) );
}
}
class RenderBuildingOre : RenderBuilding
{
public RenderBuildingOre(Actor self)
: base(self)
{
anim.PlayThen("make", () => anim.PlayFetchIndex("idle", () => (int)(5 * self.Owner.GetSiloFullness())));
}
}
class RenderWarFactory : RenderBuilding
{
public Animation roof;
bool doneBuilding;
public RenderWarFactory( Actor self )
: base( self )
{
roof = new Animation( self.unitInfo.Image ?? self.unitInfo.Name );
anim.PlayThen( "make", () =>
{
doneBuilding = true;
anim.Play( "idle" );
roof.Play( "idle-top" );
} );
}
public override IEnumerable<Pair<Sprite, float2>> Render( Actor self )
{
yield return Pair.New( anim.Image, 24f * (float2)self.Location );
if( doneBuilding )
yield return Pair.New( roof.Image, 24f * (float2)self.Location );
}
public override void Tick( Actor self, Game game, int dt )
{
base.Tick( self, game, dt );
roof.Tick( dt );
}
}
class RenderUnit : RenderSimple
{
public RenderUnit( Actor self )
: base( self )
{
anim.PlayFetchIndex( "idle", () => self.traits.Get<Mobile>().facing );
}
protected static Pair<Sprite, float2> Centered( Sprite s, float2 location )
{
var loc = location - 0.5f * s.size;
return Pair.New( s, loc.Round() );
}
public override IEnumerable<Pair<Sprite, float2>> Render( Actor self )
{
var mobile = self.traits.Get<Mobile>();
float fraction = ( mobile.moveFraction > 0 ) ? (float)mobile.moveFraction / mobile.moveFractionTotal : 0f;
var centerLocation = new float2( 12, 12 ) + 24 * float2.Lerp( mobile.fromCell, mobile.toCell, fraction );
yield return Centered( anim.Image, centerLocation );
}
}
class RenderUnitTurreted : RenderUnit
{
public Animation turretAnim;
public RenderUnitTurreted( Actor self )
: base( self )
{
turretAnim = new Animation( self.unitInfo.Name );
turretAnim.PlayFetchIndex( "turret", () => self.traits.Get<Turreted>().turretFacing );
}
public override IEnumerable<Pair<Sprite, float2>> Render( Actor self )
{
var mobile = self.traits.Get<Mobile>();
yield return Centered( anim.Image, self.CenterLocation );
yield return Centered( turretAnim.Image, self.CenterLocation );
}
public override void Tick( Actor self, Game game, int dt )
{
base.Tick( self, game, dt );
turretAnim.Tick( dt );
}
}
class Mobile : ITick, IOrder
{
public Actor self;
public int2 fromCell, destination;
public int2 toCell { get { return self.Location; } }
public int moveFraction, moveFractionTotal;
public int facing;
public Mobile( Actor self )
{
this.self = self;
fromCell = destination = self.Location;
}
public bool Turn( int desiredFacing )
{
if( facing == desiredFacing )
return false;
int df = ( desiredFacing - facing + 32 ) % 32;
facing = ( facing + ( df > 16 ? 31 : 1 ) ) % 32;
return true;
}
static float2[] fvecs = Util.MakeArray<float2>( 32,
i => -float2.FromAngle( i / 16.0f * (float)Math.PI ) * new float2( 1f, 1.3f ) );
int GetFacing( float2 d )
{
if( float2.WithinEpsilon( d, float2.Zero, 0.001f ) )
return facing;
int highest = -1;
float highestDot = -1.0f;
for( int i = 0 ; i < fvecs.Length ; i++ )
{
float dot = float2.Dot( fvecs[ i ], d );
if( dot > highestDot )
{
highestDot = dot;
highest = i;
}
}
return highest;
}
void UpdateCenterLocation()
{
float fraction = ( moveFraction > 0 ) ? (float)moveFraction / moveFractionTotal : 0f;
self.CenterLocation = new float2( 12, 12 ) + 24 * float2.Lerp( fromCell, toCell, fraction );
}
public void Tick( Actor self, Game game, int dt )
{
Move( self, game, dt );
UpdateCenterLocation();
}
void Move( Actor self, Game game, int dt )
{
if( fromCell != toCell )
{
if( Turn( GetFacing( toCell - fromCell ) ) )
return;
moveFraction += dt * ( (UnitInfo.MobileInfo)self.unitInfo ).Speed;
}
if( moveFraction < moveFractionTotal )
return;
moveFraction = 0;
moveFractionTotal = 0;
fromCell = toCell;
if( destination == toCell )
return;
List<int2> res = game.pathFinder.FindUnitPath( toCell, PathFinder.DefaultEstimator( destination ) );
if( res.Count != 0 )
{
self.Location = res[ res.Count - 1 ];
int2 dir = toCell - fromCell;
moveFractionTotal = ( dir.X != 0 && dir.Y != 0 ) ? 2500 : 2000;
}
else
destination = toCell;
}
public Order Order( Actor self, Game game, int2 xy )
{
if( xy != toCell )
return new MoveOrder( self, xy );
return null;
}
}
class McvDeploy : IOrder, ITick
{
public bool Deploying;
public McvDeploy( Actor self )
{
}
public Order Order( Actor self, Game game, int2 xy )
{
// TODO: check that there's enough space at the destination.
if( xy == self.Location )
return new DeployMcvOrder( self );
return null;
}
public void Tick( Actor self, Game game, int dt )
{
if( !Deploying )
return;
if( self.traits.Get<Mobile>().Turn( 12 ) )
return;
game.world.AddFrameEndTask( _ =>
{
game.world.Remove( self );
game.world.Add( new Actor( "fact", self.Location - new int2( 1, 1 ), self.Owner ) );
} );
}
}
class Turreted
: ITick // temporary.
{
public int turretFacing = 24;
public Turreted( Actor self )
{
}
// temporary.
public void Tick( Actor self, Game game, int dt )
{
turretFacing = ( turretFacing + 1 ) % 32;
}
}
class Building : ITick
{
public Building( Actor self )
{
}
bool first = true;
public void Tick( Actor self, Game game, int dt )
{
if( first && self.Owner == game.LocalPlayer )
{
self.Owner.TechTree.Build( self.unitInfo.Name, true );
self.CenterLocation = 24 * (float2)self.Location + 0.5f * self.SelectedSize;
}
first = false;
}
}
class Tree : IRender
{
Sprite Image;
public Tree( Sprite treeImage )
{
Image = treeImage;
}
public IEnumerable<Pair<Sprite, float2>> Render( Actor self )
{
yield return Pair.New( Image, 24 * (float2)self.Location );
}
}
}
}