Update scripts

This commit is contained in:
Soneoy_Mac
2024-04-16 03:40:27 -07:00
parent d26e1bba1f
commit b776940c48
40 changed files with 1113 additions and 0 deletions

View File

@@ -0,0 +1,69 @@
using Unity.Mathematics;
using UnityEngine;
namespace Timing
{
public class GameTime : MonoBehaviour
{
private double _lastCapturedDspTime;
public static bool Active { get; private set; }
public static double StartTime { get; private set; }
public static double TimeSinceClipStart { get; private set; }
private static NativeLinearRegression TimePrediction { get; set; }
private void Update()
{
if (!Active) return;
#region Smoothen DSP buffer time
if (AudioSettings.dspTime > _lastCapturedDspTime)
{
TimePrediction.Sample(new double2(Time.realtimeSinceStartupAsDouble, AudioSettings.dspTime));
_lastCapturedDspTime = AudioSettings.dspTime;
}
var smoothDspTime = TimePrediction.SampleCount < 2
? AudioSettings.dspTime
: TimePrediction.Predict(Time.realtimeSinceStartupAsDouble);
#endregion
TimeSinceClipStart = smoothDspTime - StartTime;
}
private void OnDestroy()
{
TimePrediction.Dispose();
}
/// <summary>
/// Tells the time manager to start counting time
/// </summary>
public static void StartFrom(double startTime)
{
StartTime = startTime;
TimeSinceClipStart = AudioSettings.dspTime - startTime;
Active = true;
TimePrediction ??= new NativeLinearRegression();
TimePrediction.Clear();
}
public static void Pause()
{
Active = false;
}
public static void UnPause(double timeSinceClipStart)
{
TimePrediction.Clear();
StartTime = AudioSettings.dspTime - timeSinceClipStart;
TimeSinceClipStart = timeSinceClipStart;
Active = true;
}
}
}

View File

@@ -0,0 +1,102 @@
using System;
using Unity.Burst;
using Unity.Collections;
using Unity.Jobs;
using Unity.Mathematics;
namespace Timing
{
public sealed class NativeLinearRegression : IDisposable
{
public int SampleCount { get; private set; }
public int MaxSampleCount { get; }
private NativeArray<double2> _samples;
public NativeLinearRegression(int maxSampleCount = 12)
{
MaxSampleCount = maxSampleCount;
_samples = new NativeArray<double2>(maxSampleCount, Allocator.Persistent);
}
public void Sample(double2 plot)
{
if (SampleCount < MaxSampleCount)
SampleCount++;
else
for (var i = 1; i < SampleCount; i++)
_samples[i - 1] = _samples[i];
_samples[SampleCount - 1] = plot;
}
public double Predict(in double x)
{
using var result = new NativeArray<double>(2, Allocator.TempJob);
var jobData = new LinearRegressionJob
{
samples = _samples.Slice(0, SampleCount),
yInterceptAndSlope = result
};
jobData.Schedule().Complete();
var yIntercept = result[0];
var slope = result[1];
return x * slope + yIntercept;
}
public void Clear()
{
SampleCount = 0;
}
public void Dispose()
{
_samples.Dispose();
}
}
[BurstCompile]
public struct LinearRegressionJob : IJob
{
[ReadOnly]
public NativeSlice<double2> samples;
[WriteOnly]
public NativeArray<double> yInterceptAndSlope;
public void Execute()
{
double sumOfX = 0;
double sumOfY = 0;
double sumOfXSq = 0;
double sumCoDeviates = 0;
var sampleCount = samples.Length;
for (var i = 0; i < sampleCount; i++)
{
var plot = samples[i];
sumCoDeviates += plot.x * plot.y;
sumOfX += plot.x;
sumOfY += plot.y;
sumOfXSq += plot.x * plot.x;
}
var ssX = sumOfXSq - sumOfX * sumOfX / sampleCount;
var sCo = sumCoDeviates - sumOfX * sumOfY / sampleCount;
var meanX = sumOfX / sampleCount;
var meanY = sumOfY / sampleCount;
// y-intercept
yInterceptAndSlope[0] = meanY - sCo / ssX * meanX;
// slope
yInterceptAndSlope[1] = sCo / ssX;
}
}
}

View File

@@ -0,0 +1,45 @@
using System.Collections;
using System.Collections.Generic;
using Helpers;
using UnityEngine;
using UnityEngine.UI;
namespace Title
{
public class TitleManager : MonoBehaviour
{
public Text anyKeyPressed;
public List<string> textList;
private int _textIndex;
private void Start()
{
textList = new List<string>
{
Localization.ParseAuto($"TITLE_ANYKEY"),
Localization.ParseAuto($"TITLE_THANKS"),
$"{Localization.ParseAuto($"TITLE_BUILDVERSION")} {Application.version}"
};
StartCoroutine(CycleThroughText());
}
private IEnumerator CycleThroughText()
{
while (true)
{
_textIndex = (_textIndex + 1) % textList.Count;
anyKeyPressed.text = textList[_textIndex];
yield return new WaitForSeconds(3);
}
// ReSharper disable once IteratorNeverReturns
}
public void ChangeScene()
{
SceneSwapper.LoadScene("ModeSelection");
}
}
}

View File

@@ -0,0 +1,84 @@
using UnityEngine;
namespace AstroDX.Utilities
{
public static class Trigonometry
{
public const float Tau = Mathf.PI * 2;
public static Vector2 Rotate(in this Vector2 v, in float degreesRad)
{
var magnitude = v.magnitude;
var originalDegrees = Mathf.Atan2(v.y, v.x);
var newDegrees = originalDegrees + degreesRad;
var newX = Mathf.Cos(newDegrees) * magnitude;
var newY = Mathf.Sin(newDegrees) * magnitude;
return new Vector2(newX, newY);
}
/// <summary>
/// Calculates a point's angle relative to a center point.
/// </summary>
/// <param name="position">The absolute position of a point.</param>
/// <param name="offset">The offset of the calculation's center point.</param>
/// <returns>The relative angle of a point from the given center point.</returns>
internal static float ToPolarAngle(in Vector2 position, in Vector2? offset = null)
{
if (!offset.HasValue)
return Mathf.Atan2(position.y, position.x);
var difference = position - offset.Value;
return Mathf.Atan2(difference.y, difference.x);
}
/// <summary>
/// Calculates the angle between a line to a point on a ring
/// and another line perpendicular from a tangent line of that point.
/// </summary>
/// <param name="adjacent"></param>
/// <param name="hypotenuse"></param>
/// <param name="clockwise"></param>
/// <returns></returns>
internal static float GetTangentAngleDelta(in float adjacent,
in float hypotenuse,
in bool clockwise)
{
var angleDiff = Mathf.Acos(adjacent / hypotenuse);
return clockwise ? -angleDiff : angleDiff;
}
/// <summary>
/// <para>
/// Calculates the angle between <c>startRotation</c> and <c>endRotation</c>,
/// given its traversing direction.
/// </para>
/// </summary>
/// <param name="startRotation">The starting rotation.</param>
/// <param name="endRotation">The ending rotation.</param>
/// <param name="clockwise">Traversing direction.</param>
/// <param name="wrapThreshold">
/// <para>Wraps to full circle for spans smaller than this value.</para>
/// <para><code>Tau / 4f</code> is recommended for offset circles</para>
/// </param>
/// <returns>
/// The span between the starting rotation and the ending rotation on a unit circle,
/// in radians.
/// </returns>
public static float GetAngleSpan(in float startRotation,
in float endRotation,
bool clockwise,
float wrapThreshold = Tau / 32f)
{
var span = clockwise
? (startRotation - endRotation + 2 * Tau) % Tau
: (endRotation - startRotation + 2 * Tau) % Tau;
if (span <= wrapThreshold)
span += Tau;
return span;
}
}
}