Refactored Mobile.MoveTo
This commit is contained in:
@@ -61,20 +61,20 @@ namespace OpenRa.Game.Traits
|
|||||||
{
|
{
|
||||||
if (currentAction != null)
|
if (currentAction != null)
|
||||||
currentAction.Cancel(self, this);
|
currentAction.Cancel(self, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<int2> OccupiedCells()
|
public IEnumerable<int2> OccupiedCells()
|
||||||
{
|
{
|
||||||
return new[] { fromCell, toCell };
|
return new[] { fromCell, toCell };
|
||||||
}
|
}
|
||||||
|
|
||||||
public UnitMovementType GetMovementType()
|
public UnitMovementType GetMovementType()
|
||||||
{
|
{
|
||||||
/* todo: boats, planes */
|
/* todo: boats, planes */
|
||||||
|
|
||||||
var vi = self.unitInfo as UnitInfo.VehicleInfo;
|
var vi = self.unitInfo as UnitInfo.VehicleInfo;
|
||||||
if (vi == null) return UnitMovementType.Foot;
|
if (vi == null) return UnitMovementType.Foot;
|
||||||
return vi.Tracked ? UnitMovementType.Track : UnitMovementType.Wheel;
|
return vi.Tracked ? UnitMovementType.Track : UnitMovementType.Wheel;
|
||||||
}
|
}
|
||||||
|
|
||||||
public interface CurrentAction
|
public interface CurrentAction
|
||||||
@@ -121,28 +121,24 @@ 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -166,60 +162,24 @@ namespace OpenRa.Game.Traits
|
|||||||
if( firstFacing != mobile.facing )
|
if( firstFacing != mobile.facing )
|
||||||
mobile.currentAction = new Turn( firstFacing ) { NextAction = this };
|
mobile.currentAction = new Turn( firstFacing ) { NextAction = this };
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (!CanEnterCell(nextCell, self)) return; /* todo: repath, sometimes */
|
if (!CanEnterCell(nextCell, self)) return; /* todo: repath, sometimes */
|
||||||
|
|
||||||
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 )
|
|
||||||
{
|
|
||||||
var oldFraction = moveFraction;
|
|
||||||
var oldTotal = moveFractionTotal;
|
|
||||||
|
|
||||||
moveFraction += ( self.unitInfo as UnitInfo.MobileInfo ).Speed;
|
|
||||||
UpdateCenterLocation( self, mobile );
|
|
||||||
if( moveFraction >= moveFractionTotal )
|
|
||||||
{
|
|
||||||
moveFraction -= moveFractionTotal;
|
|
||||||
if (!OnComplete(self, mobile))
|
|
||||||
{
|
|
||||||
moveFraction = oldFraction;
|
|
||||||
moveFractionTotal = oldTotal;
|
|
||||||
UpdateCenterLocation(self, mobile );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void UpdateCenterLocation( Actor self, Mobile mobile )
|
|
||||||
{
|
|
||||||
var frac = (float)moveFraction / moveFractionTotal;
|
|
||||||
|
|
||||||
self.CenterLocation = float2.Lerp( from, to, frac );
|
|
||||||
if( moveFraction >= moveFractionTotal )
|
|
||||||
mobile.facing = toFacing & 0xFF;
|
|
||||||
else
|
|
||||||
mobile.facing = ( fromFacing + ( toFacing - fromFacing ) * moveFraction / moveFractionTotal ) & 0xFF;
|
|
||||||
}
|
|
||||||
|
|
||||||
void CalculateMoveFraction()
|
|
||||||
{
|
|
||||||
var d = to - from;
|
|
||||||
moveFractionTotal = (int)d.Length * (25 / 6);
|
|
||||||
}
|
|
||||||
|
|
||||||
static float2 CenterOfCell( int2 loc )
|
static float2 CenterOfCell( int2 loc )
|
||||||
{
|
{
|
||||||
return new float2( 12, 12 ) + Game.CellSize * (float2)loc;
|
return new float2( 12, 12 ) + Game.CellSize * (float2)loc;
|
||||||
@@ -230,46 +190,107 @@ namespace OpenRa.Game.Traits
|
|||||||
return 0.5f * ( CenterOfCell( from ) + CenterOfCell( to ) );
|
return 0.5f * ( CenterOfCell( from ) + CenterOfCell( to ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
bool OnCompleteFirstHalf( Actor self, Mobile mobile )
|
abstract class MovePart
|
||||||
{
|
{
|
||||||
if( path.Count > 0 )
|
public readonly float2 from, to;
|
||||||
{
|
public readonly int fromFacing, toFacing;
|
||||||
var nextCell = path[ path.Count - 1 ];
|
public int moveFraction;
|
||||||
if( ( nextCell - mobile.toCell ) != ( mobile.toCell - mobile.fromCell ) )
|
public readonly int moveFractionTotal;
|
||||||
{
|
|
||||||
if (!CanEnterCell(nextCell, self))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
path.RemoveAt( path.Count - 1 );
|
public MovePart( float2 from, float2 to, int fromFacing, int toFacing, int startingFraction )
|
||||||
from = BetweenCells( mobile.fromCell, mobile.toCell );
|
{
|
||||||
to = BetweenCells( mobile.toCell, nextCell );
|
this.from = from;
|
||||||
CalculateMoveFraction();
|
this.to = to;
|
||||||
mobile.fromCell = mobile.toCell;
|
this.fromFacing = fromFacing;
|
||||||
mobile.toCell = nextCell;
|
this.toFacing = toFacing;
|
||||||
fromFacing = mobile.facing;
|
this.moveFraction = startingFraction;
|
||||||
toFacing = Util.GetNearestFacing( fromFacing,
|
this.moveFractionTotal = (int)( to - from ).Length * ( 25 / 6 );
|
||||||
Util.GetFacing( mobile.toCell-mobile.fromCell, fromFacing ) );
|
}
|
||||||
OnComplete = OnCompleteFirstHalf;
|
|
||||||
Game.UnitInfluence.Update(mobile);
|
public void TickMove( Actor self, Mobile mobile, MoveTo parent )
|
||||||
return true;
|
{
|
||||||
|
var oldFraction = moveFraction;
|
||||||
|
var oldTotal = moveFractionTotal;
|
||||||
|
|
||||||
|
moveFraction += ( self.unitInfo as UnitInfo.MobileInfo ).Speed;
|
||||||
|
UpdateCenterLocation( self, mobile );
|
||||||
|
if( moveFraction >= moveFractionTotal )
|
||||||
|
{
|
||||||
|
parent.move = OnComplete( self, mobile, parent );
|
||||||
|
if( parent.move == null )
|
||||||
|
UpdateCenterLocation( self, mobile );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
from = BetweenCells( mobile.fromCell, mobile.toCell );
|
|
||||||
to = CenterOfCell( mobile.toCell );
|
void UpdateCenterLocation( Actor self, Mobile mobile )
|
||||||
CalculateMoveFraction();
|
{
|
||||||
fromFacing = toFacing = mobile.facing;
|
var frac = (float)moveFraction / moveFractionTotal;
|
||||||
OnComplete = OnCompleteSecondHalf;
|
|
||||||
mobile.fromCell = mobile.toCell;
|
self.CenterLocation = float2.Lerp( from, to, frac );
|
||||||
return true;
|
if( moveFraction >= moveFractionTotal )
|
||||||
|
mobile.facing = toFacing & 0xFF;
|
||||||
|
else
|
||||||
|
mobile.facing = ( fromFacing + ( toFacing - fromFacing ) * moveFraction / moveFractionTotal ) & 0xFF;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract MovePart OnComplete( Actor self, Mobile mobile, MoveTo parent );
|
||||||
}
|
}
|
||||||
|
|
||||||
bool OnCompleteSecondHalf( Actor self, Mobile mobile )
|
class MoveFirstHalf : MovePart
|
||||||
{
|
{
|
||||||
moveFractionTotal = 0;
|
public MoveFirstHalf( float2 from, float2 to, int fromFacing, int toFacing, int startingFraction )
|
||||||
self.CenterLocation = CenterOfCell( mobile.toCell );
|
: base( from, to, fromFacing, toFacing, startingFraction )
|
||||||
OnComplete = null;
|
{
|
||||||
mobile.fromCell = mobile.toCell;
|
}
|
||||||
return true;
|
|
||||||
|
protected override MovePart OnComplete( Actor self, Mobile mobile, MoveTo parent )
|
||||||
|
{
|
||||||
|
if( parent.path.Count > 0 )
|
||||||
|
{
|
||||||
|
var nextCell = parent.path[ parent.path.Count - 1 ];
|
||||||
|
if( ( nextCell - mobile.toCell ) != ( mobile.toCell - mobile.fromCell ) )
|
||||||
|
{
|
||||||
|
if( !CanEnterCell( nextCell, self ) )
|
||||||
|
return null;
|
||||||
|
|
||||||
|
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;
|
||||||
|
Game.UnitInfluence.Update( mobile );
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var ret2 = new MoveSecondHalf(
|
||||||
|
BetweenCells( mobile.fromCell, mobile.toCell ),
|
||||||
|
CenterOfCell( mobile.toCell ),
|
||||||
|
mobile.facing,
|
||||||
|
mobile.facing,
|
||||||
|
moveFraction - moveFractionTotal );
|
||||||
|
mobile.fromCell = mobile.toCell;
|
||||||
|
return ret2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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 )
|
||||||
|
{
|
||||||
|
self.CenterLocation = CenterOfCell( mobile.toCell );
|
||||||
|
mobile.fromCell = mobile.toCell;
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Cancel( Actor self, Mobile mobile )
|
public void Cancel( Actor self, Mobile mobile )
|
||||||
|
|||||||
Reference in New Issue
Block a user