ProjectSquareBall/Assets/Scripts/PrepMover.cs
2024-09-04 18:39:41 +05:30

111 lines
3.1 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class PrepMover : MonoBehaviour
{
public AudioSource source;
public List<float> allHits = new List<float>();
public List<float> hitsQueue = new List<float>();
public List<Vector3> targetPoints = new List<Vector3>();
public Vector2 speeds = new Vector2();
public DrawShape botShape;
public DrawShape topShape;
void Start(){
if(PrepConnector.saveLoadData != null){
allHits = new List<float>
{
0
};
allHits.AddRange(PrepConnector.saveLoadData.hits);
hitsQueue = new List<float>();
hitsQueue.AddRange(allHits);
}else{
SceneManager.LoadScene("prep");
}
ReadFuture();
}
void ReadFuture(){
targetPoints = new List<Vector3>();
List<Vector3> botPoints = new List<Vector3>();
List<Vector3> topPoints = new List<Vector3>();
for(int i=0; i < allHits.Count; i++){
float curT = allHits[i];
float curY = GetYPoint(i);
Vector3 thisPoint = new Vector3(curT * speeds.x, curY * speeds.y);
targetPoints.Add(thisPoint);
bool flipside= i %2 == 0;
if(flipside){
if (i > 2)
{
Vector3 prevPoint = targetPoints[i - 2];
float xMid = prevPoint.x + ((thisPoint.x - prevPoint.x) / (2f * 2));
float yMid = prevPoint.y + ((thisPoint.y - prevPoint.y) / 2f);
xMid = targetPoints[i - 1].x; ;
Vector3 newP1 = new Vector3(xMid, prevPoint.y);
Vector3 newP2 = new Vector3(xMid, thisPoint.y);
botPoints.Add(newP1);
botPoints.Add(newP2);
}
botPoints.Add(thisPoint);
}else{
topPoints.Add(thisPoint);
}
}
botShape.Draw(botPoints, new Vector3(0,-30));
topShape.Draw(topPoints, new Vector3(0,30));
}
public int curIndex =0;
public float curProg = 0;
void Update()
{
for(int i=0; i < allHits.Count; i++){
if(allHits[i] < source.time){
curIndex = i;
}else{
break;
}
}
float curTime = allHits[curIndex];
float nextTime = allHits[curIndex+1];
curProg = (source.time - curTime) / (nextTime-curTime);
transform.position = Vector3.Lerp(targetPoints[curIndex], targetPoints[curIndex+1], curProg);
}
float GetYPoint(int index){
float y = 0;
for(int i=0; i < index; i++){
float prevValue = i >0 ? allHits[i-1] : 0;
float curValue = allHits[i];
float diff = curValue - prevValue;
y += diff * (i %2 == 0 ? 1 : -1);
}
return y;
}
}
/*
Dry run;
times = (0, 1, 2, 2.5, 5, 10)
y = (0, 1, -1, 1.5, -3.5, 6.5)
addition(0, 1, )
*/