Traits can now resolve orders (ATM, they all resolve the orders they issue)

This commit is contained in:
Bob
2009-11-25 20:42:03 +13:00
parent e9b87640bd
commit 7c1c0835e1
10 changed files with 117 additions and 82 deletions

View File

@@ -69,11 +69,30 @@ namespace OpenRa.Game.Traits
return true;
}
public Order Order( Actor self, int2 xy, bool lmb, Actor underCursor )
public Order IssueOrder( Actor self, int2 xy, bool lmb, Actor underCursor )
{
if( lmb || underCursor == null ) return null;
if( underCursor.Owner == self.Owner ) return null;
return OpenRa.Game.Order.Attack( self, underCursor );
return Order.Attack( self, underCursor );
}
public void ResolveOrder( Actor self, Order order )
{
if( order.OrderString == "Attack" )
{
self.CancelActivity();
QueueAttack( self, order );
}
}
protected virtual void QueueAttack( Actor self, Order order )
{
const int RangeTolerance = 1; /* how far inside our maximum range we should try to sit */
/* todo: choose the appropriate weapon, when only one works against this target */
var weapon = order.Subject.unitInfo.Primary ?? order.Subject.unitInfo.Secondary;
self.QueueActivity( new Traits.Activities.Attack( order.TargetActor,
Math.Max( 0, (int)Rules.WeaponInfo[ weapon ].Range - RangeTolerance ) ) );
}
}
@@ -94,5 +113,16 @@ namespace OpenRa.Game.Traits
DoAttack( self );
}
protected override void QueueAttack( Actor self, Order order )
{
const int RangeTolerance = 1; /* how far inside our maximum range we should try to sit */
/* todo: choose the appropriate weapon, when only one works against this target */
var weapon = order.Subject.unitInfo.Primary ?? order.Subject.unitInfo.Secondary;
self.QueueActivity( new Traits.Activities.Follow( order.TargetActor,
Math.Max( 0, (int)Rules.WeaponInfo[ weapon ].Range - RangeTolerance ) ) );
self.traits.Get<AttackTurreted>().target = order.TargetActor;
}
}
}

View File

@@ -13,6 +13,8 @@ namespace OpenRa.Game.Traits
public bool IsFull { get { return oreCarried + gemsCarried == Rules.General.BailCount; } }
public bool IsEmpty { get { return oreCarried == 0 && gemsCarried == 0; } }
public Harvester( Actor self ) { }
public void AcceptResource(bool isGem)
{
if (isGem) gemsCarried++;
@@ -27,21 +29,34 @@ namespace OpenRa.Game.Traits
gemsCarried = 0;
}
public Order Order(Actor self, int2 xy, bool lmb, Actor underCursor)
public Order IssueOrder(Actor self, int2 xy, bool lmb, Actor underCursor)
{
if (lmb) return null;
if (underCursor != null
&& underCursor.Owner == self.Owner
&& underCursor.traits.Contains<AcceptsOre>() && !IsEmpty)
return OpenRa.Game.Order.DeliverOre(self, underCursor);
return Order.DeliverOre(self, underCursor);
if (underCursor == null && Rules.Map.ContainsResource(xy))
return OpenRa.Game.Order.Harvest(self, xy);
return Order.Harvest(self, xy);
return null;
}
public Harvester(Actor self) { }
public void ResolveOrder( Actor self, Order order )
{
if( order.OrderString == "Harvest" )
{
self.CancelActivity();
self.QueueActivity( new Traits.Activities.Move( order.TargetLocation, 0 ) );
self.QueueActivity( new Traits.Activities.Harvest() );
}
else if( order.OrderString == "DeliverOre" )
{
self.CancelActivity();
self.QueueActivity( new Traits.Activities.DeliverOre( order.TargetActor ) );
}
}
}
}

View File

@@ -18,16 +18,28 @@ namespace OpenRa.Game.Traits
targetLocation = self.Location;
}
public Order Order(Actor self, int2 xy, bool lmb, Actor underCursor)
public Order IssueOrder(Actor self, int2 xy, bool lmb, Actor underCursor)
{
if (lmb) return null;
if (underCursor == null)
return OpenRa.Game.Order.Move(self, xy);
return Order.Move(self, xy);
return null;
}
public void ResolveOrder( Actor self, Order order )
{
if( order.OrderString == "Move" )
{
targetLocation = order.TargetLocation;
var attackBase = self.traits.WithInterface<AttackBase>().FirstOrDefault();
if( attackBase != null )
attackBase.target = null; /* move cancels attack order */
}
}
public void Tick(Actor self)
{
var unit = self.traits.Get<Unit>();

View File

@@ -10,12 +10,26 @@ namespace OpenRa.Game.Traits
{
public McvDeploy(Actor self) { }
public Order Order(Actor self, int2 xy, bool lmb, Actor underCursor)
public Order IssueOrder(Actor self, int2 xy, bool lmb, Actor underCursor)
{
if (lmb) return null;
if( xy != self.Location ) return null;
return OpenRa.Game.Order.DeployMcv(self);
return Order.DeployMcv(self);
}
public void ResolveOrder( Actor self, Order order )
{
if( order.OrderString == "DeployMcv" )
{
var factBuildingInfo = (UnitInfo.BuildingInfo)Rules.UnitInfo[ "fact" ];
if( Game.CanPlaceBuilding( factBuildingInfo, self.Location - new int2( 1, 1 ), self, false ) )
{
self.CancelActivity();
self.QueueActivity( new Traits.Activities.Turn( 96 ) );
self.QueueActivity( new Traits.Activities.DeployMcv() );
}
}
}
}
}

View File

@@ -25,7 +25,7 @@ namespace OpenRa.Game.Traits
Game.UnitInfluence.Update( this );
}
public Order Order(Actor self, int2 xy, bool lmb, Actor underCursor)
public Order IssueOrder(Actor self, int2 xy, bool lmb, Actor underCursor)
{
if( lmb ) return null;
@@ -34,7 +34,20 @@ namespace OpenRa.Game.Traits
if (xy == toCell) return null;
return OpenRa.Game.Order.Move( self, xy );
return Order.Move( self, xy );
}
public void ResolveOrder( Actor self, Order order )
{
if( order.OrderString == "Move" )
{
self.CancelActivity();
self.QueueActivity( new Traits.Activities.Move( order.TargetLocation, 8 ) );
var attackBase = self.traits.WithInterface<AttackBase>().FirstOrDefault();
if( attackBase != null )
attackBase.target = null; /* move cancels attack order */
}
}
public IEnumerable<int2> OccupiedCells()

View File

@@ -29,10 +29,16 @@ namespace OpenRa.Game.Traits
anim.Image, Game.CellSize * (new float2(.5f, .5f) + rallyPoint.ToFloat2()));
}
public Order Order(Actor self, int2 xy, bool lmb, Actor underCursor)
public Order IssueOrder(Actor self, int2 xy, bool lmb, Actor underCursor)
{
if (lmb || underCursor != null) return null;
return OpenRa.Game.Order.SetRallyPoint(self, xy);
return Order.SetRallyPoint(self, xy);
}
public void ResolveOrder( Actor self, Order order )
{
if( order.OrderString == "SetRallyPoint" )
rallyPoint = order.TargetLocation;
}
public void Tick(Actor self) { anim.Tick(); }

View File

@@ -11,8 +11,12 @@ namespace OpenRa.Game.Traits
interface ITick { void Tick(Actor self); }
interface IRender { IEnumerable<Tuple<Sprite, float2, int>> Render(Actor self); }
interface IOrder { Order Order(Actor self, int2 xy, bool lmb, Actor underCursor); }
interface INotifyDamage { void Damaged(Actor self, DamageState ds); }
interface INotifyDamageEx : INotifyDamage { void Damaged(Actor self, int damage); }
interface INotifyBuildComplete { void BuildingComplete (Actor self); }
interface IOrder
{
Order IssueOrder( Actor self, int2 xy, bool lmb, Actor underCursor );
void ResolveOrder( Actor self, Order order );
}
}