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