added AutoTarget trait

This commit is contained in:
Chris Forbes
2009-12-08 21:12:25 +13:00
parent 0dbf1bf027
commit 12ac96a885
4 changed files with 32 additions and 2 deletions

View File

@@ -239,7 +239,6 @@ namespace OpenRa.Game
.FirstOrDefault();
}
public static Random SharedRandom = new Random(0); /* for things that require sync */
public static Random CosmeticRandom = new Random(); /* for things that are just fluff */

View File

@@ -157,6 +157,7 @@
<Compile Include="Traits\Activities\Turn.cs" />
<Compile Include="Traits\AttackBase.cs" />
<Compile Include="Traits\AttackTurreted.cs" />
<Compile Include="Traits\AutoTarget.cs" />
<Compile Include="Traits\Building.cs" />
<Compile Include="Traits\Harvester.cs" />
<Compile Include="Traits\Helicopter.cs" />

View File

@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OpenRa.Game.Traits
{
class AutoTarget : ITick
{
public AutoTarget(Actor self) {}
public void Tick(Actor self)
{
var attack = self.traits.WithInterface<AttackBase>().First();
if (attack.target == null)
attack.target = ChooseTarget(self,
Rules.WeaponInfo[self.Info.Primary].Range);
}
Actor ChooseTarget(Actor self, float range)
{
var inRange = Game.FindUnitsInCircle(self.CenterLocation, Game.CellSize * range);
return inRange.Where(a => a.Owner != self.Owner) /* todo: one day deal with friendly players */
.OrderBy(a => (a.Location - self.Location).LengthSquared)
.FirstOrDefault();
}
}
}

View File

@@ -278,7 +278,7 @@ Footprint=_ x
SelectionPriority=3
[GUN]
Description=Turret
Traits=Building, Turreted, RenderBuildingTurreted, AttackTurreted
Traits=Building, Turreted, RenderBuildingTurreted, AttackTurreted, AutoTarget
Dimensions=1,1
Footprint=x
SelectionPriority=3