moved Activity to Actor (from Mobile)

This commit is contained in:
Bob
2009-11-25 18:34:30 +13:00
parent 5c3da9387b
commit 74ab0c7a13
6 changed files with 55 additions and 62 deletions

View File

@@ -9,6 +9,7 @@ using OpenRa.Game.GameRules;
using OpenRa.Game.Graphics;
using System.Drawing;
using OpenRa.Game.Traits;
using OpenRa.Game.Traits.Activities;
namespace OpenRa.Game
{
@@ -21,6 +22,7 @@ namespace OpenRa.Game
public int2 Location;
public Player Owner;
public int Health;
IActivity currentActivity;
public Actor( string name, int2 location, Player owner )
{
@@ -54,6 +56,13 @@ namespace OpenRa.Game
public void Tick()
{
var nextActivity = currentActivity;
while( nextActivity != null )
{
currentActivity = nextActivity;
nextActivity = nextActivity.Tick( this );
}
foreach (var tick in traits.WithInterface<Traits.ITick>())
tick.Tick(this);
}
@@ -124,5 +133,32 @@ namespace OpenRa.Game
foreach (var ndx in traits.WithInterface<INotifyDamageEx>())
ndx.Damaged(this, damage);
}
public void QueueActivity( IActivity nextActivity )
{
if( currentActivity == null )
{
currentActivity = nextActivity;
return;
}
var act = currentActivity;
while( act.NextActivity != null )
{
act = act.NextActivity;
}
act.NextActivity = nextActivity;
}
public void CancelActivity( Actor self )
{
if( currentActivity != null )
currentActivity.Cancel( self );
}
// For pathdebug, et al
public IActivity GetCurrentActivity()
{
return currentActivity;
}
}
}

View File

@@ -381,7 +381,7 @@ namespace OpenRa.Game
if( mobile != null )
{
unit.Facing = 128;
mobile.QueueActivity(new Traits.Activities.Move(dest, 1));
newActor.QueueActivity(new Traits.Activities.Move(dest, 1));
}
var heli = newActor.traits.GetOrDefault<Helicopter>();

View File

@@ -17,7 +17,7 @@ namespace OpenRa.Game.Traits
var unit = harvester.traits.Get<Unit>();
var mobile = harvester.traits.Get<Mobile>();
unit.Facing = 64;
mobile.QueueActivity(new Harvest());
harvester.QueueActivity(new Harvest());
w.Add(harvester);
});
}

View File

@@ -41,7 +41,7 @@ namespace OpenRa.Game.Traits.Activities
}
else
{
mobile.QueueActivity( new Move(
self.QueueActivity( new Move(
() =>
{
var search = new PathSearch
@@ -53,7 +53,7 @@ namespace OpenRa.Game.Traits.Activities
search.AddInitialCell( self.Location );
return Game.PathFinder.FindPath( search );
} ) );
mobile.QueueActivity( new Harvest() );
self.QueueActivity( new Harvest() );
return NextActivity;
}
}

View File

@@ -8,7 +8,7 @@ using OpenRa.Game.Traits.Activities;
namespace OpenRa.Game.Traits
{
class Mobile : ITick, IOrder
class Mobile : IOrder
{
public Actor self;
@@ -17,7 +17,6 @@ namespace OpenRa.Game.Traits
public int2 toCell { get { return self.Location; } set { Game.UnitInfluence.Remove( this ); self.Location = value; Game.UnitInfluence.Add( this ); } }
public int Voice = Game.CosmeticRandom.Next(2);
IActivity currentActivity;
public Mobile(Actor self)
{
@@ -26,37 +25,6 @@ namespace OpenRa.Game.Traits
Game.UnitInfluence.Update( this );
}
public void QueueActivity( IActivity nextActivity )
{
if( currentActivity == null )
{
currentActivity = nextActivity;
return;
}
var act = currentActivity;
while( act.NextActivity != null )
{
act = act.NextActivity;
}
act.NextActivity = nextActivity;
}
public void Tick(Actor self)
{
if( currentActivity == null )
{
fromCell = toCell;
return;
}
var nextActivity = currentActivity;
while( nextActivity != null )
{
currentActivity = nextActivity;
nextActivity = nextActivity.Tick( self );
}
}
public Order Order(Actor self, int2 xy, bool lmb, Actor underCursor)
{
if( lmb ) return null;
@@ -70,12 +38,6 @@ namespace OpenRa.Game.Traits
!Game.IsCellBuildable(xy, GetMovementType()) );
}
public void Cancel(Actor self)
{
if (currentActivity != null)
currentActivity.Cancel(self);
}
public IEnumerable<int2> OccupiedCells()
{
return new[] { fromCell, toCell };
@@ -100,11 +62,9 @@ namespace OpenRa.Game.Traits
public IEnumerable<int2> GetCurrentPath()
{
var move = currentActivity as Traits.Activities.Move;
var move = self.GetCurrentActivity() as Traits.Activities.Move;
if (move == null || move.path == null) return new int2[] { };
return Enumerable.Reverse(move.path);
}
public bool HasActivity { get { return currentActivity != null; } }
}
}

View File

@@ -18,8 +18,8 @@ namespace OpenRa.Game
var mobile = order.Subject.traits.GetOrDefault<Mobile>();
if (mobile != null)
{
mobile.Cancel(order.Subject);
mobile.QueueActivity(new Traits.Activities.Move(order.TargetLocation, 8));
order.Subject.CancelActivity( order.Subject );
order.Subject.QueueActivity( new Traits.Activities.Move( order.TargetLocation, 8 ) );
}
var heli = order.Subject.traits.GetOrDefault<Helicopter>();
@@ -38,10 +38,10 @@ namespace OpenRa.Game
/* todo: choose the appropriate weapon, when only one works against this target */
var weapon = order.Subject.unitInfo.Primary ?? order.Subject.unitInfo.Secondary;
mobile.Cancel(order.Subject);
order.Subject.CancelActivity(order.Subject);
if (order.Subject.traits.Contains<AttackTurreted>())
{
mobile.QueueActivity(
order.Subject.QueueActivity(
new Traits.Activities.Follow(order.TargetActor,
Math.Max(0, (int)Rules.WeaponInfo[weapon].Range - RangeTolerance)));
@@ -49,7 +49,7 @@ namespace OpenRa.Game
}
else
{
mobile.QueueActivity(
order.Subject.QueueActivity(
new Traits.Activities.Attack(order.TargetActor,
Math.Max(0, (int)Rules.WeaponInfo[weapon].Range - RangeTolerance)));
}
@@ -61,25 +61,22 @@ namespace OpenRa.Game
if( !Game.CanPlaceBuilding( factBuildingInfo, order.Subject.Location - new int2( 1, 1 ), order.Subject, false ) )
break; /* throw the order on the floor */
var mobile = order.Subject.traits.Get<Mobile>();
mobile.Cancel(order.Subject);
mobile.QueueActivity( new Traits.Activities.Turn( 96 ) );
mobile.QueueActivity( new Traits.Activities.DeployMcv() );
order.Subject.CancelActivity( order.Subject );
order.Subject.QueueActivity( new Traits.Activities.Turn( 96 ) );
order.Subject.QueueActivity( new Traits.Activities.DeployMcv() );
break;
}
case "DeliverOre":
{
var mobile = order.Subject.traits.Get<Mobile>();
mobile.Cancel(order.Subject);
mobile.QueueActivity(new Traits.Activities.DeliverOre(order.TargetActor));
order.Subject.CancelActivity( order.Subject );
order.Subject.QueueActivity( new Traits.Activities.DeliverOre( order.TargetActor ) );
break;
}
case "Harvest":
{
var mobile = order.Subject.traits.Get<Mobile>();
mobile.Cancel(order.Subject);
mobile.QueueActivity(new Traits.Activities.Move(order.TargetLocation, 0));
mobile.QueueActivity(new Traits.Activities.Harvest() );
order.Subject.CancelActivity( order.Subject );
order.Subject.QueueActivity( new Traits.Activities.Move( order.TargetLocation, 0 ) );
order.Subject.QueueActivity( new Traits.Activities.Harvest() );
break;
}
case "PlaceBuilding":