From 5ebf64f80341303c3b37688ba59b0ece82b8948a Mon Sep 17 00:00:00 2001 From: Bob Date: Mon, 26 Oct 2009 23:55:47 +1300 Subject: [PATCH] Separating Turret-specific parts out of AttackTurreted. --- OpenRa.Game/Traits/AttackTurreted.cs | 57 ++++++++++++++++++---------- 1 file changed, 36 insertions(+), 21 deletions(-) diff --git a/OpenRa.Game/Traits/AttackTurreted.cs b/OpenRa.Game/Traits/AttackTurreted.cs index 33fecd7d49..6a6b8cf6f9 100755 --- a/OpenRa.Game/Traits/AttackTurreted.cs +++ b/OpenRa.Game/Traits/AttackTurreted.cs @@ -5,35 +5,33 @@ using System.Text; namespace OpenRa.Game.Traits { - class AttackTurreted : ITick, IOrder + abstract class AttackBase : IOrder { public Actor target; // time (in frames) until each weapon can fire again. - int primaryFireDelay = 0; - int secondaryFireDelay = 0; + protected int primaryFireDelay = 0; + protected int secondaryFireDelay = 0; - public AttackTurreted( Actor self ) { self.traits.Get(); } - - public void Tick(Actor self) + protected bool CanAttack( Actor self ) { - if (primaryFireDelay > 0) --primaryFireDelay; - if (secondaryFireDelay > 0) --secondaryFireDelay; + if( primaryFireDelay > 0 ) --primaryFireDelay; + if( secondaryFireDelay > 0 ) --secondaryFireDelay; - if (target != null && target.IsDead) target = null; /* he's dead, jim. */ - if (target == null) return; + if( target != null && target.IsDead ) target = null; /* he's dead, jim. */ + if( target == null ) return false; - var turreted = self.traits.Get(); - turreted.desiredFacing = Util.GetFacing(target.CenterLocation - self.CenterLocation, turreted.turretFacing); - if (turreted.desiredFacing != turreted.turretFacing) - return; + return true; + } - if (self.unitInfo.Primary != null && CheckFire(self, self.unitInfo.Primary, ref primaryFireDelay)) + protected void DoAttack( Actor self ) + { + if( self.unitInfo.Primary != null && CheckFire( self, self.unitInfo.Primary, ref primaryFireDelay ) ) { - secondaryFireDelay = Math.Max(4, secondaryFireDelay); + secondaryFireDelay = Math.Max( 4, secondaryFireDelay ); return; } - if (self.unitInfo.Secondary != null && CheckFire(self, self.unitInfo.Secondary, ref secondaryFireDelay)) + if( self.unitInfo.Secondary != null && CheckFire( self, self.unitInfo.Secondary, ref secondaryFireDelay ) ) return; } @@ -41,13 +39,13 @@ namespace OpenRa.Game.Traits { if( fireDelay > 0 ) return false; var weapon = Rules.WeaponInfo[ weaponName ]; - if( weapon.Range * weapon.Range < (target.Location - self.Location).LengthSquared ) return false; + if( weapon.Range * weapon.Range < ( target.Location - self.Location ).LengthSquared ) return false; // FIXME: rules specifies ROF in 1/15 sec units; ticks are 1/25 sec fireDelay = weapon.ROF; - Game.world.Add( new Bullet( weaponName, self.Owner, self, - self.CenterLocation.ToInt2(), + Game.world.Add( new Bullet( weaponName, self.Owner, self, + self.CenterLocation.ToInt2(), target.CenterLocation.ToInt2() ) ); return true; @@ -55,7 +53,7 @@ namespace OpenRa.Game.Traits public Order Order( Actor self, int2 xy, bool lmb, Actor underCursor ) { - if( underCursor == null ) return null; + if( !lmb || underCursor == null ) return null; if( underCursor.Owner == self.Owner ) return null; @@ -63,6 +61,23 @@ namespace OpenRa.Game.Traits } } + class AttackTurreted : AttackBase, ITick + { + public AttackTurreted( Actor self ) { self.traits.Get(); } + + public void Tick(Actor self) + { + if( !CanAttack( self ) ) return; + + var turreted = self.traits.Get(); + turreted.desiredFacing = Util.GetFacing( target.CenterLocation - self.CenterLocation, turreted.turretFacing ); + if( turreted.desiredFacing != turreted.turretFacing ) + return; + + DoAttack( self ); + } + } + class AttackOrder : Order { public readonly Actor Attacker;