git-svn-id: svn://svn.ijw.co.nz/svn/OpenRa@1302 993157c7-ee19-0410-b2c4-bb4e9862e678
This commit is contained in:
@@ -7,122 +7,13 @@ using BluntDirectX.Direct3D;
|
|||||||
|
|
||||||
namespace OpenRa.Game
|
namespace OpenRa.Game
|
||||||
{
|
{
|
||||||
delegate void TickFunc(World world, double t);
|
class Mcv : Unit
|
||||||
|
|
||||||
class Mcv : Actor, ISelectable
|
|
||||||
{
|
{
|
||||||
static Range<int> mcvRange = UnitSheetBuilder.GetUnit( "mcv" );
|
static Range<int> mcvRange = UnitSheetBuilder.GetUnit( "mcv" );
|
||||||
|
|
||||||
int facing = 0;
|
public Mcv( int2 location, int palette )
|
||||||
int2 fromCell, toCell;
|
: base( location, palette )
|
||||||
int moveFraction, moveFractionTotal;
|
|
||||||
|
|
||||||
TickFunc currentOrder, nextOrder;
|
|
||||||
|
|
||||||
public Mcv(int2 cell, int palette)
|
|
||||||
{
|
{
|
||||||
fromCell = toCell = cell;
|
|
||||||
renderLocation = (cell * 24).ToFloat2() - new float2(12, 12);
|
|
||||||
this.palette = palette;
|
|
||||||
}
|
|
||||||
|
|
||||||
static float2[] fvecs = Util.MakeArray<float2>(32,
|
|
||||||
delegate(int i) { return -float2.FromAngle(i / 16.0f * (float)Math.PI); });
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
public override Sprite[] CurrentImages
|
|
||||||
{
|
|
||||||
get { return new Sprite[] { UnitSheetBuilder.sprites[facing + mcvRange.Start] }; }
|
|
||||||
}
|
|
||||||
|
|
||||||
const int Speed = 6;
|
|
||||||
|
|
||||||
public override void Tick( World world, double t )
|
|
||||||
{
|
|
||||||
if( currentOrder == null && nextOrder != null )
|
|
||||||
{
|
|
||||||
currentOrder = nextOrder;
|
|
||||||
nextOrder = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
if( currentOrder != null )
|
|
||||||
currentOrder( world, t );
|
|
||||||
}
|
|
||||||
|
|
||||||
public void AcceptMoveOrder( int2 destination )
|
|
||||||
{
|
|
||||||
nextOrder = delegate( World world, double t )
|
|
||||||
{
|
|
||||||
int speed = (int)( t * ( Speed * 100 ) );
|
|
||||||
|
|
||||||
if( nextOrder != null )
|
|
||||||
destination = toCell;
|
|
||||||
|
|
||||||
int desiredFacing = GetFacing( ( toCell - fromCell ).ToFloat2() );
|
|
||||||
if( facing != desiredFacing )
|
|
||||||
Turn(desiredFacing);
|
|
||||||
else
|
|
||||||
{
|
|
||||||
moveFraction += speed;
|
|
||||||
if( moveFraction >= moveFractionTotal )
|
|
||||||
{
|
|
||||||
moveFraction = 0;
|
|
||||||
moveFractionTotal = 0;
|
|
||||||
fromCell = toCell;
|
|
||||||
|
|
||||||
if( toCell == destination )
|
|
||||||
currentOrder = null;
|
|
||||||
else
|
|
||||||
{
|
|
||||||
List<int2> res = PathFinder.Instance.FindUnitPath( world, this, destination );
|
|
||||||
if( res.Count != 0 )
|
|
||||||
{
|
|
||||||
toCell = res[ res.Count - 1 ];
|
|
||||||
|
|
||||||
int2 dir = toCell - fromCell;
|
|
||||||
moveFractionTotal = ( dir.X != 0 && dir.Y != 0 ) ? 250 : 200;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
destination = toCell;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
float2 location;
|
|
||||||
if( moveFraction > 0 )
|
|
||||||
location = 24 * float2.Lerp(fromCell.ToFloat2(), toCell.ToFloat2(),
|
|
||||||
(float)moveFraction / moveFractionTotal);
|
|
||||||
else
|
|
||||||
location = 24 * fromCell.ToFloat2();
|
|
||||||
|
|
||||||
renderLocation = (location - new float2( 12, 12 )).Round();
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
void Turn(int desiredFacing)
|
|
||||||
{
|
|
||||||
int df = (desiredFacing - facing + 32) % 32;
|
|
||||||
facing = (facing + (df > 16 ? 31 : 1)) % 32;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AcceptDeployOrder()
|
public void AcceptDeployOrder()
|
||||||
@@ -145,17 +36,17 @@ namespace OpenRa.Game
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public IOrder Order( int2 xy )
|
public override Sprite[] CurrentImages
|
||||||
|
{
|
||||||
|
get { return new Sprite[] { UnitSheetBuilder.sprites[ facing + mcvRange.Start ] }; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public override IOrder Order( int2 xy )
|
||||||
{
|
{
|
||||||
if( ( fromCell == toCell || moveFraction == 0 ) && fromCell == xy )
|
if( ( fromCell == toCell || moveFraction == 0 ) && fromCell == xy )
|
||||||
return new DeployMcvOrder( this );
|
return new DeployMcvOrder( this );
|
||||||
else
|
|
||||||
return new MoveOrder( this, xy );
|
|
||||||
}
|
|
||||||
|
|
||||||
public int2 Location
|
return base.Order( xy );
|
||||||
{
|
|
||||||
get { return toCell; }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,10 +11,10 @@ namespace OpenRa.Game
|
|||||||
|
|
||||||
class MoveOrder : IOrder
|
class MoveOrder : IOrder
|
||||||
{
|
{
|
||||||
public readonly Mcv Unit;
|
public readonly Unit Unit;
|
||||||
public readonly int2 Destination;
|
public readonly int2 Destination;
|
||||||
|
|
||||||
public MoveOrder(Mcv unit, int2 destination)
|
public MoveOrder(Unit unit, int2 destination)
|
||||||
{
|
{
|
||||||
this.Unit = unit;
|
this.Unit = unit;
|
||||||
this.Destination = destination;
|
this.Destination = destination;
|
||||||
@@ -28,16 +28,16 @@ namespace OpenRa.Game
|
|||||||
|
|
||||||
class DeployMcvOrder : IOrder
|
class DeployMcvOrder : IOrder
|
||||||
{
|
{
|
||||||
public Mcv Unit;
|
public Mcv Mcv;
|
||||||
|
|
||||||
public DeployMcvOrder( Mcv unit )
|
public DeployMcvOrder( Mcv mcv )
|
||||||
{
|
{
|
||||||
this.Unit = unit;
|
this.Mcv = mcv;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Apply()
|
public void Apply()
|
||||||
{
|
{
|
||||||
Unit.AcceptDeployOrder();
|
Mcv.AcceptDeployOrder();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -72,6 +72,7 @@
|
|||||||
<Compile Include="TerrainRenderer.cs" />
|
<Compile Include="TerrainRenderer.cs" />
|
||||||
<Compile Include="Tree.cs" />
|
<Compile Include="Tree.cs" />
|
||||||
<Compile Include="TreeCache.cs" />
|
<Compile Include="TreeCache.cs" />
|
||||||
|
<Compile Include="Unit.cs" />
|
||||||
<Compile Include="UnitSheetBuilder.cs" />
|
<Compile Include="UnitSheetBuilder.cs" />
|
||||||
<Compile Include="Util.cs" />
|
<Compile Include="Util.cs" />
|
||||||
<Compile Include="Vertex.cs" />
|
<Compile Include="Vertex.cs" />
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ namespace OpenRa.Game
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<int2> FindUnitPath( World world, Mcv unit, int2 destination )
|
public List<int2> FindUnitPath( World world, Unit unit, int2 destination )
|
||||||
{
|
{
|
||||||
int2 offset = new int2( map.XOffset, map.YOffset );
|
int2 offset = new int2( map.XOffset, map.YOffset );
|
||||||
|
|
||||||
|
|||||||
131
OpenRa.Game/Unit.cs
Normal file
131
OpenRa.Game/Unit.cs
Normal file
@@ -0,0 +1,131 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace OpenRa.Game
|
||||||
|
{
|
||||||
|
abstract class Unit : Actor, ISelectable
|
||||||
|
{
|
||||||
|
protected int facing = 0;
|
||||||
|
protected int2 fromCell, toCell;
|
||||||
|
protected int moveFraction, moveFractionTotal;
|
||||||
|
|
||||||
|
protected delegate void TickFunc( World world, double t );
|
||||||
|
protected TickFunc currentOrder = null;
|
||||||
|
protected TickFunc nextOrder = null;
|
||||||
|
|
||||||
|
public Unit( int2 cell, int palette )
|
||||||
|
{
|
||||||
|
fromCell = toCell = cell;
|
||||||
|
// HACK: display the mcv centered in it's cell;
|
||||||
|
renderLocation = ( cell * 24 ).ToFloat2() - new float2( 12, 12 );
|
||||||
|
this.palette = palette;
|
||||||
|
}
|
||||||
|
|
||||||
|
static float2[] fvecs = Util.MakeArray<float2>( 32,
|
||||||
|
delegate( int i ) { return -float2.FromAngle( i / 16.0f * (float)Math.PI ); } );
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
const int Speed = 6;
|
||||||
|
|
||||||
|
public override void Tick( World world, double t )
|
||||||
|
{
|
||||||
|
if( currentOrder == null && nextOrder != null )
|
||||||
|
{
|
||||||
|
currentOrder = nextOrder;
|
||||||
|
nextOrder = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if( currentOrder != null )
|
||||||
|
currentOrder( world, t );
|
||||||
|
}
|
||||||
|
|
||||||
|
public void AcceptMoveOrder( int2 destination )
|
||||||
|
{
|
||||||
|
nextOrder = delegate( World world, double t )
|
||||||
|
{
|
||||||
|
int speed = (int)( t * ( Speed * 100 ) );
|
||||||
|
|
||||||
|
if( nextOrder != null )
|
||||||
|
destination = toCell;
|
||||||
|
|
||||||
|
int desiredFacing = GetFacing( ( toCell - fromCell ).ToFloat2() );
|
||||||
|
if( facing != desiredFacing )
|
||||||
|
Turn( desiredFacing );
|
||||||
|
else
|
||||||
|
{
|
||||||
|
moveFraction += speed;
|
||||||
|
if( moveFraction >= moveFractionTotal )
|
||||||
|
{
|
||||||
|
moveFraction = 0;
|
||||||
|
moveFractionTotal = 0;
|
||||||
|
fromCell = toCell;
|
||||||
|
|
||||||
|
if( toCell == destination )
|
||||||
|
currentOrder = null;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
List<int2> res = PathFinder.Instance.FindUnitPath( world, this, destination );
|
||||||
|
if( res.Count != 0 )
|
||||||
|
{
|
||||||
|
toCell = res[ res.Count - 1 ];
|
||||||
|
|
||||||
|
int2 dir = toCell - fromCell;
|
||||||
|
moveFractionTotal = ( dir.X != 0 && dir.Y != 0 ) ? 250 : 200;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
destination = toCell;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
float2 location;
|
||||||
|
if( moveFraction > 0 )
|
||||||
|
location = 24 * float2.Lerp( fromCell.ToFloat2(), toCell.ToFloat2(),
|
||||||
|
(float)moveFraction / moveFractionTotal );
|
||||||
|
else
|
||||||
|
location = 24 * fromCell.ToFloat2();
|
||||||
|
|
||||||
|
renderLocation = location - new float2( 12, 12 ); // HACK: center mcv in it's cell
|
||||||
|
|
||||||
|
renderLocation = renderLocation.Round();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void Turn( int desiredFacing )
|
||||||
|
{
|
||||||
|
int df = ( desiredFacing - facing + 32 ) % 32;
|
||||||
|
facing = ( facing + ( df > 16 ? 31 : 1 ) ) % 32;
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual IOrder Order( int2 xy )
|
||||||
|
{
|
||||||
|
return new MoveOrder( this, xy );
|
||||||
|
}
|
||||||
|
|
||||||
|
public int2 Location
|
||||||
|
{
|
||||||
|
get { return toCell; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user