added AutoTarget trait
This commit is contained in:
@@ -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 */
|
||||
|
||||
|
||||
@@ -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" />
|
||||
|
||||
30
OpenRa.Game/Traits/AutoTarget.cs
Normal file
30
OpenRa.Game/Traits/AutoTarget.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user