Files
OpenRA/OpenRa.Game/Traits/Util.cs
2009-10-31 14:46:04 +13:00

79 lines
2.1 KiB
C#
Executable File

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OpenRa.Game.Traits
{
static class Util
{
public static void TickFacing( ref int facing, int desiredFacing, int rot )
{
var leftTurn = ( facing - desiredFacing ) & 0xFF;
var rightTurn = ( desiredFacing - facing ) & 0xFF;
if( Math.Min( leftTurn, rightTurn ) < rot )
facing = desiredFacing & 0xFF;
else if( rightTurn < leftTurn )
facing = ( facing + rot ) & 0xFF;
else
facing = ( facing - rot ) & 0xFF;
}
static float2[] fvecs = Graphics.Util.MakeArray<float2>( 32,
i => -float2.FromAngle( i / 16.0f * (float)Math.PI ) * new float2( 1f, 1.3f ) );
public static int GetFacing( float2 d, int currentFacing )
{
if( float2.WithinEpsilon( d, float2.Zero, 0.001f ) )
return currentFacing;
int highest = -1;
float highestDot = -1.0f;
for( int i = 0 ; i < fvecs.Length ; i++ )
{
float dot = float2.Dot( fvecs[ i ], d );
if( dot > highestDot )
{
highestDot = dot;
highest = i;
}
}
return highest * 8;
}
public static int GetNearestFacing( int facing, int desiredFacing )
{
var turn = desiredFacing - facing;
if( turn > 128 )
turn -= 256;
if( turn < -128 )
turn += 256;
return facing + turn;
}
public static int2 GetTurretPosition(Actor self, int[] offset)
{
var ru = self.traits.WithInterface<RenderUnit>().FirstOrDefault();
if (ru == null) return int2.Zero; /* things that don't have a rotating base don't need the turrets repositioned */
var bodyFacing = self.traits.Get<Mobile>().facing;
var quantizedFacing = bodyFacing - bodyFacing % ru.anim.CurrentSequence.Length;
var angle = (quantizedFacing / 256f) * (2 * (float)Math.PI);
var sinAngle = Math.Sin(angle);
var cosAngle = Math.Cos(angle);
var pos = new int2(
(int)(cosAngle * offset[0] + sinAngle * offset[1]),
(int)(cosAngle * offset[1] - sinAngle * offset[0]));
pos.Y = (int)(.7f * pos.Y);
return pos;
}
}
}