Introduce world offsets for turrets & armaments.

This commit is contained in:
Paul Chote
2013-03-25 20:33:32 +13:00
parent 27d852a425
commit bb6a1ed6e0
4 changed files with 163 additions and 38 deletions

View File

@@ -8,6 +8,7 @@
*/
#endregion
using System;
using System.Collections.Generic;
using OpenRA.Mods.RA.Render;
using OpenRA.FileFormats;
@@ -24,6 +25,10 @@ namespace OpenRA.Mods.RA
public readonly int[] LegacyOffset = {0,0};
public readonly bool AlignWhenIdle = false;
public readonly CoordinateModel OffsetModel = CoordinateModel.Legacy;
[Desc("Muzzle position relative to turret or body. (forward, right, up) triples")]
public readonly WVec Offset = WVec.Zero;
public virtual object Create(ActorInitializer init) { return new Turreted(init, this); }
}
@@ -31,10 +36,17 @@ namespace OpenRA.Mods.RA
{
[Sync] public int turretFacing = 0;
public int? desiredFacing;
public TurretedInfo info;
TurretedInfo info;
protected Turret turret;
IFacing facing;
// For subclasses that want to move the turret relative to the body
protected WVec LocalOffset = WVec.Zero;
public WVec Offset { get { return info.Offset + LocalOffset; } }
public string Name { get { return info.Turret; } }
public CoordinateModel CoordinateModel { get { return info.OffsetModel; } }
public static int GetInitialTurretFacing(ActorInitializer init, int def)
{
if (init.Contains<TurretFacingInit>())
@@ -62,7 +74,7 @@ namespace OpenRA.Mods.RA
public bool FaceTarget(Actor self, Target target)
{
desiredFacing = Util.GetFacing( target.CenterLocation - self.CenterLocation, turretFacing );
desiredFacing = Util.GetFacing(target.CenterLocation - self.CenterLocation, turretFacing);
return turretFacing == desiredFacing;
}
@@ -74,8 +86,30 @@ namespace OpenRA.Mods.RA
public PVecFloat PxPosition(Actor self, IFacing facing)
{
// Hack for external code unaware of world coordinates
if (info.OffsetModel == CoordinateModel.World)
return (PVecFloat)PPos.FromWPosHackZ(WPos.Zero + Position(self)).ToFloat2();
return turret.PxPosition(self, facing);
}
// Turret offset in world-space
public WVec Position(Actor self)
{
if (info.OffsetModel != CoordinateModel.World)
throw new InvalidOperationException("Turreted.Position requires a world coordinate offset");
var coords = self.Trait<ILocalCoordinatesModel>();
var bodyOrientation = coords.QuantizeOrientation(self, self.Orientation);
return coords.LocalToWorld(Offset.Rotate(bodyOrientation));
}
// Orientation in unit-space
public WRot LocalOrientation(Actor self)
{
// Hack: turretFacing is relative to the world, so subtract the body yaw
return WRot.FromYaw(WAngle.FromFacing(turretFacing) - self.Orientation.Yaw);
}
}
public class Turret