using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; public class PrepMover : MonoBehaviour { public AudioSource source; public List allHits = new List(); public List hitsQueue = new List(); public List targetPoints = new List(); public Vector2 speeds = new Vector2(); public DrawShape botShape; public DrawShape topShape; void Start(){ if(PrepConnector.saveLoadData != null){ allHits = new List { 0 }; allHits.AddRange(PrepConnector.saveLoadData.hits); hitsQueue = new List(); hitsQueue.AddRange(allHits); }else{ SceneManager.LoadScene("prep"); } ReadFuture(); } void ReadFuture(){ targetPoints = new List(); List botPoints = new List(); List topPoints = new List(); 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, ) */