private void SetInterpolatedValues(KeyValuePair<PositionedObject, KeyFrameValues> currentPair, float percentage)
{
var currentValues = currentPair.Value;
var nextValues = NextKeyFrame == null
? currentValues
: NextKeyFrame.Values.ContainsKey(currentPair.Key)
? NextKeyFrame.Values[currentPair.Key]
: currentValues;
//var nextValues = NextKeyFrame.Values[currentPair.Key];
var currentObject = currentPair.Key;
if (currentValues.Parent == null)
{
currentObject.AttachTo(this, true);
}
else if (currentObject.Parent != currentValues.Parent)
{
currentObject.AttachTo(currentValues.Parent, true);
}
// Position
// In a single dimension, if the spriterobject is on x = 5, and the position to move a subobject to is x=8, then it should actually move to x=13, because we add the spriterobject's position to the subobject's interpolated value, since while positions are absolute, they are "absolute" relative to the container
currentObject.RelativePosition = Vector3.Lerp(currentValues.RelativePosition, nextValues.RelativePosition,
percentage);
if (float.IsNaN(currentObject.RelativePosition.X) ||
float.IsNaN(currentObject.RelativePosition.Y)
|| float.IsNaN(currentObject.RelativePosition.Z))
{
throw new Exception(string.Format("Float.IsNaN true! Object name {0} RelativePosition: {1}",
currentObject.Name, currentObject.RelativePosition));
}
// Angle
int spin = currentValues.Spin;
float angleA = currentValues.RelativeRotation.Z;
float angleB = nextValues.RelativeRotation.Z;
if (spin == 1 && angleB - angleA < 0)
{
angleB += 360f;
}
else if (spin == -1 && angleB - angleA > 0)
{
angleB -= 360f;
}
currentObject.RelativeRotationZ =
MathHelper.ToRadians(MathHelper.Lerp(angleA,
angleB, percentage));
// Sprite specific stuff
var sprite = currentObject as ScaledSprite;
if (sprite != null)
{
sprite.Texture = currentValues.Texture;
sprite.Alpha = MathHelper.Lerp(currentValues.Alpha, nextValues.Alpha, percentage);
}
var spo = currentObject as IRelativeScalable;
if (spo != null)
{
spo.RelativeScaleX = MathHelper.Lerp(currentValues.RelativeScaleX, nextValues.RelativeScaleX, percentage);
spo.RelativeScaleY = MathHelper.Lerp(currentValues.RelativeScaleY, nextValues.RelativeScaleY, percentage);
}
}