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; 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; var desiredFacing = (targetedPosition - pos).Yaw.Facing;
if (facing.Facing != desiredFacing) if (facing.Facing != desiredFacing)
{ {

View File

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

View File

@@ -24,6 +24,9 @@ namespace OpenRA.Mods.Common.Traits
[Desc("Armament names")] [Desc("Armament names")]
public readonly string[] Armaments = { "primary", "secondary" }; 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 Cursor = null;
public readonly string OutsideRangeCursor = null; public readonly string OutsideRangeCursor = null;

View File

@@ -40,7 +40,7 @@ namespace OpenRA.Mods.Common.Traits
var f = facing.Facing; var f = facing.Facing;
var pos = self.CenterPosition; 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 delta = targetedPosition - pos;
var facingToTarget = delta.HorizontalLengthSquared != 0 ? delta.Yaw.Facing : f; var facingToTarget = delta.HorizontalLengthSquared != 0 ? delta.Yaw.Facing : f;

View File

@@ -146,7 +146,7 @@ namespace OpenRA.Mods.Common.Traits
return; return;
var pos = self.CenterPosition; 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; var targetYaw = (targetedPosition - pos).Yaw;
foreach (var a in Armaments) foreach (var a in Armaments)

View File

@@ -131,7 +131,8 @@ namespace OpenRA.Mods.Common.Traits
return false; return false;
var pos = self.CenterPosition; 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; DesiredFacing = delta.HorizontalLengthSquared != 0 ? delta.Yaw.Facing : TurretFacing;
MoveTurret(); MoveTurret();
return HasAchievedDesiredFacing; return HasAchievedDesiredFacing;