Files
GoakeFlax/Source/Game/WeaponSway.cs

238 lines
7.1 KiB
C#

using System;
using System.Collections.Generic;
using FlaxEngine;
using Console = Cabrito.Console;
namespace Game
{
public class WeaponSway : Script
{
private Actor rootActor;
private Actor cameraHolder;
public override void OnStart()
{
rootActor = Actor.Parent.GetChild("RootActor");
cameraHolder = rootActor.GetChild("CameraHolder");
Actor.LocalOrientation = GetRotation();
}
float easeInSine( float t ) {
return Mathf.Sin( 1.5707963f * t );
}
float easeOutSine( float t ) {
return 1 + Mathf.Sin( 1.5707963f * (--t) );
}
float easeInOutSine( float t ) {
return 0.5f * (1 + Mathf.Sin( 3.1415926f * (t - 0.5f) ) );
}
float easeInQuad( float t ) {
return t * t;
}
float easeOutQuad( float t ) {
return t * (2 - t);
}
float easeInOutQuad( float t ) {
return t < 0.5 ? 2 * t * t : t * (4 - 2 * t) - 1;
}
float easeInCubic( float t ) {
return t * t * t;
}
float easeOutCubic( float t ) {
return 1 + (--t) * t * t;
}
float easeInOutCubic( float t ) {
return t < 0.5 ? 4 * t * t * t : 1 + (--t) * (2 * (--t)) * (2 * t);
}
float easeInQuart( float t ) {
t *= t;
return t * t;
}
float easeOutQuart( float t ) {
t = (--t) * t;
return 1 - t * t;
}
float easeInOutQuart( float t ) {
if( t < 0.5 ) {
t *= t;
return 8 * t * t;
} else {
t = (--t) * t;
return 1 - 8 * t * t;
}
}
float easeInQuint( float t ) {
float t2 = t * t;
return t * t2 * t2;
}
float easeOutQuint( float t ) {
float t2 = (--t) * t;
return 1 + t * t2 * t2;
}
float easeInOutQuint( float t ) {
float t2;
if( t < 0.5 ) {
t2 = t * t;
return 16 * t * t2 * t2;
} else {
t2 = (--t) * t;
return 1 + 16 * t * t2 * t2;
}
}
float easeInExpo( float t ) {
return (Mathf.Pow( 2, 8 * t ) - 1) / 255;
}
float easeOutExpo( float t ) {
return 1 - Mathf.Pow( 2, -8 * t );
}
float easeInOutExpo( float t ) {
if( t < 0.5 ) {
return (Mathf.Pow( 2, 16 * t ) - 1) / 510;
} else {
return 1 - 0.5f * Mathf.Pow( 2, -16 * (t - 0.5f) );
}
}
float easeInCirc( float t ) {
return 1 - Mathf.Sqrt( 1 - t );
}
float easeOutCirc( float t ) {
return Mathf.Sqrt( t );
}
float easeInOutCirc( float t ) {
if( t < 0.5 ) {
return (1 - Mathf.Sqrt( 1 - 2 * t )) * 0.5f;
} else {
return (1 + Mathf.Sqrt( 2 * t - 1 )) * 0.5f;
}
}
float easeInBack( float t ) {
return t * t * (2.70158f * t - 1.70158f);
}
float easeOutBack( float t ) {
return 1 + (--t) * t * (2.70158f * t + 1.70158f);
}
float easeInOutBack( float t ) {
if( t < 0.5 ) {
return t * t * (7 * t - 2.5f) * 2;
} else {
return 1 + (--t) * t * 2 * (7 * t + 2.5f);
}
}
float easeInElastic( float t ) {
float t2 = t * t;
return t2 * t2 * Mathf.Sin( t * (float)Math.PI * 4.5f );
}
float easeOutElastic( float t ) {
float t2 = (t - 1) * (t - 1);
return 1 - t2 * t2 * Mathf.Cos( t * (float)Math.PI * 4.5f );
}
float easeInOutElastic( float t ) {
float t2;
if( t < 0.45 ) {
t2 = t * t;
return 8 * t2 * t2 * Mathf.Sin( t * (float)Math.PI * 9 );
} else if( t < 0.55 ) {
return 0.5f + 0.75f * Mathf.Sin( t * (float)Math.PI * 4 );
} else {
t2 = (t - 1) * (t - 1);
return 1 - 8 * t2 * t2 * Mathf.Sin( t * (float)Math.PI * 9 );
}
}
float easeInBounce( float t ) {
return Mathf.Pow( 2, 6 * (t - 1) ) * Mathf.Abs( Mathf.Sin( t * (float)Math.PI * 3.5f ) );
}
float easeOutBounce( float t ) {
return 1 - Mathf.Pow( 2, -6 * t ) * Mathf.Abs( Mathf.Cos( t * (float)Math.PI * 3.5f ) );
}
float easeInOutBounce( float t ) {
if( t < 0.5 ) {
return 8 * Mathf.Pow( 2, 8 * (t - 1) ) * Mathf.Abs( Mathf.Sin( t * (float)Math.PI * 7 ) );
} else {
return 1 - 8 * Mathf.Pow( 2, -8 * t ) * Mathf.Abs( Mathf.Sin( t * (float)Math.PI * 7 ) );
}
}
public float swaySpeed = 15f;
private Quaternion GetRotation()
{
Quaternion pitch = cameraHolder.LocalOrientation;
Quaternion yawRoll = rootActor.LocalOrientation;
return yawRoll * pitch;
}
public override void OnLateUpdate()
{
//easeInQuad
Quaternion rotation = GetRotation();
Vector3 targetAngles = rotation.EulerAngles;
Vector3 angles = Actor.LocalOrientation.EulerAngles;
float swaySpeedScaled = swaySpeed * Time.DeltaTime;
const float maxAngle = 30f;
float deltaX = Mathf.DeltaAngle(angles.X, targetAngles.X);
float deltaY = Mathf.DeltaAngle(angles.Y, targetAngles.Y);
float deltaZ = Mathf.DeltaAngle(angles.Z, targetAngles.Z);
if (deltaX > maxAngle)
angles.X -= maxAngle - deltaX;
else if (deltaX < -maxAngle)
angles.X += maxAngle + deltaX;
if (deltaY > maxAngle)
angles.Y -= maxAngle - deltaY;
else if (deltaY < -maxAngle)
angles.Y += maxAngle + deltaY;
if (deltaZ > maxAngle)
angles.Z -= maxAngle - deltaZ;
else if (deltaZ < -maxAngle)
angles.Z += maxAngle + deltaZ;
float percX = Mathf.Abs(deltaX) / maxAngle;
float percY = Mathf.Abs(deltaY) / maxAngle;
float percZ = Mathf.Abs(deltaZ) / maxAngle;
float minSpeed = swaySpeedScaled * 0.00001f;
Func<float, float> fun = (f) => f*f;
angles.X = Mathf.MoveTowardsAngle(angles.X, targetAngles.X, Math.Max(swaySpeedScaled*fun(percX), minSpeed));
angles.Y = Mathf.MoveTowardsAngle(angles.Y, targetAngles.Y, Math.Max(swaySpeedScaled*fun(percY), minSpeed));
angles.Z = Mathf.MoveTowardsAngle(angles.Z, targetAngles.Z, Math.Max(swaySpeedScaled*fun(percZ), minSpeed));
//Actor.LocalOrientation = Quaternion.Lerp(Actor.LocalOrientation, rotation, Math.Min(1.0f, easeInCubic(swaySpeed * (1.0f/120f))));
Actor.LocalOrientation = Quaternion.Euler(angles);
}
}
}