SpawnTriangles.cs
unknown
csharp
3 years ago
6.3 kB
6
Indexable
using System.Collections; using System.Collections.Generic; using UnityEngine; using SFB; using System.IO; using UnityEngine.SceneManagement; public class SpawnTriangles : MonoBehaviour { public GameObject triangle; private AudioSource audioSource; private string songLocation; private string chartLocation; public GameObject pauseUi; public GameObject clearTriangle; public float[] timestamps; public int[] timestampDirections; public bool paused = false; public List<GameObject> orderTop = new List<GameObject>(); public List<GameObject> orderRight = new List<GameObject>(); public List<GameObject> orderDown = new List<GameObject>(); public List<GameObject> orderLeft = new List<GameObject>(); void Start() { audioSource = this.gameObject.AddComponent<AudioSource>(); StartCoroutine(ReadLevelFile()); } void Update() { if(Input.GetKeyDown(KeyCode.Q)) { SceneManager.LoadScene("Charter"); } if(Input.GetKeyDown(KeyCode.Escape)) { SceneManager.LoadScene("MainMenu"); } if(Input.GetKeyDown(KeyCode.R)) { Restart(); } } IEnumerator ReadLevelFile() { if(PlayerPrefs.GetInt("restart") != 1) { var extensions = new [] { new ExtensionFilter("Sound Files", "mp3", "wav" ) }; var songPath = StandaloneFileBrowser.OpenFilePanel("Open File", "", extensions, true); songLocation = songPath[0]; var extensions1 = new [] { new ExtensionFilter("Text Files", "txt") }; var paths = StandaloneFileBrowser.OpenFilePanel("Open File", "", extensions1, true); chartLocation = paths[0]; } else { songLocation = PlayerPrefs.GetString("songLocation"); chartLocation = PlayerPrefs.GetString("chartLocation"); PlayerPrefs.SetInt("restart", 0); } string[] lines = System.IO.File.ReadAllLines(chartLocation); List<float> tempTimestamps = new List<float>(); List<int> tempTimestampDirections = new List<int>(); foreach(string line in lines) { string[] lineSplit = line.Split(' '); tempTimestamps.Add(float.Parse(lineSplit[0])); tempTimestampDirections.Add(int.Parse(lineSplit[1])); } timestamps = tempTimestamps.ToArray(); timestampDirections = tempTimestampDirections.ToArray(); StartCoroutine(LoadSongCoroutine(songLocation)); StartCoroutine(LoadChartMap()); yield return null; } IEnumerator LoadSongCoroutine(string soundPath) { string url = string.Format("file://{0}", soundPath); WWW www = new WWW(url); yield return www; audioSource.clip = www.GetAudioClip(false, false); audioSource.Play(); } void RandomSpawn() { Vector2 pos = new Vector2(); pos = new Vector2(Random.Range(9.19f, -9.38f), 5.3f); //float side = Mathf.Floor(Random.Range(1, 5)); //if(side == 1) pos = new Vector2(Random.Range(9.19f, -9.38f), 5.3f); //else if(side == 2) pos = new Vector2(-9.8f, Random.Range(5.22f, -6.13f)); //else if(side == 3) pos = new Vector2(Random.Range(-9.8f, 9.44f), -6.13f); //else if(side == 4) pos = new Vector2(9.44f, Random.Range(5.55f, -6.13f)); Instantiate(triangle, pos, triangle.transform.rotation); } void SpawnAtPos(Vector3 pos, int key, int index) { GameObject triangleGO = Instantiate(triangle, pos, triangle.transform.rotation); triangleGO.name = key.ToString(); if(timestampDirections[index].ToString() == "1") { orderTop.Add(triangleGO); } if(timestampDirections[index].ToString() == "2") { orderRight.Add(triangleGO); } if(timestampDirections[index].ToString() == "3") { orderDown.Add(triangleGO); } if(timestampDirections[index].ToString() == "4") { orderLeft.Add(triangleGO); } } Vector2 RandomSpawnVector() { Vector2 pos = new Vector2(); pos = new Vector2(Random.Range(9.19f, -9.38f), 5.3f); //float side = Mathf.Floor(Random.Range(1, 5)); //if(side == 1) pos = new Vector2(Random.Range(9.19f, -9.38f), 5.3f); //else if(side == 2) pos = new Vector2(-9.8f, Random.Range(5.22f, -6.13f)); //else if(side == 3) pos = new Vector2(Random.Range(-9.8f, 9.44f), -6.13f); //else if(side == 4) pos = new Vector2(9.44f, Random.Range(5.55f, -6.13f)); return pos; } GameObject SpawnClearTriangleAtPos(Vector2 pos) { return Instantiate(clearTriangle, pos, clearTriangle.transform.rotation); } IEnumerator LoadChartMap() { int index = 0; foreach(float time in timestamps) { Vector3 pos = GameObject.Find(timestampDirections[index].ToString()).transform.position; Vector3 closestTimestampPos = GameObject.Find(timestampDirections[index].ToString() + "D").transform.position; float Distance = Vector3.Distance(closestTimestampPos, pos); float TimeBetweenObjects = Distance / 6; yield return new WaitUntil(() => audioSource.time >= time - TimeBetweenObjects); SpawnAtPos(pos, timestampDirections[index], index); index++; } yield return null; } public void Restart() { PlayerPrefs.SetInt("restart", 1); PlayerPrefs.SetString("songLocation", songLocation); PlayerPrefs.SetString("chartLocation", chartLocation); SceneManager.LoadScene("SampleScene"); } public void Menu() { SceneManager.LoadScene("MainMenu"); } public void Resume() { Time.timeScale = 1; audioSource.Play(); pauseUi.SetActive(false); GameObject.Find("protect").GetComponent<SpriteRenderer>().color = Color.red; paused = false; } }
Editor is loading...