using Newtonsoft.Json; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.U2D; public class Mover : MonoBehaviour { public Vector3 movingSpeed; public float loudBoost = 1f; public AudioSplitter splitter; public GameObject prefab; float prev_loud = 0; public float loudnessThreshold = 0.08f; public float yieldTime = 0.2f; float LastTime; public SpriteShapeController botShape; public SpriteShapeController topShape; public LineRenderer botLineRenderer, topLineRenderer; public DrawShape botDrawer; public DrawShape topDrawer; public SpriteRenderer insideFill; public float glowReductionSpeed = 2f; [SerializeField] public List currentSteps = new List(); [SerializeField] List recordedSteps = new List(); public string currentStepsJson = ""; public string recordedStepsJson; public bool playReplay = false; float defFOV; private void Awake() { defFOV = Camera.main.orthographicSize; } private void Start() { // Application.targetFrameRate = 164; if(recordedStepsJson.Length > 1) { recordedSteps = JsonConvert.DeserializeObject>(recordedStepsJson); GenerateRecordedPlatform(); } } Vector3 LastPos; float movingSpeedMultiplier = 1; public bool manualFlip = true; public bool manualFlipX = false; void Update() { if (insideFill.transform.localScale.x > 0) { insideFill.transform.localScale = Vector3.one * (insideFill.transform.localScale.x - (Time.deltaTime * glowReductionSpeed)); } insideFill.color = new Color(1,1,1, insideFill.transform.localScale.y); Camera.main.orthographicSize = Mathf.Lerp(Camera.main.orthographicSize, defFOV, 3 * Time.deltaTime); if (!manualFlip) { return; } if(Input.GetKeyDown(KeyCode.LeftArrow)) { manualFlipX = false; movingSpeed.x = -Mathf.Abs(movingSpeed.x); }else if (Input.GetKeyDown(KeyCode.RightArrow)) { manualFlipX = false; movingSpeed.x = Mathf.Abs(movingSpeed.x); }else if (Input.GetKeyDown(KeyCode.UpArrow)) { manualFlipX = true; movingSpeed.y = Mathf.Abs(movingSpeed.y); }else if (Input.GetKeyDown(KeyCode.DownArrow)) { manualFlipX = true; movingSpeed.y = -Mathf.Abs(movingSpeed.y); } } void FixedUpdate() { movingSpeedMultiplier = Mathf.Lerp(movingSpeedMultiplier, 1, Time.deltaTime * 2); transform.Translate(movingSpeed*Time.deltaTime * movingSpeedMultiplier); if(recordedStepsJson.Length > 1 && playReplay) { PostWork(); } else { PreWork(); } } void GenerateRecordedPlatform() { List botPoints = GetBottomPoints(); botLineRenderer.positionCount = botPoints.Count; botLineRenderer.SetPositions(botPoints.ToArray()); List topPoints = GetTopPoints(); topLineRenderer.positionCount = topPoints.Count; topLineRenderer.SetPositions(topPoints.ToArray()); botDrawer.Draw(botPoints, new Vector3(0, 30)); topDrawer.Draw(topPoints, new Vector3(0,-30)); } void FixSpriteShape(SpriteShapeController ssc) { if (ssc.spriteShape != null && ssc.spriteShape.fillTexture != null) { // Bake the mesh after setting up the spline ssc.BakeMesh(); } else { Debug.LogError("SpriteShape or fillTexture is missing."); } ssc.BakeMesh(); ssc.GetComponent().enabled = true; } List bottomPoints = new List(); List topPoints = new List(); public List GetBottomPoints() { List botPoints = new List(); for (int i = 0; i < recordedSteps.Count; i++) { Vector3 point = recordedSteps[i].point; if (i % 2 == 0) { if (i > 2) { Vector3 prevPoint = recordedSteps[i-2].point; float xMid = prevPoint.x + ((point.x - prevPoint.x) / (2f *xFactorBot)); xMid = recordedSteps[i - 1].point.x; float yMid = prevPoint.y + ((point.y - prevPoint.y) / 2f); Vector3 newP1 = new Vector3(xMid, prevPoint.y); Vector3 newP2 = new Vector3(xMid, point.y); botPoints.Add(newP1); botPoints.Add(newP2); } botPoints.Add(point); } } return botPoints; } public List GetTopPoints() { List botPoints = new List(); for (int i = 0; i < recordedSteps.Count; i++) { Vector3 point = recordedSteps[i].point; if (i % 2 != 0) { if (i > 2) { Vector3 prevPoint = recordedSteps[i - 2].point; float xMid = prevPoint.x + ((point.x - prevPoint.x) / (2f * xFactorTop)); float yMid = prevPoint.y + ((point.y - prevPoint.y) / 2f); xMid = recordedSteps[i - 1].point.x; ; Vector3 newP1 = new Vector3(xMid, prevPoint.y); Vector3 newP2 = new Vector3(xMid, point.y); botPoints.Add(newP1); botPoints.Add(newP2); } botPoints.Add(point); } } return botPoints; } void AddPointToShape(SpriteShapeController shapeController, Vector3 point) { Spline spline = shapeController.spline; spline.InsertPointAt(spline.GetPointCount(), point); spline.SetTangentMode(spline.GetPointCount() - 1, ShapeTangentMode.Continuous); } public float xFactorBot,xFactorTop = 1f; private void OnDrawGizmos() { if (playReplay && !Application.isPlaying) { recordedSteps = JsonConvert.DeserializeObject>(recordedStepsJson); List botPoints = GetBottomPoints(); Gizmos.color = Color.yellow; //Bottom lines for(int i=1; i < botPoints.Count; i++) { Gizmos.DrawLine(botPoints[i - 1], botPoints[i]); } List topPoints = GetTopPoints(); //top lines for (int i = 1; i < topPoints.Count; i++) { Gizmos.DrawLine(topPoints[i - 1], topPoints[i]); } Gizmos.color = Color.red; //moving path for(int i =1; i < recordedSteps.Count; i++) { Gizmos.DrawLine(recordedSteps[i-1].point, recordedSteps[i].point); } } } void PreWork() { float loudness_delta = splitter.loudness - prev_loud; bool notTooShort = (Time.time - LastTime) > yieldTime; bool oldLoudLogic = splitter.loudness > loudnessThreshold && prev_loud < loudnessThreshold; if ((loudness_delta > loudnessThreshold && notTooShort) || Input.GetKeyDown(KeyCode.Space)) { Debug.Log(LastTime - Time.time); //Instantiate(prefab, transform.position, Quaternion.identity); int random = Random.Range(0, 3); bool flippedX = LastPos.x - transform.position.x > LastPos.y - transform.position.y; if (manualFlip) { flippedX = manualFlipX; } if (flippedX) { movingSpeed.x = -movingSpeed.x; Instantiate(prefab, transform.position + new Vector3(-1 / 5f, 0), Quaternion.Euler(new Vector3(0, 0, 0))).transform.GetComponentInChildren().text = loudness_delta.ToString("n3"); movingSpeedMultiplier += (loudness_delta * loudBoost) / loudnessThreshold; } else { movingSpeed.y = -movingSpeed.y; Instantiate(prefab, transform.position + new Vector3(0, -1 / 5f), Quaternion.Euler(new Vector3(0, 0, 90))).transform.GetComponentInChildren().text = loudness_delta.ToString("n3"); ; movingSpeedMultiplier += (loudness_delta * loudBoost) / loudnessThreshold; } LastTime = Time.time; currentSteps.Add(new RecorderStep(LastTime, movingSpeed, movingSpeedMultiplier, transform.position)); currentStepsJson = JsonConvert.SerializeObject(currentSteps); LastPos = transform.position; } prev_loud = splitter.loudness; } void PostWork() { if(recordedSteps.Count <= 0) { return; } if (Time.time >= recordedSteps[0].time) { //Time to act movingSpeed = recordedSteps[0].movingSpeed; movingSpeedMultiplier = recordedSteps[0].movingSpeedMultiplier; musicSourceToSync.time = (float)recordedSteps[0].time; recordedSteps.RemoveAt(0); HitFX(); } } public GameObject particleFxPrefab; public AudioSource musicSourceToSync; public float fovChangeOnHit = 0.9f; void HitFX() { StartCoroutine(CoroutineScaleupGlow()); Vector3 movingSpeedY = movingSpeed; movingSpeedY.x = 0; Instantiate(particleFxPrefab, transform.position - (movingSpeedY/5f), Quaternion.LookRotation(-movingSpeedY)); Camera.main.orthographicSize = defFOV * fovChangeOnHit; } IEnumerator CoroutineScaleupGlow() { while(insideFill.transform.localScale.x < 0.6f) { insideFill.transform.localScale = Vector3.one * (insideFill.transform.localScale.x + (Time.deltaTime * glowReductionSpeed * 5)); yield return null; } } } [System.Serializable] public class RecorderStep { public double time; public Vector3 movingSpeed; public float movingSpeedMultiplier; public Vector3 point; public RecorderStep(double t, Vector3 s, float m, Vector3 p) { time = t; movingSpeed = s; movingSpeedMultiplier = m; point = p; } }