Add plumbing for targeting of target center instead of closest targetable position

Note: Projectiles that can track their target need minor additional changes, but for InstantHit (which already implemented support for this) and unguided projectiles (Bullet, GravityBomb) this commit is already sufficient.
This commit is contained in:
reaperrr
2017-06-11 21:41:12 +02:00
committed by abcdefg30
parent 9c9a23be86
commit da7433a95f
6 changed files with 16 additions and 5 deletions

View File

@@ -114,7 +114,7 @@ namespace OpenRA.Mods.Common.Activities
return NextActivity;
}
var targetedPosition = Target.Positions.PositionClosestTo(pos);
var targetedPosition = attack.Info.AttackTargetCenter ? Target.CenterPosition : Target.Positions.PositionClosestTo(pos);
var desiredFacing = (targetedPosition - pos).Yaw.Facing;
if (facing.Facing != desiredFacing)
{

View File

@@ -102,6 +102,8 @@ namespace OpenRA.Mods.Common.Traits
public readonly Barrel[] Barrels;
readonly Actor self;
readonly AttackBase[] attackTraits;
AttackBase attack;
Turreted turret;
AmmoPool ammoPool;
BodyOrientation coords;
@@ -134,6 +136,7 @@ namespace OpenRA.Mods.Common.Traits
barrels.Add(new Barrel { Offset = WVec.Zero, Yaw = WAngle.Zero });
Barrels = barrels.ToArray();
attackTraits = self.TraitsImplementing<AttackBase>().Where(a => a.Info.Armaments.Contains(Info.Name)).ToArray();
}
public virtual WDist MaxRange()
@@ -190,6 +193,10 @@ namespace OpenRA.Mods.Common.Traits
// The world coordinate model uses Actor.Orientation
public virtual Barrel CheckFire(Actor self, IFacing facing, Target target)
{
attack = attackTraits.FirstOrDefault(Exts.IsTraitEnabled);
if (attack == null)
return null;
if (IsReloading)
return null;
@@ -229,7 +236,7 @@ namespace OpenRA.Mods.Common.Traits
Source = muzzlePosition(),
CurrentSource = muzzlePosition,
SourceActor = self,
PassiveTarget = target.Positions.PositionClosestTo(muzzlePosition()),
PassiveTarget = attack.Info.AttackTargetCenter ? target.CenterPosition : target.Positions.PositionClosestTo(muzzlePosition()),
GuidedTarget = target
};

View File

@@ -24,6 +24,9 @@ namespace OpenRA.Mods.Common.Traits
[Desc("Armament names")]
public readonly string[] Armaments = { "primary", "secondary" };
[Desc("Attack the target's center, instead of the closest targetable offset.")]
public readonly bool AttackTargetCenter = false;
public readonly string Cursor = null;
public readonly string OutsideRangeCursor = null;

View File

@@ -40,7 +40,7 @@ namespace OpenRA.Mods.Common.Traits
var f = facing.Facing;
var pos = self.CenterPosition;
var targetedPosition = target.Positions.PositionClosestTo(pos);
var targetedPosition = Info.AttackTargetCenter ? target.CenterPosition : target.Positions.PositionClosestTo(pos);
var delta = targetedPosition - pos;
var facingToTarget = delta.HorizontalLengthSquared != 0 ? delta.Yaw.Facing : f;

View File

@@ -146,7 +146,7 @@ namespace OpenRA.Mods.Common.Traits
return;
var pos = self.CenterPosition;
var targetedPosition = target.Positions.PositionClosestTo(pos);
var targetedPosition = Info.AttackTargetCenter ? target.CenterPosition : target.Positions.PositionClosestTo(pos);
var targetYaw = (targetedPosition - pos).Yaw;
foreach (var a in Armaments)

View File

@@ -131,7 +131,8 @@ namespace OpenRA.Mods.Common.Traits
return false;
var pos = self.CenterPosition;
var delta = target.Positions.PositionClosestTo(pos) - pos;
var targetPos = attack != null && attack.Info.AttackTargetCenter ? target.CenterPosition : target.Positions.PositionClosestTo(pos);
var delta = targetPos - pos;
DesiredFacing = delta.HorizontalLengthSquared != 0 ? delta.Yaw.Facing : TurretFacing;
MoveTurret();
return HasAchievedDesiredFacing;