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

399 lines
13 KiB
C#

using Newtonsoft.Json;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using Unity.VisualScripting;
using UnityEditor.SearchService;
using UnityEngine;
using UnityEngine.Rendering.Universal;
using UnityEngine.SceneManagement;
using UnityEngine.U2D;
public class Mover : MonoBehaviour
{
public Vector3 movingSpeed;
public float loudBoost = 1f;
public float momentumBoost = 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 GameObject txtDebug;
public bool isDebug = false;
public Light2D glowLight;
public float glowReductionSpeed = 2f;
public float fovZoomingSpeed = 0.5f;
[SerializeField]
public List<RecorderStep> currentSteps = new List<RecorderStep>();
[SerializeField]
List<RecorderStep> recordedSteps = new List<RecorderStep>();
public string currentStepsJson = "";
public string recordedStepsJson;
public bool playReplay = false;
float defFOV;
public AudioSource source;
List<float> recordedTimes = null;
List<float> pastTimes = null;
private void Awake()
{
defFOV = Camera.main.orthographicSize;
if (!playReplay) { themeMan.Randomize(); }
if(PrepConnector.saveLoadData != null){
recordedTimes = PrepConnector.saveLoadData.hits;
}
}
private void Start()
{
// Application.targetFrameRate = 164;
if(recordedStepsJson.Length > 1 && playReplay)
{
recordedSteps = JsonConvert.DeserializeObject<List<RecorderStep>>(recordedStepsJson);
GenerateRecordedPlatform();
}
}
Vector3 LastPos;
float movingSpeedMultiplier = 1;
public bool manualFlip = true;
public bool manualFlipX = false;
void Update()
{
if (glowLight.intensity > 0)
{
glowLight.intensity=(glowLight.intensity- (Time.deltaTime * glowReductionSpeed));
}
trailParticle.emissionRate = 300 * (glowLight.intensity);
if (playReplay) { Camera.main.orthographicSize -= Time.deltaTime * fovZoomingSpeed; }
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);
}
cube.rotation = Quaternion.Lerp(cube.rotation, targetRotation, cubeRotateSpeed * Time.deltaTime);
}
void FixedUpdate()
{
movingSpeedMultiplier = Mathf.Lerp(movingSpeedMultiplier, 1, Time.deltaTime * 2);
float boostedX = movingSpeed.x * Mathf.Clamp((movingSpeedMultiplier * 1),0,1.25f);
float boostedY = movingSpeed.y * (movingSpeedMultiplier * 1f);
//transform.Translate(new Vector3(boostedX, boostedY)*Time.deltaTime);
transform.Translate(movingSpeed * Time.deltaTime * movingSpeedMultiplier);
if(recordedStepsJson.Length > 1 && playReplay)
{
PostWork();
}
else
{
PreWork();
}
}
void GenerateRecordedPlatform()
{
List<Vector3> botPoints = GetBottomPoints();
botLineRenderer.positionCount = botPoints.Count;
botLineRenderer.SetPositions(botPoints.ToArray());
List<Vector3> 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<SpriteShapeRenderer>().enabled = true;
}
List<Vector3> bottomPoints = new List<Vector3>();
List<Vector3> topPoints = new List<Vector3>();
public List<Vector3> GetBottomPoints()
{
List<Vector3> botPoints = new List<Vector3>();
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<Vector3> GetTopPoints()
{
List<Vector3> botPoints = new List<Vector3>();
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<List<RecorderStep>>(recordedStepsJson);
List<Vector3> botPoints = GetBottomPoints();
Gizmos.color = Color.yellow;
//Bottom lines
for(int i=1; i < botPoints.Count; i++)
{
Gizmos.DrawLine(botPoints[i - 1], botPoints[i]);
}
List<Vector3> 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);
}
}
}
float tensionBuildup = 0f;
float endTimer = 0;
void PreWork()
{
float loudness_delta = splitter.loudness - prev_loud;
bool notTooShort = (Time.time - LastTime) > yieldTime;
bool oldLoudLogic = splitter.loudness > loudnessThreshold && prev_loud < loudnessThreshold;
bool newLoudLogic = (loudness_delta > loudnessThreshold && notTooShort);
bool prepLogic = false;
if(pastTimes.Count <= 0){
if(endTimer < 10f){
endTimer+=Time.deltaTime;
}else{
SceneManager.LoadScene("prep");
endTimer=0;
}
}else{
if(pastTimes[0] < source.time){
prepLogic = true;
pastTimes.RemoveAt(0);
}
}
bool newDynamicLogic = recordedTimes == null ? oldLoudLogic : prepLogic ;
tensionBuildup += Time.deltaTime;
if (newDynamicLogic || 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<TextMesh>().text = loudness_delta.ToString("n3");
}
else
{
movingSpeed.y = -movingSpeed.y;
Instantiate(prefab, transform.position + new Vector3(0, -1 / 5f), Quaternion.Euler(new Vector3(0, 0, 90))).transform.GetComponentInChildren<TextMesh>().text = loudness_delta.ToString("n3"); ;
}
movingSpeedMultiplier += (loudness_delta * loudBoost) / loudnessThreshold;
movingSpeedMultiplier += tensionBuildup * momentumBoost;
LastTime = Time.time;
currentSteps.Add(new RecorderStep(LastTime, movingSpeed, movingSpeedMultiplier, transform.position));
currentStepsJson = JsonConvert.SerializeObject(currentSteps);
LastPos = transform.position;
tensionBuildup = 0;
}
prev_loud = splitter.loudness;
}
int index = 0;
void PostWork()
{
if(recordedSteps.Count <= 0) { return; }
tensionBuildup += Time.deltaTime;
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();
tensionBuildup = 0;
index++;
if (isDebug) { Instantiate(txtDebug, transform.position, Quaternion.identity).GetComponent<TextMeshPro>().text = index.ToString(); }
}
}
public GameObject particleFxPrefab;
public AudioSource musicSourceToSync;
public float fovChangeOnHit = 0.1f;
public ParticleSystem trailParticle;
public GameObject shockwavePrefab;
public ThemeManager themeMan;
public Transform cube;
public float cubeRotateSpeed = 15f;
Quaternion targetRotation = Quaternion.identity;
void HitFX()
{
StartCoroutine(CoroutineScaleupGlow());
Vector3 movingSpeedY = movingSpeed;
movingSpeedY.x = 0;
Vector3 hitpoint = transform.position - (movingSpeedY / 5f);
Instantiate(particleFxPrefab, hitpoint, Quaternion.LookRotation(-movingSpeedY)).GetComponent<ParticleSystem>().startColor =themeMan.mainColor;
Camera.main.orthographicSize = Mathf.Clamp(Camera.main.orthographicSize + (fovZoomingSpeed), 0,defFOV);
shockwavePrefab.GetComponent<ShockwaveShaderHelper>().Reset();
Instantiate(glowLight,hitpoint, Quaternion.identity).AddComponent<DyingLight>();
themeMan.Randomize(movingSpeedMultiplier /3f);
targetRotation *= Quaternion.Euler(0, 0, -90);
// themeMan.SetColor(new Color(Random.Range(0f,1f), Random.Range(0f,1f),Random.Range(0f,1f)));
/*themeMan.r = Random.Range(0f, 1f);
themeMan.g = Random.Range(0f, 1f);
themeMan.b = Random.Range(0f, 1f);
themeMan.Refresh();*/
// Instantiate(shockwavePrefab, shockwavePrefab.transform.parent).GetComponent<ShockwaveShaderHelper>().started = true;
}
IEnumerator CoroutineScaleupGlow()
{
glowLight.intensity += 0.4f;
while(glowLight.intensity < 0.65f)
{
glowLight.intensity = (glowLight.intensity + (Time.deltaTime * glowReductionSpeed * 8));
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;
}
}