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

@@ -192,7 +192,9 @@ namespace OpenRa.Game
throw new InvalidOperationException("BuildUnit without suitable production structure!"); throw new InvalidOperationException("BuildUnit without suitable production structure!");
var unit = new Actor(name, (1/24f * producer.CenterLocation).ToInt2(), player); var unit = new Actor(name, (1/24f * producer.CenterLocation).ToInt2(), player);
unit.Order(unit.Location + new int2(0, 3)).Apply(false); var mobile = unit.traits.Get<Mobile>();
mobile.facing = 128;
mobile.SetNextAction( new Traits.Mobile.MoveTo( unit.Location + new int2( 0, 3 ) ) );
world.AddFrameEndTask(_ => world.Add(unit)); world.AddFrameEndTask(_ => world.Add(unit));

View File

@@ -33,7 +33,7 @@ namespace OpenRa.Game
if (Game.LocalPlayer == Unit.Owner) if (Game.LocalPlayer == Unit.Owner)
Game.PlaySound("ackno.r00", false); Game.PlaySound("ackno.r00", false);
var mobile = Unit.traits.Get<Traits.Mobile>(); var mobile = Unit.traits.Get<Traits.Mobile>();
mobile.SetNextAction( new Traits.Mobile.MoveTo( Destination ) ); mobile.QueueAction( new Traits.Mobile.MoveTo( Destination ) );
} }
} }
} }

View File

@@ -35,7 +35,9 @@ namespace OpenRa.Game.Traits
public override void Apply( bool leftMouseButton ) public override void Apply( bool leftMouseButton )
{ {
if( leftMouseButton ) return; if( leftMouseButton ) return;
Unit.traits.Get<Mobile>().SetNextAction( new Mobile.Turn( 96 ) { NextAction = new DeployAction() } ); var mobile = Unit.traits.Get<Mobile>();
mobile.QueueAction( new Mobile.Turn( 96 ) );
mobile.QueueAction( new DeployAction() );
} }
class DeployAction : Mobile.CurrentAction class DeployAction : Mobile.CurrentAction
@@ -50,6 +52,12 @@ namespace OpenRa.Game.Traits
Game.world.Add( new Actor( "fact", self.Location - new int2( 1, 1 ), self.Owner ) ); Game.world.Add( new Actor( "fact", self.Location - new int2( 1, 1 ), self.Owner ) );
} ); } );
} }
public void Cancel( Actor self, Mobile mobile )
{
// Cancel can't happen between this being moved to the head of the list, and it being Ticked.
throw new InvalidOperationException( "DeployMcvAction: Cancel() should never occur." );
}
} }
} }
} }

View File

@@ -32,6 +32,21 @@ namespace OpenRa.Game.Traits
currentAction.NextAction = nextAction; 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) public void Tick(Actor self)
{ {
if( currentAction != null ) if( currentAction != null )
@@ -51,15 +66,16 @@ namespace OpenRa.Game.Traits
public interface CurrentAction public interface CurrentAction
{ {
CurrentAction NextAction { set; } CurrentAction NextAction { get; set; }
void Tick( Actor self, Mobile mobile ); void Tick( Actor self, Mobile mobile );
void Cancel( Actor self, Mobile mobile );
} }
public class Turn : CurrentAction public class Turn : CurrentAction
{ {
public CurrentAction NextAction { get; set; } public CurrentAction NextAction { get; set; }
public readonly int desiredFacing; public int desiredFacing;
public Turn( int desiredFacing ) public Turn( int desiredFacing )
{ {
@@ -77,6 +93,12 @@ namespace OpenRa.Game.Traits
} }
Util.TickFacing( ref mobile.facing, desiredFacing, self.unitInfo.ROT ); Util.TickFacing( ref mobile.facing, desiredFacing, self.unitInfo.ROT );
} }
public void Cancel( Actor self, Mobile mobile )
{
desiredFacing = mobile.facing;
NextAction = null;
}
} }
public class MoveTo : CurrentAction public class MoveTo : CurrentAction
@@ -211,6 +233,12 @@ namespace OpenRa.Game.Traits
OnComplete = null; OnComplete = null;
mobile.fromCell = mobile.toCell; mobile.fromCell = mobile.toCell;
} }
public void Cancel( Actor self, Mobile mobile )
{
path.Clear();
NextAction = null;
}
} }
} }
} }