Separate IBodyOrientation from render traits.

This commit is contained in:
Paul Chote
2013-05-25 17:21:17 +12:00
parent 53aa698491
commit c149898592
16 changed files with 122 additions and 26 deletions

View File

@@ -226,6 +226,7 @@
<Compile Include="Widgets\ClientTooltipRegionWidget.cs" />
<Compile Include="Graphics\Renderable.cs" />
<Compile Include="Traits\Render\RenderSprites.cs" />
<Compile Include="Traits\BodyOrientation.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\OpenRA.FileFormats\OpenRA.FileFormats.csproj">

View File

@@ -0,0 +1,58 @@
#region Copyright & License Information
/*
* Copyright 2007-2011 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using OpenRA.Graphics;
using OpenRA.FileFormats;
namespace OpenRA.Traits
{
public class BodyOrientationInfo : ITraitInfo, IBodyOrientationInfo
{
[Desc("Camera pitch for rotation calculations")]
public readonly WAngle CameraPitch = WAngle.FromDegrees(40);
public object Create(ActorInitializer init) { return new BodyOrientation(init.self, this); }
}
public class BodyOrientation : IBodyOrientation
{
[Sync] public int QuantizedFacings { get; set; }
BodyOrientationInfo Info;
public BodyOrientation(Actor self, BodyOrientationInfo info)
{
Info = info;
}
public WAngle CameraPitch { get { return Info.CameraPitch; } }
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, -Info.CameraPitch.Sin()*vec.X/1024, vec.Z);
}
public WRot QuantizeOrientation(Actor self, WRot orientation)
{
// Quantization disabled
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));
}
}
}

View File

@@ -16,13 +16,8 @@ using OpenRA.FileFormats;
namespace OpenRA.Traits
{
public class RenderSimpleInfo : RenderSpritesInfo, IBodyOrientationInfo
public class RenderSimpleInfo : RenderSpritesInfo, Requires<IBodyOrientationInfo>
{
[Desc("Number of facings for gameplay calculations. -1 indiciates auto-detection from sequence")]
public readonly int QuantizedFacings = -1;
[Desc("Camera pitch the sprite was rendered with. Used to determine rotation ellipses")]
public readonly WAngle CameraPitch = WAngle.FromDegrees(40);
public override object Create(ActorInitializer init) { return new RenderSimple(init.self); }
public virtual IEnumerable<IRenderable> RenderPreview(ActorInfo ai, PaletteReference pr)
@@ -34,7 +29,7 @@ namespace OpenRA.Traits
}
}
public class RenderSimple : RenderSprites, IBodyOrientation, IAutoSelectionSize
public class RenderSimple : RenderSprites, IAutoSelectionSize
{
RenderSimpleInfo Info;
@@ -49,6 +44,7 @@ namespace OpenRA.Traits
: this(self, MakeFacingFunc(self))
{
anim.PlayRepeating("idle");
self.Trait<IBodyOrientation>().QuantizedFacings = anim.CurrentSequence.Facings;
}
public int2 SelectionSize(Actor self)
@@ -74,22 +70,5 @@ namespace OpenRA.Traits
anim.PlayThen(NormalizeSequence(self, name),
() => anim.PlayRepeating(NormalizeSequence(self, "idle")));
}
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, -Info.CameraPitch.Sin()*vec.X/1024, vec.Z);
}
public WRot QuantizeOrientation(Actor self, WRot orientation)
{
// Map yaw to the closest facing
var numDirs = Info.QuantizedFacings == -1 ? anim.CurrentSequence.Facings : Info.QuantizedFacings;
var facing = Util.QuantizeFacing(orientation.Yaw.Angle / 4, numDirs) * (256 / numDirs);
// Roll and pitch are always zero
return new WRot(WAngle.Zero, WAngle.Zero, WAngle.FromFacing(facing));
}
}
}

View File

@@ -189,6 +189,8 @@ namespace OpenRA.Traits
public interface IRenderAsTerrain { IEnumerable<IRenderable> RenderAsTerrain(WorldRenderer wr, Actor self); }
public interface IBodyOrientation
{
WAngle CameraPitch { get; }
int QuantizedFacings { get; set; }
WVec LocalToWorld(WVec vec);
WRot QuantizeOrientation(Actor self, WRot orientation);
}