Predective algo

This commit is contained in:
2024-09-08 18:16:38 +05:30
parent cd837472b9
commit 7822aec907
26 changed files with 9189 additions and 592 deletions

View File

@@ -0,0 +1,44 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class MoverSettingsManager : MonoBehaviour
{
public Slider xSlider,ySlider;
public Button btnClose,btnSave,btnRestart;
public PredictiveMover mover;
public GameObject panel;
public GameObject activator;
void Start()
{
xSlider.onValueChanged.AddListener(OnSettingsChanged);
ySlider.onValueChanged.AddListener(OnSettingsChanged);
btnClose.onClick.AddListener(HideSettings);
btnRestart.onClick.AddListener(Restart);
m_onSettingsChanged();
}
void HideSettings(){
activator.SetActive(true);
panel.SetActive(false);
}
void OnSettingsChanged(float useless){
m_onSettingsChanged();
}
void m_onSettingsChanged(){
mover.multipliers.x = xSlider.value;
mover.multipliers.y = ySlider.value;
mover.RegenerateLines();
}
void Restart(){
mover.Restart();
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 00de3ca2780fc9d4383d675e3bb35bf7
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,30 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class MoverSettingsPanel : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
float touchdownTimer =0f;
public void OnPointerDown(PointerEventData eventData)
{
touchdownTimer =0f;
}
public void OnPointerUp(PointerEventData eventData)
{
if(touchdownTimer > 4){
settingsPanel.SetActive(true);
}
}
public GameObject settingsPanel;
void Update()
{
if(touchdownTimer < 5f){
touchdownTimer +=Time.deltaTime;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 17228f1c211f8ed4db8114fd85f6b2d8
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,123 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PredictiveMover : MonoBehaviour
{
private Vector3[] points;
public List<float> allHits = new List<float>();
public LineRenderer line;
public GameObject objectToMove;
public AudioSource source;
public float audioOffset = -0.05f;
public Vector2 multipliers;
public AnimationCurve lerpCurve;
public DrawShape botShape, topShape;
void OnDrawGizmos()
{
RegenerateLines();
}
public void RegenerateLines()
{
points = new Vector3[allHits.Count];
for (int i = 0; i < points.Length; i++)
{
points[i] = GetPosition(i);
}
line.positionCount = points.Length;
line.SetPositions(points);
}
void Start(){
Restart();
}
void FixedUpdate(){
// RegenerateLines();
objectToMove.transform.position = Vector3.Lerp(objectToMove.transform.position,GetPositionAtTime(source.time + audioOffset),0.5f);
CamFollower.instance.UpdateFrame();
}
public void Restart(){
source.time =0;
source.Play();
RegenerateLines();
List<Vector3> botPoints = new List<Vector3>();
List<Vector3> topPoints = new List<Vector3>();
for(int i=0; i < points.Length; i++){
if(i%2 == 0){
botPoints.Add(points[i]);
}else{
topPoints.Add(points[i]);
}
}
botShape.Draw(botPoints, new Vector3(0, 30));
topShape.Draw(topPoints, new Vector3(0, -30));
}
Vector3 GetPosition(int index)
{
float hitX = allHits[index];
float diff = 0;
float prevY = 0;
// Calculate diff and prevY if the index is greater than 0
if (index > 0)
{
diff = allHits[index] - allHits[index - 1];
prevY = points[index - 1].y; // Use the already calculated point to avoid recursion
}
float flipSide = index % 2 == 0 ? 1 : -1;
float y = prevY + (flipSide * diff * multipliers.y);
return new Vector3(hitX * multipliers.x, y, 0);
}
public float bouncingIntensity = 0.5f;
public Vector3 GetPositionAtTime(float time)
{
if (points == null || points.Length < 2)
{
return Vector3.zero; // Return zero if points are not set up
}
// If the time is before the first hit, return the first position
if (time <= allHits[0])
{
return points[0];
}
// If the time is after the last hit, return the last position
if (time >= allHits[allHits.Count - 1])
{
return points[points.Length - 1];
}
// Find the current segment based on the given time
for (int i = 0; i < allHits.Count - 1; i++)
{
if (time >= allHits[i] && time < allHits[i + 1])
{
Vector3 startPoint = points[i];
Vector3 endPoint = points[i + 1];
float segmentDuration = allHits[i + 1] - allHits[i];
float t = (time - allHits[i]) / segmentDuration;
float curvedVal = lerpCurve.Evaluate(t);
return Vector3.Lerp(startPoint, endPoint,curvedVal);
}
}
return Vector3.zero; // Return zero if no valid segment is found (should not happen)
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 3a0f01b5b35bc4d46bbe9ec3a2c09a68
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: