weapon sway

This commit is contained in:
2022-04-10 13:52:40 +03:00
parent 210feaa48d
commit 377f6d1bf7
13 changed files with 427 additions and 195 deletions

View File

@@ -82,20 +82,18 @@ namespace Game
{
// Check if this audio has multiple variations
List<AudioClip> audioClips = new List<AudioClip>();
int index = 1;
do
for (int i = 1; i<50; i++)
{
// TODO: make this more efficient, maybe get a list of assets and filter by name
audioClip = Content.Load<AudioClip>(Path.Combine(audioBasePath, soundName + "_var" + index + ".flax"));
if (audioClip != null)
audioClips.Add(audioClip);
index++;
} while (audioClip != null);
// TODO: make this more efficient, maybe get a list of assets and filter by name?
AudioClip audioClipVariation = Content.Load<AudioClip>(Path.Combine(audioBasePath, soundName + "_var" + i + ".flax"));
if (audioClipVariation == null)
break;
audioClips.Add(audioClipVariation);
}
if (audioClips.Count > 0)
{
audio.AudioClips = audioClips.ToArray();
}
else
Console.PrintError("AudioClip '" + soundName + "' not found");
}

View File

@@ -21,16 +21,21 @@ namespace Cabrito
double updateTimeAvg = 0.0;
ulong updateTimeCount;
const double updateInterval = 0.25;
double updateAccumTime = 0.0;
Stopwatch sw2;
double drawTimeAvg = 0.0;
ulong drawTimeCount;
const double drawInterval = 0.25;
double drawAccumTime = 0.0;
Stopwatch sw3;
double physicsTimeAvg = 0.0;
ulong physicsTimeCount;
const double physicsInterval = 0.25;
double physicsAccumTime = 0.0;
Stopwatch sw0;
string currentRenderer = "Unknown";
@@ -43,10 +48,12 @@ namespace Cabrito
label = (Label) control.Control;
sw = Stopwatch.StartNew();
sw2 = Stopwatch.StartNew();
sw3 = Stopwatch.StartNew();
sw0 = Stopwatch.StartNew();
currentRenderer = GPUDevice.Instance.RendererType.ToString();
sw2 = Stopwatch.StartNew();
if (t == null)
{
//Destroy(t);
@@ -54,8 +61,6 @@ namespace Cabrito
t.Render += OnDraw;
}
sw3 = Stopwatch.StartNew();
var settings = FlaxEditor.Content.Settings.GameSettings.Load();
timeSettings = settings.Time.CreateInstance<FlaxEditor.Content.Settings.TimeSettings>();
}
@@ -70,6 +75,7 @@ namespace Cabrito
public override void OnUpdate()
{
updateAccumTime += Time.DeltaTime;
updateTimeCount++;
double elapsed = sw.Elapsed.TotalSeconds;
if (elapsed >= updateInterval)
@@ -85,9 +91,13 @@ namespace Cabrito
StringBuilder sb = new StringBuilder();
sb.Append("eFPS: " + Engine.FramesPerSecond);
sb.Append("\nuFPS: " + ((int) Math.Round(1.0f / updateTimeAvg)).ToString());
sb.Append("\nrFPS: " + ((int) Math.Round(1.0f / drawTimeAvg)).ToString());
sb.Append("\npFPS: " + ((int) Math.Round(1.0f / physicsTimeAvg)).ToString());
sb.Append(" uTime: " + sw0.Elapsed.TotalSeconds);
sb.Append("\nuFPS: " + ((int) Math.Round(1.0f / updateTimeAvg)));
sb.Append(" uTime: " + updateAccumTime);
sb.Append("\nrFPS: " + ((int) Math.Round(1.0f / drawTimeAvg)));
sb.Append(" rTime: " + drawAccumTime);
sb.Append("\npFPS: " + ((int) Math.Round(1.0f / physicsTimeAvg)));
sb.Append(" pTime: " + physicsAccumTime);
//sb.Append("\nCon: " + conTime.ToString() + "ms");
//sb.Append("\nGC memory: " + (GC.GetTotalMemory(false) / 1000000.0f).ToString() + "MB");
//sb.Append("\nUpdate profiler: " + updateProfTime.ToString() + "ms");
@@ -148,6 +158,7 @@ namespace Cabrito
public override void OnFixedUpdate()
{
physicsAccumTime += Time.DeltaTime;
physicsTimeCount++;
double elapsed = sw3.Elapsed.TotalSeconds;
if (elapsed >= physicsInterval)
@@ -160,6 +171,7 @@ namespace Cabrito
void OnDraw(RenderTask tt, GPUContext context)
{
drawAccumTime += Time.DeltaTime;
drawTimeCount++;
double elapsed = sw2.Elapsed.TotalSeconds;
if (elapsed >= drawInterval)

View File

@@ -1,4 +1,5 @@
using FlaxEngine;
using FlaxEditor.Content.Settings;
using FlaxEngine;
using Console = Cabrito.Console;
namespace Game
@@ -89,6 +90,16 @@ namespace Game
public override void OnUpdate()
{
#if FLAX_EDITOR
if (Input.GetKeyDown(KeyboardKeys.F7))
{
var physicsSettings = GameSettings.Load<PhysicsSettings>();
physicsSettings.EnableSubstepping = !physicsSettings.EnableSubstepping;
GameSettings.Save(physicsSettings);
//GameSettings.Apply();
}
#endif
if (!camera.IsActive)
return;
if (texture == null)

View File

@@ -30,6 +30,8 @@ namespace Game
{
[Limit(0, 9000), Tooltip("Base Movement speed")]
public float MoveSpeed { get; set; } = 320;
private static Vector3 Gravity { get; set; } = new Vector3(0, -800.0f, 0f);
private float viewPitch;
private float viewYaw;
@@ -54,7 +56,7 @@ namespace Game
base.OnAwake();
bool record = false;
//record = true;
record = true;
if (record)
{
@@ -62,8 +64,8 @@ namespace Game
}
else
{
input = new PlayerInputLocal();
//input = new PlayerInputDemo(@"C:\dev\GoakeFlax\testdemo.gdem"); //playback
//input = new PlayerInputLocal();
input = new PlayerInputDemo(@"C:\dev\GoakeFlax\testdemo.gdem"); //playback
//input = new PlayerInputDemo(@"C:\dev\GoakeFlax\testdemo_desync.gdem"); //playback
}
@@ -184,6 +186,10 @@ namespace Game
if (input is PlayerInputDemo)
input.OnUpdate();
float deltadif = Time.DeltaTime - (1.0f / Time.PhysicsFPS);
if (Math.Abs(deltadif) > 0.0001f)
Console.Print("drift: " + deltadif);
input.OnFixedUpdate();
PlayerInputState inputState = input.GetCurrentInputState();
@@ -525,7 +531,7 @@ namespace Game
// hit something, try to step up
if (onGround)
{
Vector3 stepDelta = -Physics.Gravity.Normalized * stepSize;
Vector3 stepDelta = -Gravity.Normalized * stepSize;
Vector3 slidePosition = position;
Vector3 slideVelocity = velocity;
@@ -544,11 +550,11 @@ namespace Game
// step down
Vector3 stepDown = position - stepDelta;
TraceInfo traceDown = TracePlayer(actor, position, stepDown);
if (traceDown.fraction < 1f && -Vector3.Dot(Physics.Gravity.Normalized, traceDown.hitNormal) < slopeNormal)
if (traceDown.fraction < 1f && -Vector3.Dot(Gravity.Normalized, traceDown.hitNormal) < slopeNormal)
{
// can't step down, slide move like normally
Console.Print("no stepping 1, frac: " + traceDown.fraction + ", dot: " +
(-Vector3.Dot(Physics.Gravity.Normalized, traceDown.hitNormal)) +
(-Vector3.Dot(Gravity.Normalized, traceDown.hitNormal)) +
", norm: " + traceDown.hitNormal);
position = slidePosition;
velocity = slideVelocity;
@@ -562,8 +568,8 @@ namespace Game
position.Y += collisionMargin;
// ??
var d1 = -Vector3.Dot(Physics.Gravity.Normalized, position);
var d2 = -Vector3.Dot(Physics.Gravity.Normalized, originalPosition);
var d1 = -Vector3.Dot(Gravity.Normalized, position);
var d2 = -Vector3.Dot(Gravity.Normalized, originalPosition);
if (d1 < d2)
{
//Console.Print("no stepping 2, " + d1 + " < " + d2);
@@ -818,13 +824,13 @@ namespace Game
// categorize position
onGround = true;
Vector3 groundDelta = Physics.Gravity.Normalized;//Physics.Gravity.Normalized * (collisionMargin * 2);
Vector3 groundDelta = Gravity.Normalized;//Gravity.Normalized * (collisionMargin * 2);
//if (velocity.Y < 0f)
// groundDelta = Physics.Gravity.Normalized * velocity.Y * Time.DeltaTime;
// groundDelta = Gravity.Normalized * velocity.Y * Time.DeltaTime;
TraceInfo traceGround = TracePlayer(Actor, position, position + groundDelta);
if (!traceGround.startSolid && traceGround.fraction < 1f &&
-Vector3.Dot(Physics.Gravity.Normalized, traceGround.hitNormal) < slopeNormal)
-Vector3.Dot(Gravity.Normalized, traceGround.hitNormal) < slopeNormal)
{
//Console.Print("slope?");
// slope
@@ -845,7 +851,7 @@ namespace Game
}
if (!traceGround.startSolid && (traceGround.fraction >= 1f ||
-Vector3.Dot(Physics.Gravity.Normalized, traceGround.hitNormal) < slopeNormal))
-Vector3.Dot(Gravity.Normalized, traceGround.hitNormal) < slopeNormal))
{
// falling or sliding down a slope
onGround = false;
@@ -873,7 +879,7 @@ namespace Game
{
// reset velocity from gravity
if (-Vector3.Dot(Physics.Gravity.Normalized, velocity) < 0 &&
if (-Vector3.Dot(Gravity.Normalized, velocity) < 0 &&
Vector3.Dot(velocity, traceGround.hitNormal) < -0.1)
{
velocity = Vector3.ProjectOnPlane(velocity, traceGround.hitNormal);
@@ -949,7 +955,7 @@ namespace Game
// PM_Aircontrol(wishdir, wishspeedAirControl);
// apply gravity
velocity += Physics.Gravity * Time.DeltaTime;
velocity += Gravity * Time.DeltaTime;
//Console.Print(Time.DeltaTime.ToString());
}

205
Source/Game/WeaponSway.cs Normal file
View File

@@ -0,0 +1,205 @@
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 ) );
}
}
private Quaternion targetRotation;
private Quaternion oldRotation;
private Quaternion accumRotation;
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();
Actor.LocalOrientation = Quaternion.Lerp(Actor.LocalOrientation, rotation, Math.Min(1.0f, easeInCubic(swaySpeed * Time.DeltaTime)));
}
}
}