From 3ef0f90b00a28352ea9ef9d77c876aabc835a342 Mon Sep 17 00:00:00 2001 From: Bob Date: Mon, 26 Oct 2009 23:25:35 +1300 Subject: [PATCH] Added attack order. Cursor is currently incorrect. --- OpenRa.Game/Actor.cs | 6 +++-- OpenRa.Game/Traits/AttackTurreted.cs | 27 ++++++++++++++++++- OpenRa.Game/Traits/McvDeploy.cs | 2 +- OpenRa.Game/Traits/Mobile.cs | 37 ++++++++++++++------------ OpenRa.Game/Traits/TraitsInterfaces.cs | 2 +- 5 files changed, 52 insertions(+), 22 deletions(-) diff --git a/OpenRa.Game/Actor.cs b/OpenRa.Game/Actor.cs index 4981014dc8..c970f33890 100755 --- a/OpenRa.Game/Actor.cs +++ b/OpenRa.Game/Actor.cs @@ -65,10 +65,12 @@ namespace OpenRa.Game public Order Order( int2 xy, bool lmb ) { if (Owner != Game.LocalPlayer) - return null; + return null; + + var underCursor = Game.UnitInfluence.GetUnitAt( xy ) ?? Game.BuildingInfluence.GetBuildingAt( xy ); return traits.WithInterface() - .Select( x => x.Order( this, xy, lmb ) ) + .Select( x => x.Order( this, xy, lmb, underCursor ) ) .FirstOrDefault( x => x != null ); } diff --git a/OpenRa.Game/Traits/AttackTurreted.cs b/OpenRa.Game/Traits/AttackTurreted.cs index 24d0a488ba..33fecd7d49 100755 --- a/OpenRa.Game/Traits/AttackTurreted.cs +++ b/OpenRa.Game/Traits/AttackTurreted.cs @@ -5,7 +5,7 @@ using System.Text; namespace OpenRa.Game.Traits { - class AttackTurreted : ITick + class AttackTurreted : ITick, IOrder { public Actor target; @@ -52,5 +52,30 @@ namespace OpenRa.Game.Traits return true; } + + public Order Order( Actor self, int2 xy, bool lmb, Actor underCursor ) + { + if( underCursor == null ) return null; + + if( underCursor.Owner == self.Owner ) return null; + + return new AttackOrder( self, underCursor ); + } + } + + class AttackOrder : Order + { + public readonly Actor Attacker; + public readonly Actor Target; + + public AttackOrder( Actor attacker, Actor target ) + { + this.Attacker = attacker; + this.Target = target; + } + public override void Apply() + { + Attacker.traits.Get().target = Target; + } } } diff --git a/OpenRa.Game/Traits/McvDeploy.cs b/OpenRa.Game/Traits/McvDeploy.cs index 33ef8f3737..b64ba7fd3e 100644 --- a/OpenRa.Game/Traits/McvDeploy.cs +++ b/OpenRa.Game/Traits/McvDeploy.cs @@ -11,7 +11,7 @@ namespace OpenRa.Game.Traits { } - public Order Order(Actor self, int2 xy, bool lmb) + public Order Order(Actor self, int2 xy, bool lmb, Actor underCursor) { if( lmb ) return null; diff --git a/OpenRa.Game/Traits/Mobile.cs b/OpenRa.Game/Traits/Mobile.cs index 9b080fb510..9705e7123a 100644 --- a/OpenRa.Game/Traits/Mobile.cs +++ b/OpenRa.Game/Traits/Mobile.cs @@ -47,9 +47,12 @@ namespace OpenRa.Game.Traits fromCell = toCell; } - public Order Order(Actor self, int2 xy, bool lmb) + public Order Order(Actor self, int2 xy, bool lmb, Actor underCursor) { - if( lmb ) return null; + if( lmb ) return null; + + if( underCursor != null ) + return null; if (xy != toCell) return new MoveOrder(self, xy); @@ -248,21 +251,21 @@ namespace OpenRa.Game.Traits { var nextCell = parent.path[ parent.path.Count - 1 ]; if( ( nextCell - mobile.toCell ) != ( mobile.toCell - mobile.fromCell ) ) - { - if( CanEnterCell( nextCell, self ) ) - { - 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; + { + if( CanEnterCell( nextCell, self ) ) + { + 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; } } } diff --git a/OpenRa.Game/Traits/TraitsInterfaces.cs b/OpenRa.Game/Traits/TraitsInterfaces.cs index 3c0c189c98..861026e1c8 100644 --- a/OpenRa.Game/Traits/TraitsInterfaces.cs +++ b/OpenRa.Game/Traits/TraitsInterfaces.cs @@ -9,5 +9,5 @@ namespace OpenRa.Game.Traits { interface ITick { void Tick(Actor self); } interface IRender { IEnumerable> Render(Actor self); } - interface IOrder { Order Order(Actor self, int2 xy, bool lmb); } + interface IOrder { Order Order(Actor self, int2 xy, bool lmb, Actor underCursor); } }