Refactored Mobile.MoveTo
This commit is contained in:
@@ -121,18 +121,14 @@ namespace OpenRa.Game.Traits
|
||||
int2 destination;
|
||||
List<int2> path;
|
||||
|
||||
int moveFraction, moveFractionTotal;
|
||||
float2 from, to;
|
||||
int fromFacing, toFacing;
|
||||
|
||||
Func<Actor, Mobile, bool> OnComplete;
|
||||
MovePart move;
|
||||
|
||||
public MoveTo( int2 destination )
|
||||
{
|
||||
this.destination = destination;
|
||||
}
|
||||
|
||||
bool CanEnterCell(int2 c, Actor self)
|
||||
static bool CanEnterCell(int2 c, Actor self)
|
||||
{
|
||||
var u = Game.UnitInfluence.GetUnitAt(c);
|
||||
return u == null || u == self;
|
||||
@@ -140,9 +136,9 @@ namespace OpenRa.Game.Traits
|
||||
|
||||
public void Tick( Actor self, Mobile mobile )
|
||||
{
|
||||
if( moveFractionTotal != 0 )
|
||||
if( move != null )
|
||||
{
|
||||
TickMove( self, mobile );
|
||||
move.TickMove( self, mobile, this );
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -171,20 +167,47 @@ namespace OpenRa.Game.Traits
|
||||
|
||||
mobile.toCell = nextCell;
|
||||
path.RemoveAt( path.Count - 1 );
|
||||
moveFractionTotal = ( dir.X != 0 && dir.Y != 0 ) ? 35 : 25;
|
||||
from = CenterOfCell( mobile.fromCell );
|
||||
to = BetweenCells( mobile.fromCell, mobile.toCell );
|
||||
CalculateMoveFraction();
|
||||
fromFacing = mobile.facing;
|
||||
toFacing = mobile.facing;
|
||||
OnComplete = OnCompleteFirstHalf;
|
||||
|
||||
move = new MoveFirstHalf(
|
||||
CenterOfCell( mobile.fromCell ),
|
||||
BetweenCells( mobile.fromCell, mobile.toCell ),
|
||||
mobile.facing,
|
||||
mobile.facing,
|
||||
0 );
|
||||
|
||||
Game.UnitInfluence.Update(mobile);
|
||||
}
|
||||
mobile.currentAction.Tick( self, mobile );
|
||||
}
|
||||
|
||||
void TickMove( Actor self, Mobile mobile )
|
||||
static float2 CenterOfCell( int2 loc )
|
||||
{
|
||||
return new float2( 12, 12 ) + Game.CellSize * (float2)loc;
|
||||
}
|
||||
|
||||
static float2 BetweenCells( int2 from, int2 to )
|
||||
{
|
||||
return 0.5f * ( CenterOfCell( from ) + CenterOfCell( to ) );
|
||||
}
|
||||
|
||||
abstract class MovePart
|
||||
{
|
||||
public readonly float2 from, to;
|
||||
public readonly int fromFacing, toFacing;
|
||||
public int moveFraction;
|
||||
public readonly int moveFractionTotal;
|
||||
|
||||
public MovePart( float2 from, float2 to, int fromFacing, int toFacing, int startingFraction )
|
||||
{
|
||||
this.from = from;
|
||||
this.to = to;
|
||||
this.fromFacing = fromFacing;
|
||||
this.toFacing = toFacing;
|
||||
this.moveFraction = startingFraction;
|
||||
this.moveFractionTotal = (int)( to - from ).Length * ( 25 / 6 );
|
||||
}
|
||||
|
||||
public void TickMove( Actor self, Mobile mobile, MoveTo parent )
|
||||
{
|
||||
var oldFraction = moveFraction;
|
||||
var oldTotal = moveFractionTotal;
|
||||
@@ -193,15 +216,11 @@ namespace OpenRa.Game.Traits
|
||||
UpdateCenterLocation( self, mobile );
|
||||
if( moveFraction >= moveFractionTotal )
|
||||
{
|
||||
moveFraction -= moveFractionTotal;
|
||||
if (!OnComplete(self, mobile))
|
||||
{
|
||||
moveFraction = oldFraction;
|
||||
moveFractionTotal = oldTotal;
|
||||
parent.move = OnComplete( self, mobile, parent );
|
||||
if( parent.move == null )
|
||||
UpdateCenterLocation( self, mobile );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void UpdateCenterLocation( Actor self, Mobile mobile )
|
||||
{
|
||||
@@ -214,62 +233,64 @@ namespace OpenRa.Game.Traits
|
||||
mobile.facing = ( fromFacing + ( toFacing - fromFacing ) * moveFraction / moveFractionTotal ) & 0xFF;
|
||||
}
|
||||
|
||||
void CalculateMoveFraction()
|
||||
{
|
||||
var d = to - from;
|
||||
moveFractionTotal = (int)d.Length * (25 / 6);
|
||||
protected abstract MovePart OnComplete( Actor self, Mobile mobile, MoveTo parent );
|
||||
}
|
||||
|
||||
static float2 CenterOfCell( int2 loc )
|
||||
class MoveFirstHalf : MovePart
|
||||
{
|
||||
public MoveFirstHalf( float2 from, float2 to, int fromFacing, int toFacing, int startingFraction )
|
||||
: base( from, to, fromFacing, toFacing, startingFraction )
|
||||
{
|
||||
return new float2( 12, 12 ) + Game.CellSize * (float2)loc;
|
||||
}
|
||||
|
||||
static float2 BetweenCells( int2 from, int2 to )
|
||||
protected override MovePart OnComplete( Actor self, Mobile mobile, MoveTo parent )
|
||||
{
|
||||
return 0.5f * ( CenterOfCell( from ) + CenterOfCell( to ) );
|
||||
}
|
||||
|
||||
bool OnCompleteFirstHalf( Actor self, Mobile mobile )
|
||||
if( parent.path.Count > 0 )
|
||||
{
|
||||
if( path.Count > 0 )
|
||||
{
|
||||
var nextCell = path[ path.Count - 1 ];
|
||||
var nextCell = parent.path[ parent.path.Count - 1 ];
|
||||
if( ( nextCell - mobile.toCell ) != ( mobile.toCell - mobile.fromCell ) )
|
||||
{
|
||||
if( !CanEnterCell( nextCell, self ) )
|
||||
return false;
|
||||
return null;
|
||||
|
||||
path.RemoveAt( path.Count - 1 );
|
||||
from = BetweenCells( mobile.fromCell, mobile.toCell );
|
||||
to = BetweenCells( mobile.toCell, nextCell );
|
||||
CalculateMoveFraction();
|
||||
parent.path.RemoveAt( parent.path.Count - 1 );
|
||||
|
||||
var ret = new MoveFirstHalf(
|
||||
BetweenCells( mobile.fromCell, mobile.toCell ),
|
||||
BetweenCells( mobile.toCell, nextCell ),
|
||||
mobile.facing,
|
||||
Util.GetNearestFacing( mobile.facing, Util.GetFacing( nextCell - mobile.toCell, mobile.facing ) ),
|
||||
moveFraction - moveFractionTotal );
|
||||
mobile.fromCell = mobile.toCell;
|
||||
mobile.toCell = nextCell;
|
||||
fromFacing = mobile.facing;
|
||||
toFacing = Util.GetNearestFacing( fromFacing,
|
||||
Util.GetFacing( mobile.toCell-mobile.fromCell, fromFacing ) );
|
||||
OnComplete = OnCompleteFirstHalf;
|
||||
Game.UnitInfluence.Update( mobile );
|
||||
return true;
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
from = BetweenCells( mobile.fromCell, mobile.toCell );
|
||||
to = CenterOfCell( mobile.toCell );
|
||||
CalculateMoveFraction();
|
||||
fromFacing = toFacing = mobile.facing;
|
||||
OnComplete = OnCompleteSecondHalf;
|
||||
var ret2 = new MoveSecondHalf(
|
||||
BetweenCells( mobile.fromCell, mobile.toCell ),
|
||||
CenterOfCell( mobile.toCell ),
|
||||
mobile.facing,
|
||||
mobile.facing,
|
||||
moveFraction - moveFractionTotal );
|
||||
mobile.fromCell = mobile.toCell;
|
||||
return true;
|
||||
return ret2;
|
||||
}
|
||||
}
|
||||
|
||||
bool OnCompleteSecondHalf( Actor self, Mobile mobile )
|
||||
class MoveSecondHalf : MovePart
|
||||
{
|
||||
public MoveSecondHalf( float2 from, float2 to, int fromFacing, int toFacing, int startingFraction )
|
||||
: base( from, to, fromFacing, toFacing, startingFraction )
|
||||
{
|
||||
}
|
||||
|
||||
protected override MovePart OnComplete( Actor self, Mobile mobile, MoveTo parent )
|
||||
{
|
||||
moveFractionTotal = 0;
|
||||
self.CenterLocation = CenterOfCell( mobile.toCell );
|
||||
OnComplete = null;
|
||||
mobile.fromCell = mobile.toCell;
|
||||
return true;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public void Cancel( Actor self, Mobile mobile )
|
||||
|
||||
Reference in New Issue
Block a user