fix scan rate of combat units (was every frame)

This commit is contained in:
Chris Forbes
2010-03-19 20:41:11 +13:00
parent 09f0846115
commit b5afedb601
2 changed files with 24 additions and 8 deletions

View File

@@ -19,13 +19,20 @@
#endregion #endregion
using System.Linq; using System.Linq;
using System.Drawing;
namespace OpenRA.Traits namespace OpenRA.Traits
{ {
class AutoTargetInfo : StatelessTraitInfo<AutoTarget> { } class AutoTargetInfo : StatelessTraitInfo<AutoTarget>
{
public readonly float ScanTimeAverage = 2f;
public readonly float ScanTimeSpread = .5f;
}
class AutoTarget : ITick, INotifyDamage class AutoTarget : ITick, INotifyDamage
{ {
int nextScanTime = 0;
void AttackTarget(Actor self, Actor target) void AttackTarget(Actor self, Actor target)
{ {
var attack = self.traits.Get<AttackBase>(); var attack = self.traits.Get<AttackBase>();
@@ -37,16 +44,24 @@ namespace OpenRA.Traits
{ {
if (!self.IsIdle) return; if (!self.IsIdle) return;
var attack = self.traits.Get<AttackBase>(); if (--nextScanTime <= 0)
var range = Util.GetMaximumRange(self); {
var attack = self.traits.Get<AttackBase>();
if (attack.target == null || var range = Util.GetMaximumRange(self);
(attack.target.Location - self.Location).LengthSquared > range * range + 2)
AttackTarget(self, ChooseTarget(self, range)); if (attack.target == null ||
(attack.target.Location - self.Location).LengthSquared > range * range + 2)
AttackTarget(self, ChooseTarget(self, range));
var info = self.Info.Traits.Get<AutoTargetInfo>();
nextScanTime = (int)(25 * (info.ScanTimeAverage +
(self.World.SharedRandom.NextDouble() * 2 - 1) * info.ScanTimeSpread));
}
} }
Actor ChooseTarget(Actor self, float range) Actor ChooseTarget(Actor self, float range)
{ {
Game.chat.AddLine(Color.White, "Debug", "AutoTarget.ChooseTarget()");
var inRange = self.World.FindUnitsInCircle(self.CenterLocation, Game.CellSize * range); var inRange = self.World.FindUnitsInCircle(self.CenterLocation, Game.CellSize * range);
return inRange return inRange

View File

@@ -22,6 +22,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Drawing;
namespace OpenRA.Traits.Activities namespace OpenRA.Traits.Activities
{ {
@@ -96,7 +97,7 @@ namespace OpenRA.Traits.Activities
return this; return this;
} }
if( destination == self.Location ) if (destination == self.Location)
return NextActivity; return NextActivity;
if( path == null ) if( path == null )