make AutoTarget use an activity. add AutoTarget to teslatank

This commit is contained in:
Bob
2010-11-14 19:44:58 +13:00
parent 80e897abfb
commit b2f3b8f2af
3 changed files with 47 additions and 20 deletions

View File

@@ -89,6 +89,8 @@ namespace OpenRA.Mods.RA
public virtual void Tick(Actor self)
{
--nextScanTime;
foreach (var w in Weapons)
w.Tick();
@@ -191,18 +193,10 @@ namespace OpenRA.Mods.RA
}
public void ScanAndAttack(Actor self, bool allowMovement, bool holdStill)
{
if (--nextScanTime <= 0)
{
var targetActor = ScanForTarget(self, null);
if (targetActor != null)
AttackTarget(Target.FromActor(targetActor), false, allowMovement && !holdStill);
var info = self.Info.Traits.Get<AttackBaseInfo>();
nextScanTime = (int)(25 * (info.ScanTimeAverage +
(self.World.SharedRandom.NextDouble() * 2 - 1) * info.ScanTimeSpread));
}
}
public Actor ScanForTarget(Actor self, Actor currentTarget)
@@ -210,6 +204,7 @@ namespace OpenRA.Mods.RA
var range = GetMaximumRange();
if (self.IsIdle || currentTarget == null || !Combat.IsInRange(self.CenterLocation, range, currentTarget))
if(nextScanTime <= 0)
return ChooseTarget(self, range);
return currentTarget;
@@ -222,6 +217,10 @@ namespace OpenRA.Mods.RA
Actor ChooseTarget(Actor self, float range)
{
var info = self.Info.Traits.Get<AttackBaseInfo>();
nextScanTime = (int)(25 * (info.ScanTimeAverage +
(self.World.SharedRandom.NextDouble() * 2 - 1) * info.ScanTimeSpread));
var inRange = self.World.FindUnitsInCircle(self.CenterLocation, Game.CellSize * range);
return inRange

View File

@@ -9,6 +9,7 @@
#endregion
using OpenRA.Traits;
using OpenRA.Traits.Activities;
namespace OpenRA.Mods.RA
{
@@ -17,15 +18,8 @@ namespace OpenRA.Mods.RA
public readonly bool AllowMovement = true;
}
class AutoTarget : ITick, INotifyDamage
class AutoTarget : INotifyIdle, INotifyDamage
{
public void Tick(Actor self)
{
if (!self.IsIdle && self.Info.Traits.Get<AutoTargetInfo>().AllowMovement) return;
self.Trait<AttackBase>().ScanAndAttack(self, self.Info.Traits.Get<AutoTargetInfo>().AllowMovement);
}
public void Damaged(Actor self, AttackInfo e)
{
if (!self.IsIdle) return;
@@ -42,5 +36,38 @@ namespace OpenRA.Mods.RA
self.Trait<AttackBase>().AttackTarget(Target.FromActor(e.Attacker), false, self.Info.Traits.Get<AutoTargetInfo>().AllowMovement);
}
public void Idle( Actor self )
{
self.QueueActivity( new IdleAttackActivity() );
}
class IdleAttackActivity : Idle
{
Actor currentTarget;
IActivity inner;
public override IActivity Tick( Actor self )
{
if( NextActivity != null && inner != null )
inner.Cancel( self );
if( inner == null )
{
if( NextActivity != null )
return NextActivity;
var attack = self.Trait<AttackBase>();
var range = attack.GetMaximumRange();
currentTarget = attack.ScanForTarget(self, null);
if( currentTarget != null )
inner = attack.GetAttackActivity( self, Target.FromActor(currentTarget), self.Info.Traits.Get<AutoTargetInfo>().AllowMovement );
}
if( inner != null )
inner = Util.RunActivity( self, inner );
return this;
}
}
}
}

View File

@@ -991,6 +991,7 @@ TTNK:
RenderUnitSpinner:
Selectable:
Bounds: 28,28,0,0
AutoTarget:
FTRK:
Inherits: ^Vehicle