Added Cancel() to Mobile.CurrentAction. Added QueueAction to Mobile. Made Order.Apply use queue rather than SetNextAction.

This commit is contained in:
Bob
2009-10-24 19:17:13 +13:00
parent d66475edcb
commit d25bd8550a
4 changed files with 156 additions and 118 deletions

View File

@@ -30,6 +30,21 @@ namespace OpenRa.Game.Traits
currentAction = nextAction;
else
currentAction.NextAction = nextAction;
}
public void QueueAction( CurrentAction nextAction )
{
if( currentAction == null )
{
currentAction = nextAction;
return;
}
var act = currentAction;
while( act.NextAction != null )
{
act = act.NextAction;
}
act.NextAction = nextAction;
}
public void Tick(Actor self)
@@ -50,16 +65,17 @@ namespace OpenRa.Game.Traits
public interface CurrentAction
{
CurrentAction NextAction { set; }
void Tick( Actor self, Mobile mobile );
{
CurrentAction NextAction { get; set; }
void Tick( Actor self, Mobile mobile );
void Cancel( Actor self, Mobile mobile );
}
public class Turn : CurrentAction
{
public CurrentAction NextAction { get; set; }
public readonly int desiredFacing;
public int desiredFacing;
public Turn( int desiredFacing )
{
@@ -76,6 +92,12 @@ namespace OpenRa.Game.Traits
return;
}
Util.TickFacing( ref mobile.facing, desiredFacing, self.unitInfo.ROT );
}
public void Cancel( Actor self, Mobile mobile )
{
desiredFacing = mobile.facing;
NextAction = null;
}
}
@@ -87,7 +109,7 @@ namespace OpenRa.Game.Traits
List<int2> path;
int moveFraction, moveFractionTotal;
float2 from, to;
float2 from, to;
int fromFacing, toFacing;
Action<Actor, Mobile> OnComplete;
@@ -130,14 +152,14 @@ namespace OpenRa.Game.Traits
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;
to = BetweenCells( mobile.fromCell, mobile.toCell );
CalculateMoveFraction();
fromFacing = mobile.facing;
toFacing = mobile.facing;
OnComplete = OnCompleteFirstHalf;
}
mobile.currentAction.Tick( self, mobile );
}
}
void TickMove( Actor self, Mobile mobile )
{
@@ -150,21 +172,21 @@ namespace OpenRa.Game.Traits
//mobile.fromCell = mobile.toCell;
}
return;
}
void UpdateCenterLocation( Actor self, Mobile mobile, float frac )
{
self.CenterLocation = float2.Lerp( from, to, frac );
if( moveFraction >= moveFractionTotal )
mobile.facing = toFacing;
else
mobile.facing = ( fromFacing + ( toFacing - fromFacing ) * moveFraction / moveFractionTotal ) & 0xFF;
}
void CalculateMoveFraction()
{
var d = to - from;
moveFractionTotal = (int)Math.Sqrt( d.X * d.X + d.Y * d.Y ) * (25 / 6);
}
void UpdateCenterLocation( Actor self, Mobile mobile, float frac )
{
self.CenterLocation = float2.Lerp( from, to, frac );
if( moveFraction >= moveFractionTotal )
mobile.facing = toFacing;
else
mobile.facing = ( fromFacing + ( toFacing - fromFacing ) * moveFraction / moveFractionTotal ) & 0xFF;
}
void CalculateMoveFraction()
{
var d = to - from;
moveFractionTotal = (int)Math.Sqrt( d.X * d.X + d.Y * d.Y ) * (25 / 6);
}
static float2 CenterOfCell( int2 loc )
@@ -178,38 +200,44 @@ namespace OpenRa.Game.Traits
}
void OnCompleteFirstHalf( Actor self, Mobile mobile )
{
if( path.Count > 0 )
{
var nextCell = path[ path.Count - 1 ];
if( ( nextCell - mobile.toCell ) != ( mobile.toCell - mobile.fromCell ) )
{
path.RemoveAt( path.Count - 1 );
from = BetweenCells( mobile.fromCell, mobile.toCell );
to = BetweenCells( mobile.toCell, nextCell );
CalculateMoveFraction();
mobile.fromCell = mobile.toCell;
mobile.toCell = nextCell;
fromFacing = mobile.facing;
toFacing = Util.GetNearestFacing( fromFacing, Util.GetFacing( mobile.toCell-mobile.fromCell, fromFacing ) );
OnComplete = OnCompleteFirstHalf;
return;
}
}
from = BetweenCells( mobile.fromCell, mobile.toCell );
to = CenterOfCell( mobile.toCell );
CalculateMoveFraction();
fromFacing = toFacing = mobile.facing;
OnComplete = OnCompleteSecondHalf;
mobile.fromCell = mobile.toCell;
{
if( path.Count > 0 )
{
var nextCell = path[ path.Count - 1 ];
if( ( nextCell - mobile.toCell ) != ( mobile.toCell - mobile.fromCell ) )
{
path.RemoveAt( path.Count - 1 );
from = BetweenCells( mobile.fromCell, mobile.toCell );
to = BetweenCells( mobile.toCell, nextCell );
CalculateMoveFraction();
mobile.fromCell = mobile.toCell;
mobile.toCell = nextCell;
fromFacing = mobile.facing;
toFacing = Util.GetNearestFacing( fromFacing, Util.GetFacing( mobile.toCell-mobile.fromCell, fromFacing ) );
OnComplete = OnCompleteFirstHalf;
return;
}
}
from = BetweenCells( mobile.fromCell, mobile.toCell );
to = CenterOfCell( mobile.toCell );
CalculateMoveFraction();
fromFacing = toFacing = mobile.facing;
OnComplete = OnCompleteSecondHalf;
mobile.fromCell = mobile.toCell;
}
void OnCompleteSecondHalf( Actor self, Mobile mobile )
{
{
moveFractionTotal = 0;
self.CenterLocation = CenterOfCell( mobile.toCell );
OnComplete = null;
mobile.fromCell = mobile.toCell;
OnComplete = null;
mobile.fromCell = mobile.toCell;
}
public void Cancel( Actor self, Mobile mobile )
{
path.Clear();
NextAction = null;
}
}
}