Reorganize IBodyOrientation.

This commit is contained in:
Paul Chote
2014-07-20 17:29:47 +12:00
parent 8579ae956a
commit ef2f909f65
9 changed files with 80 additions and 37 deletions

View File

@@ -8,56 +8,76 @@
*/ */
#endregion #endregion
using System;
namespace OpenRA.Traits namespace OpenRA.Traits
{ {
public class BodyOrientationInfo : ITraitInfo, IBodyOrientationInfo public class BodyOrientationInfo : ITraitInfo, IBodyOrientationInfo
{ {
[Desc("Number of facings for gameplay calculations. -1 indiciates auto-detection from sequence")] [Desc("Number of facings for gameplay calculations. -1 indicates auto-detection from another trait")]
public readonly int QuantizedFacings = -1; public readonly int QuantizedFacings = -1;
[Desc("Camera pitch for rotation calculations")] [Desc("Camera pitch for rotation calculations")]
public readonly WAngle CameraPitch = WAngle.FromDegrees(40); public readonly WAngle CameraPitch = WAngle.FromDegrees(40);
public WVec LocalToWorld(WVec vec)
{
// RA's 2d perspective doesn't correspond to an orthonormal 3D
// coordinate system, so fudge the y axis to make things look good
return new WVec(vec.Y, -CameraPitch.Sin() * vec.X / 1024, vec.Z);
}
public WRot QuantizeOrientation(WRot orientation, int facings)
{
// Quantization disabled
if (facings == 0)
return orientation;
// Map yaw to the closest facing
var facing = Util.QuantizeFacing(orientation.Yaw.Angle / 4, facings) * (256 / facings);
// Roll and pitch are always zero if yaw is quantized
return new WRot(WAngle.Zero, WAngle.Zero, WAngle.FromFacing(facing));
}
public object Create(ActorInitializer init) { return new BodyOrientation(init.self, this); } public object Create(ActorInitializer init) { return new BodyOrientation(init.self, this); }
} }
public class BodyOrientation : IBodyOrientation public class BodyOrientation : IBodyOrientation
{ {
[Sync] public int QuantizedFacings { get; private set; } readonly BodyOrientationInfo info;
BodyOrientationInfo info; readonly Lazy<int> quantizedFacings;
[Sync] public int QuantizedFacings { get { return quantizedFacings.Value; } }
public BodyOrientation(Actor self, BodyOrientationInfo info) public BodyOrientation(Actor self, BodyOrientationInfo info)
{ {
this.info = info; this.info = info;
if (info.QuantizedFacings > 0)
QuantizedFacings = info.QuantizedFacings; quantizedFacings = Exts.Lazy(() =>
{
// Override value is set
if (info.QuantizedFacings >= 0)
return info.QuantizedFacings;
var qboi = self.Info.Traits.GetOrDefault<IQuantizeBodyOrientationInfo>();
if (qboi == null)
throw new InvalidOperationException("Actor type '" + self.Info.Name + "' does not define a quantized body orientation.");
return qboi.QuantizedBodyFacings(self.World.Map.SequenceProvider, self.Info);
});
} }
public WAngle CameraPitch { get { return info.CameraPitch; } } public WAngle CameraPitch { get { return info.CameraPitch; } }
public WVec LocalToWorld(WVec vec) public WVec LocalToWorld(WVec vec)
{ {
// RA's 2d perspective doesn't correspond to an orthonormal 3D return info.LocalToWorld(vec);
// coordinate system, so fudge the y axis to make things look good
return new WVec(vec.Y, -info.CameraPitch.Sin()*vec.X/1024, vec.Z);
} }
public WRot QuantizeOrientation(Actor self, WRot orientation) public WRot QuantizeOrientation(Actor self, WRot orientation)
{ {
// Quantization disabled return info.QuantizeOrientation(orientation, quantizedFacings.Value);
if (QuantizedFacings == 0)
return orientation;
// Map yaw to the closest facing
var facing = Util.QuantizeFacing(orientation.Yaw.Angle / 4, QuantizedFacings) * (256 / QuantizedFacings);
// Roll and pitch are always zero if yaw is quantized
return new WRot(WAngle.Zero, WAngle.Zero, WAngle.FromFacing(facing));
}
public void SetAutodetectedFacings(int facings)
{
if (info.QuantizedFacings < 0)
QuantizedFacings = facings;
} }
} }
} }

View File

@@ -227,15 +227,22 @@ namespace OpenRA.Traits
public interface IRenderShroud { void RenderShroud(WorldRenderer wr, Shroud shroud); } public interface IRenderShroud { void RenderShroud(WorldRenderer wr, Shroud shroud); }
public interface IPostRenderSelection { IEnumerable<IRenderable> RenderAfterWorld(WorldRenderer wr); } public interface IPostRenderSelection { IEnumerable<IRenderable> RenderAfterWorld(WorldRenderer wr); }
public interface IBodyOrientation public interface IBodyOrientation
{ {
WAngle CameraPitch { get; } WAngle CameraPitch { get; }
int QuantizedFacings { get; } int QuantizedFacings { get; }
WVec LocalToWorld(WVec vec); WVec LocalToWorld(WVec vec);
WRot QuantizeOrientation(Actor self, WRot orientation); WRot QuantizeOrientation(Actor self, WRot orientation);
void SetAutodetectedFacings(int facings);
} }
public interface IBodyOrientationInfo : ITraitInfo { }
public interface IBodyOrientationInfo : ITraitInfo
{
WVec LocalToWorld(WVec vec);
WRot QuantizeOrientation(WRot orientation, int facings);
}
public interface IQuantizeBodyOrientationInfo { int QuantizedBodyFacings(SequenceProvider sequenceProvider, ActorInfo ai); }
public interface ITargetableInfo public interface ITargetableInfo
{ {

View File

@@ -14,7 +14,7 @@ using OpenRA.Traits;
namespace OpenRA.Mods.RA.Render namespace OpenRA.Mods.RA.Render
{ {
class RenderGunboatInfo : RenderSpritesInfo, Requires<IBodyOrientationInfo> class RenderGunboatInfo : RenderSpritesInfo, IQuantizeBodyOrientationInfo, Requires<IBodyOrientationInfo>
{ {
[Desc("Turreted 'Turret' key to display")] [Desc("Turreted 'Turret' key to display")]
public readonly string Turret = "primary"; public readonly string Turret = "primary";
@@ -25,6 +25,11 @@ namespace OpenRA.Mods.RA.Render
public readonly string WakeRightSequence = "wake-right"; public readonly string WakeRightSequence = "wake-right";
public override object Create(ActorInitializer init) { return new RenderGunboat(init.self, this); } public override object Create(ActorInitializer init) { return new RenderGunboat(init.self, this); }
public int QuantizedBodyFacings(SequenceProvider sequenceProvider, ActorInfo ai)
{
return 2;
}
} }
class RenderGunboat : RenderSprites, INotifyDamageStateChanged class RenderGunboat : RenderSprites, INotifyDamageStateChanged
@@ -54,8 +59,6 @@ namespace OpenRA.Mods.RA.Render
var rightWake = new Animation(self.World, name); var rightWake = new Animation(self.World, name);
rightWake.Play(info.WakeRightSequence); rightWake.Play(info.WakeRightSequence);
Add(info.WakeRightSequence, new AnimationWithOffset(rightWake, null, () => facing.Facing <= 128, -87)); Add(info.WakeRightSequence, new AnimationWithOffset(rightWake, null, () => facing.Facing <= 128, -87));
self.Trait<IBodyOrientation>().SetAutodetectedFacings(2);
} }
public void DamageStateChanged(Actor self, AttackInfo e) public void DamageStateChanged(Actor self, AttackInfo e)

View File

@@ -49,8 +49,6 @@ namespace OpenRA.Mods.RA.Render
this.info = info; this.info = info;
DefaultAnimation.PlayRepeating(NormalizeSequence(self, "idle")); DefaultAnimation.PlayRepeating(NormalizeSequence(self, "idle"));
self.Trait<IBodyOrientation>().SetAutodetectedFacings(DefaultAnimation.CurrentSequence.Facings);
} }
public virtual void BuildingComplete(Actor self) public virtual void BuildingComplete(Actor self)

View File

@@ -8,7 +8,9 @@
*/ */
#endregion #endregion
using System.Linq;
using OpenRA.Mods.RA.Effects; using OpenRA.Mods.RA.Effects;
using OpenRA.Graphics;
using OpenRA.Traits; using OpenRA.Traits;
namespace OpenRA.Mods.RA.Render namespace OpenRA.Mods.RA.Render
@@ -25,6 +27,11 @@ namespace OpenRA.Mods.RA.Render
public readonly string[] StandAnimations = { "stand" }; public readonly string[] StandAnimations = { "stand" };
public override object Create(ActorInitializer init) { return new RenderInfantry(init.self, this); } public override object Create(ActorInitializer init) { return new RenderInfantry(init.self, this); }
public override int QuantizedBodyFacings(SequenceProvider sequenceProvider, ActorInfo ai)
{
return sequenceProvider.GetSequence(RenderSprites.GetImage(ai), StandAnimations.First()).Facings;
}
} }
public class RenderInfantry : RenderSimple, INotifyAttack, INotifyKilled, INotifyIdle public class RenderInfantry : RenderSimple, INotifyAttack, INotifyKilled, INotifyIdle
@@ -64,8 +71,6 @@ namespace OpenRA.Mods.RA.Render
DefaultAnimation.PlayFetchIndex(NormalizeInfantrySequence(self, info.StandAnimations.Random(Game.CosmeticRandom)), () => 0); DefaultAnimation.PlayFetchIndex(NormalizeInfantrySequence(self, info.StandAnimations.Random(Game.CosmeticRandom)), () => 0);
State = AnimationState.Waiting; State = AnimationState.Waiting;
move = self.Trait<IMove>(); move = self.Trait<IMove>();
self.Trait<IBodyOrientation>().SetAutodetectedFacings(DefaultAnimation.CurrentSequence.Facings);
} }
public void Attacking(Actor self, Target target) public void Attacking(Actor self, Target target)

View File

@@ -15,7 +15,7 @@ using OpenRA.Traits;
namespace OpenRA.Mods.RA.Render namespace OpenRA.Mods.RA.Render
{ {
public class RenderSimpleInfo : RenderSpritesInfo, ILegacyEditorRenderInfo, Requires<IBodyOrientationInfo> public class RenderSimpleInfo : RenderSpritesInfo, IQuantizeBodyOrientationInfo, ILegacyEditorRenderInfo, Requires<IBodyOrientationInfo>
{ {
public override object Create(ActorInitializer init) { return new RenderSimple(init.self); } public override object Create(ActorInitializer init) { return new RenderSimple(init.self); }
@@ -27,6 +27,11 @@ namespace OpenRA.Mods.RA.Render
return anim.Render(WPos.Zero, WVec.Zero, 0, pr, Scale); return anim.Render(WPos.Zero, WVec.Zero, 0, pr, Scale);
} }
public virtual int QuantizedBodyFacings(SequenceProvider sequenceProvider, ActorInfo ai)
{
return sequenceProvider.GetSequence(RenderSprites.GetImage(ai), "idle").Facings;
}
public string EditorPalette { get { return Palette; } } public string EditorPalette { get { return Palette; } }
public string EditorImage(ActorInfo actor) { return RenderSimple.GetImage(actor); } public string EditorImage(ActorInfo actor) { return RenderSimple.GetImage(actor); }
} }
@@ -46,7 +51,6 @@ namespace OpenRA.Mods.RA.Render
: this(self, MakeFacingFunc(self)) : this(self, MakeFacingFunc(self))
{ {
DefaultAnimation.PlayRepeating(NormalizeSequence(self, "idle")); DefaultAnimation.PlayRepeating(NormalizeSequence(self, "idle"));
self.Trait<IBodyOrientation>().SetAutodetectedFacings(DefaultAnimation.CurrentSequence.Facings);
} }
public int2 SelectionSize(Actor self) { return AutoSelectionSize(self); } public int2 SelectionSize(Actor self) { return AutoSelectionSize(self); }

4
OpenRA.Mods.RA/Render/WithVoxelBody.cs Executable file → Normal file
View File

@@ -16,11 +16,13 @@ using OpenRA.Traits;
namespace OpenRA.Mods.RA.Render namespace OpenRA.Mods.RA.Render
{ {
[Desc("Also returns a default selection size that is calculated automatically from the voxel dimensions.")] [Desc("Also returns a default selection size that is calculated automatically from the voxel dimensions.")]
public class WithVoxelBodyInfo : ITraitInfo, Requires<RenderVoxelsInfo> public class WithVoxelBodyInfo : ITraitInfo, IQuantizeBodyOrientationInfo, Requires<RenderVoxelsInfo>
{ {
public readonly string Sequence = "idle"; public readonly string Sequence = "idle";
public object Create(ActorInitializer init) { return new WithVoxelBody(init.self, this); } public object Create(ActorInitializer init) { return new WithVoxelBody(init.self, this); }
public int QuantizedBodyFacings(SequenceProvider sequenceProvider, ActorInfo ai) { return 0; }
} }
public class WithVoxelBody : IAutoSelectionSize public class WithVoxelBody : IAutoSelectionSize

View File

@@ -16,7 +16,7 @@ using OpenRA.Traits;
namespace OpenRA.Mods.RA.Render namespace OpenRA.Mods.RA.Render
{ {
public class WithVoxelUnloadBodyInfo : ITraitInfo, Requires<RenderVoxelsInfo> public class WithVoxelUnloadBodyInfo : ITraitInfo, IQuantizeBodyOrientationInfo, Requires<RenderVoxelsInfo>
{ {
[Desc("Voxel sequence name to use when docked to a refinery.")] [Desc("Voxel sequence name to use when docked to a refinery.")]
public readonly string UnloadSequence = "unload"; public readonly string UnloadSequence = "unload";
@@ -25,6 +25,8 @@ namespace OpenRA.Mods.RA.Render
public readonly string IdleSequence = "idle"; public readonly string IdleSequence = "idle";
public object Create(ActorInitializer init) { return new WithVoxelUnloadBody(init.self, this); } public object Create(ActorInitializer init) { return new WithVoxelUnloadBody(init.self, this); }
public int QuantizedBodyFacings(SequenceProvider sequenceProvider, ActorInfo ai) { return 0; }
} }
public class WithVoxelUnloadBody : IAutoSelectionSize public class WithVoxelUnloadBody : IAutoSelectionSize

View File

@@ -15,10 +15,12 @@ using OpenRA.Traits;
namespace OpenRA.Mods.RA.Render namespace OpenRA.Mods.RA.Render
{ {
public class WithVoxelWalkerBodyInfo : ITraitInfo, Requires<RenderVoxelsInfo>, Requires<IMoveInfo> public class WithVoxelWalkerBodyInfo : ITraitInfo, IQuantizeBodyOrientationInfo, Requires<RenderVoxelsInfo>, Requires<IMoveInfo>
{ {
public readonly int TickRate = 5; public readonly int TickRate = 5;
public object Create(ActorInitializer init) { return new WithVoxelWalkerBody(init.self, this); } public object Create(ActorInitializer init) { return new WithVoxelWalkerBody(init.self, this); }
public int QuantizedBodyFacings(SequenceProvider sequenceProvider, ActorInfo ai) { return 0; }
} }
public class WithVoxelWalkerBody : IAutoSelectionSize, ITick public class WithVoxelWalkerBody : IAutoSelectionSize, ITick