From 2809b06a715cd59b6a2b003ad6f3196b7b395113 Mon Sep 17 00:00:00 2001 From: Karina Date: Sun, 10 Jun 2018 17:06:17 +0300 Subject: [PATCH] added comments to improve code readability --- .../Assets/Scripts/Apply_Shader.cs | 10 ++-- .../Assets/Scripts/Arrow_Movement.cs | 12 ++--- .../ArduinoDancer/Assets/Scripts/BG_Audio.cs | 6 +-- .../Assets/Scripts/BG_Colour_Changer.cs | 6 +-- .../Assets/Scripts/Difficulty_Menu_Handler.cs | 20 ++++---- .../ArduinoDancer/Assets/Scripts/Game_Data.cs | 1 - .../Assets/Scripts/Heartbeat_Controller.cs | 17 ++++--- .../Assets/Scripts/IngameMenu.cs | 4 +- .../Assets/Scripts/Menu_Handler.cs | 5 +- .../ArduinoDancer/Assets/Scripts/PauseMenu.cs | 8 +-- .../Assets/Scripts/Quit_Tutorial.cs | 4 +- .../Assets/Scripts/Resoultion.cs | 11 ++-- .../Assets/Scripts/Score_Handler.cs | 2 - .../Assets/Scripts/Song_Parser.cs | 35 ++++++------- .../Assets/Scripts/Song_Selector.cs | 51 +++++++++++++------ .../Assets/Scripts/Step_Generator.cs | 10 ++-- .../Assets/Scripts/StopAllAudio.cs | 8 +-- .../Assets/Scripts/testingArduino.cs | 3 +- 18 files changed, 118 insertions(+), 95 deletions(-) diff --git a/src/DesktopApp/ArduinoDancer/Assets/Scripts/Apply_Shader.cs b/src/DesktopApp/ArduinoDancer/Assets/Scripts/Apply_Shader.cs index ac0ef7e..75ac448 100755 --- a/src/DesktopApp/ArduinoDancer/Assets/Scripts/Apply_Shader.cs +++ b/src/DesktopApp/ArduinoDancer/Assets/Scripts/Apply_Shader.cs @@ -1,4 +1,4 @@ -using UnityEngine; +using UnityEngine; using System.Collections; public class Apply_Shader : MonoBehaviour { @@ -9,6 +9,7 @@ public class Apply_Shader : MonoBehaviour { [Range(0, 1)] public float verts_force = 0.0f; + [Range(0, 1)] public float verts_force_2 = 0.0f; @@ -16,7 +17,8 @@ protected Material material { get { - if (_material == null) _material.hideFlags = HideFlags.HideAndDontSave; + if (_material == null) + _material.hideFlags = HideFlags.HideAndDontSave; // Creates a material that is explicitly created & destroyed by the component. return _material; } } @@ -24,10 +26,12 @@ protected Material material private void OnRenderImage(RenderTexture source, RenderTexture destination) { if (shader == null) return; + Material mat = material; mat.SetFloat("_VertsColor", 1 - verts_force); mat.SetFloat("_VertsColor2", 1 - verts_force_2); - Graphics.Blit(source, destination, mat); + Graphics.Blit(source, destination, mat); //Source texture,The destination RenderTexture,Material to use. + } void OnDisable() diff --git a/src/DesktopApp/ArduinoDancer/Assets/Scripts/Arrow_Movement.cs b/src/DesktopApp/ArduinoDancer/Assets/Scripts/Arrow_Movement.cs index 3abe087..569445e 100755 --- a/src/DesktopApp/ArduinoDancer/Assets/Scripts/Arrow_Movement.cs +++ b/src/DesktopApp/ArduinoDancer/Assets/Scripts/Arrow_Movement.cs @@ -17,7 +17,7 @@ public class Arrow_Movement : MonoBehaviour { public enum direction { left, down, up, right }; private const float strumOffset = 0.075f; private const float despawnTime = 1.5f; - + void Start () { gen = GameObject.FindGameObjectWithTag("GameManager").GetComponent(); scoreHandler = GameObject.FindGameObjectWithTag("GameManager").GetComponent(); @@ -44,23 +44,23 @@ void Update() { { // move arrow arrowSpeed = gen.arrowSpeed; - Vector3 tempPos = transform.position; + Vector3 tempPos = transform.position; tempPos.y -= arrowSpeed; transform.position = tempPos; - // support both WASD and arrows from keyboard as input + // support both WASD and arrows from keyboard as input bool isPressedDownKeyboardLeft = Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.A); bool isPressedDownKeyboardDown = Input.GetKeyDown(KeyCode.DownArrow) || Input.GetKeyDown(KeyCode.S); bool isPressedDownKeyboardUp = Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.W); bool isPressedDownKeyboardRight = Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.D); - - if (Input.GetKeyDown(KeyCode.Q)) SceneManager.LoadScene("ingame"); + + if (Input.GetKeyDown(KeyCode.Q)) SceneManager.LoadScene("ingame"); if (isPressedDownKeyboardLeft && dir == direction.left) CheckLocation(); if (isPressedDownKeyboardDown && dir == direction.down) CheckLocation(); if (isPressedDownKeyboardUp && dir == direction.up) CheckLocation(); if (isPressedDownKeyboardRight && dir == direction.right) CheckLocation(); - //Missed + //Missed the arrows if (transform.position.y < arrowBack.transform.position.y - strumOffset) { GetComponent().material.SetColor("_Color", new Color(0.5f, 0.0f, 0.0f)); //decolorize arrow diff --git a/src/DesktopApp/ArduinoDancer/Assets/Scripts/BG_Audio.cs b/src/DesktopApp/ArduinoDancer/Assets/Scripts/BG_Audio.cs index 01fb002..49012af 100755 --- a/src/DesktopApp/ArduinoDancer/Assets/Scripts/BG_Audio.cs +++ b/src/DesktopApp/ArduinoDancer/Assets/Scripts/BG_Audio.cs @@ -11,7 +11,7 @@ public class BG_Audio : MonoBehaviour { /// public static BG_Audio Instance { - get { return instance; } + get { return instance; } // returns the property value or the indexer element } /// @@ -24,8 +24,8 @@ void Awake() Destroy(this.gameObject); return; } - else instance = this; + else instance = this; - DontDestroyOnLoad(this.gameObject); + DontDestroyOnLoad(this.gameObject); // dont destroy automatically when loading a new scene } } diff --git a/src/DesktopApp/ArduinoDancer/Assets/Scripts/BG_Colour_Changer.cs b/src/DesktopApp/ArduinoDancer/Assets/Scripts/BG_Colour_Changer.cs index f082e3b..661cd4f 100755 --- a/src/DesktopApp/ArduinoDancer/Assets/Scripts/BG_Colour_Changer.cs +++ b/src/DesktopApp/ArduinoDancer/Assets/Scripts/BG_Colour_Changer.cs @@ -11,10 +11,10 @@ public class BG_Colour_Changer : MonoBehaviour { void Start () { camera = GetComponent(); } - + void Update () { - int hue = Mathf.PingPong(Time.time * speed, 1); //a value between the time in seconds since the start of the frame * 0.5 < 1 - HSBColor randCol = new HSBColor(hue, 1, 1); + int hue = Mathf.PingPong(Time.time * speed, 1); // time in seconds since the start of the frame * 0.5< value < 1 + HSBColor randCol = new HSBColor(hue, 1, 1); randCol.s = randCol.s / 2; //desaturate camera.backgroundColor = randCol.ToColor(); // change background color } diff --git a/src/DesktopApp/ArduinoDancer/Assets/Scripts/Difficulty_Menu_Handler.cs b/src/DesktopApp/ArduinoDancer/Assets/Scripts/Difficulty_Menu_Handler.cs index bfbef27..185bba8 100755 --- a/src/DesktopApp/ArduinoDancer/Assets/Scripts/Difficulty_Menu_Handler.cs +++ b/src/DesktopApp/ArduinoDancer/Assets/Scripts/Difficulty_Menu_Handler.cs @@ -3,37 +3,37 @@ public class Difficulty_Menu_Handler : MonoBehaviour { - string sceneToLoad = "Song Selection"; + string selectSongScene = "Song Selection"; /// /// Sets difficulty to beginner /// - public void SetBeginner() { + public void SetBeginner() { Game_Data.difficulty = Song_Parser.difficulties.beginner; - UnityEngine.SceneManagement.SceneManager.LoadScene(sceneToLoad); + UnityEngine.SceneManagement.SceneManager.LoadScene(selectSongScene); } /// /// Sets difficulty to easy /// - public void SetEasy() { + public void SetEasy() { Game_Data.difficulty = Song_Parser.difficulties.easy; - UnityEngine.SceneManagement.SceneManager.LoadScene(sceneToLoad); + UnityEngine.SceneManagement.SceneManager.LoadScene(selectSongScene); } /// /// Sets difficulty to medium /// - public void SetMedium() { + public void SetMedium() { Game_Data.difficulty = Song_Parser.difficulties.medium; - UnityEngine.SceneManagement.SceneManager.LoadScene(sceneToLoad); + UnityEngine.SceneManagement.SceneManager.LoadScene(selectSongScene); } /// /// Sets difficulty to hard /// - public void SetHard() { + public void SetHard() { Game_Data.difficulty = Song_Parser.difficulties.hard; - UnityEngine.SceneManagement.SceneManager.LoadScene(sceneToLoad); + UnityEngine.SceneManagement.SceneManager.LoadScene(selectSongScene); } /// @@ -41,6 +41,6 @@ public void SetHard() { /// public void SetChallenge() { Game_Data.difficulty = Song_Parser.difficulties.challenge; - UnityEngine.SceneManagement.SceneManager.LoadScene(sceneToLoad); + UnityEngine.SceneManagement.SceneManager.LoadScene(selectSongScene); } } diff --git a/src/DesktopApp/ArduinoDancer/Assets/Scripts/Game_Data.cs b/src/DesktopApp/ArduinoDancer/Assets/Scripts/Game_Data.cs index efaf159..1aa779c 100755 --- a/src/DesktopApp/ArduinoDancer/Assets/Scripts/Game_Data.cs +++ b/src/DesktopApp/ArduinoDancer/Assets/Scripts/Game_Data.cs @@ -3,7 +3,6 @@ using System.Linq; using System.Text; - public static class Game_Data { public static string songDirectory = ""; diff --git a/src/DesktopApp/ArduinoDancer/Assets/Scripts/Heartbeat_Controller.cs b/src/DesktopApp/ArduinoDancer/Assets/Scripts/Heartbeat_Controller.cs index 60a7c86..24cbbbc 100755 --- a/src/DesktopApp/ArduinoDancer/Assets/Scripts/Heartbeat_Controller.cs +++ b/src/DesktopApp/ArduinoDancer/Assets/Scripts/Heartbeat_Controller.cs @@ -49,9 +49,9 @@ IEnumerator LoadTrack(string path, Song_Parser.Metadata meta) string url = string.Format("file://{0}", path); WWW www = new WWW(url); - while (!www.isDone) yield return null; + while (!www.isDone) yield return null; // download isnt finished - AudioClip clip = www.GetAudioClip(false, false); + AudioClip clip = www.GetAudioClip(false, false); // doesnt matter if it's 2d or 3d, no need to be downloaded completely audioSource.clip = clip; songLoaded = true; @@ -68,8 +68,7 @@ void HeartBeat() { if (songLoaded && !editingBPM) { - //Calc how long a beat is in seconds - float secondsPerBeat = frames / currentBPM; + float secondsPerBeat = frames / currentBPM; //Calc how long a beat is in seconds //If the time has passed for one beat, beat the heart and reset animCounter += Time.deltaTime; @@ -89,9 +88,11 @@ void ControllerHandler() if (anim.GetBool("isBeat")) anim.SetBool("isBeat", false); if (Input.GetKeyDown(KeyCode.N)){ + // restore pitch to the normal audioSource.pitch = 1; editingBPM = false; }else if(Input.GetKeyDown(KeyCode.Space)) { + // changing BPM editingBPM = true; anim.SetBool("isBeat", true); @@ -112,11 +113,11 @@ void ControllerHandler() /// void SongBPMChange() { - + float newPitch = currentBPM / originalBPM; - if (Mathf.Abs(audioSource.pitch - newPitch) > lerpLimit) + if (Mathf.Abs(audioSource.pitch - newPitch) > lerpLimit) // smooth change audioSource.pitch = Mathf.Lerp(audioSource.pitch, currentBPM / originalBPM, Time.deltaTime); - else + else // just change audioSource.pitch = currentBPM / originalBPM; } -} \ No newline at end of file +} diff --git a/src/DesktopApp/ArduinoDancer/Assets/Scripts/IngameMenu.cs b/src/DesktopApp/ArduinoDancer/Assets/Scripts/IngameMenu.cs index 961122e..2257dab 100755 --- a/src/DesktopApp/ArduinoDancer/Assets/Scripts/IngameMenu.cs +++ b/src/DesktopApp/ArduinoDancer/Assets/Scripts/IngameMenu.cs @@ -3,13 +3,13 @@ using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement; + public class IngameMenu : MonoBehaviour { - + string mainMenuScene = "Main Menu"; string controlsScene = "Controls"; string scoreScene = "Score"; - /// /// loads the main meni /// diff --git a/src/DesktopApp/ArduinoDancer/Assets/Scripts/Menu_Handler.cs b/src/DesktopApp/ArduinoDancer/Assets/Scripts/Menu_Handler.cs index 0770101..1c5e7c0 100755 --- a/src/DesktopApp/ArduinoDancer/Assets/Scripts/Menu_Handler.cs +++ b/src/DesktopApp/ArduinoDancer/Assets/Scripts/Menu_Handler.cs @@ -22,7 +22,7 @@ public void StartGameClick() StartCoroutine(DespawnWarning()); } else SceneManager.LoadScene(1); - + } /// @@ -67,12 +67,11 @@ public void FindSongsClick() System.Windows.Forms.FolderBrowserDialog fbd = new System.Windows.Forms.FolderBrowserDialog(); System.Windows.Forms.DialogResult result = fbd.ShowDialog(); // shows dialog to select songs - if (result == System.Windows.Forms.DialogResult.OK) + if (result == System.Windows.Forms.DialogResult.OK) { Game_Data.songDirectory = fbd.SelectedPath; // make the selected path the song directory if(Song_Parser.IsNullOrWhiteSpace(Game_Data.songDirectory)) Game_Data.validSongDir = false; else Game_Data.validSongDir = true; - } } } diff --git a/src/DesktopApp/ArduinoDancer/Assets/Scripts/PauseMenu.cs b/src/DesktopApp/ArduinoDancer/Assets/Scripts/PauseMenu.cs index 4b0b5bc..8b09e66 100755 --- a/src/DesktopApp/ArduinoDancer/Assets/Scripts/PauseMenu.cs +++ b/src/DesktopApp/ArduinoDancer/Assets/Scripts/PauseMenu.cs @@ -3,7 +3,7 @@ using UnityEngine; public class PauseMenu : MonoBehaviour { - + public static bool isPaused = false; public GameObject pauseMenuUI; @@ -13,13 +13,13 @@ void Start() Time.timeScale = 1f; isPaused = false; } - - void Update () + + void Update () { if (Input.GetKeyDown(KeyCode.P)) // pause is triggered { if(isPaused) Resume(); - else Pause(); + else Pause(); } } diff --git a/src/DesktopApp/ArduinoDancer/Assets/Scripts/Quit_Tutorial.cs b/src/DesktopApp/ArduinoDancer/Assets/Scripts/Quit_Tutorial.cs index 332c811..dc5d239 100755 --- a/src/DesktopApp/ArduinoDancer/Assets/Scripts/Quit_Tutorial.cs +++ b/src/DesktopApp/ArduinoDancer/Assets/Scripts/Quit_Tutorial.cs @@ -2,9 +2,11 @@ using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; + public class Quit_Tutorial : MonoBehaviour { void Update () { - if (Input.GetKeyDown(KeyCode.Q)) SceneManager.LoadScene("Main Menu"); + if (Input.GetKeyDown(KeyCode.Q)) + SceneManager.LoadScene("Main Menu"); } } diff --git a/src/DesktopApp/ArduinoDancer/Assets/Scripts/Resoultion.cs b/src/DesktopApp/ArduinoDancer/Assets/Scripts/Resoultion.cs index e1fda28..acf8a3d 100755 --- a/src/DesktopApp/ArduinoDancer/Assets/Scripts/Resoultion.cs +++ b/src/DesktopApp/ArduinoDancer/Assets/Scripts/Resoultion.cs @@ -3,9 +3,8 @@ public class Resoultion : MonoBehaviour { - - void Update() - { - Screen.SetResolution(1600, 800, false); - } -} \ No newline at end of file + void Update() + { + Screen.SetResolution(1600, 800, false); + } +} diff --git a/src/DesktopApp/ArduinoDancer/Assets/Scripts/Score_Handler.cs b/src/DesktopApp/ArduinoDancer/Assets/Scripts/Score_Handler.cs index 9d20f1e..789df49 100755 --- a/src/DesktopApp/ArduinoDancer/Assets/Scripts/Score_Handler.cs +++ b/src/DesktopApp/ArduinoDancer/Assets/Scripts/Score_Handler.cs @@ -18,14 +18,12 @@ public class Score_Handler : MonoBehaviour private const float scoreVal = 100.0f; private const float strumOffset = 0.1f; - // Use this for initialization void Start() { heart = GameObject.FindGameObjectWithTag("Player"); heartAudio = heart.GetComponent(); } - // Update is called once per frame void Update() { multiplier = heartAudio.pitch; diff --git a/src/DesktopApp/ArduinoDancer/Assets/Scripts/Song_Parser.cs b/src/DesktopApp/ArduinoDancer/Assets/Scripts/Song_Parser.cs index e64e1c7..850f64d 100755 --- a/src/DesktopApp/ArduinoDancer/Assets/Scripts/Song_Parser.cs +++ b/src/DesktopApp/ArduinoDancer/Assets/Scripts/Song_Parser.cs @@ -52,7 +52,7 @@ public struct Notes public enum difficulties { beginner, easy, medium, hard, challenge }; /// - /// parses song file + /// parses song file /// /// file location /// songData @@ -69,9 +69,10 @@ public Metadata Parse(string newFilePath) bool inNotes = false; - Metadata songData = new Metadata(); - //Initialise Metadata + Metadata songData = new Metadata(); //Initialise Metadata songData.valid = true; //If it encounters any major errors during parsing, this is set to false and the song cannot be selected + + // default difficulties values songData.beginnerExists = false; songData.easyExists = false; songData.mediumExists = false; @@ -83,7 +84,7 @@ public Metadata Parse(string newFilePath) string fileDir = Path.GetDirectoryName(filePath); if (!fileDir.EndsWith("\\") && !fileDir.EndsWith("/")) fileDir += "\\"; - + for (int i = 0; i < fileData.Count; i++) { @@ -92,8 +93,7 @@ public Metadata Parse(string newFilePath) if (line.StartsWith("//")) continue; else if (line.StartsWith("#")) { - string key = line.Substring(0, line.IndexOf(':')).Trim('#').Trim(':').ToUpper(); - + string key = line.Substring(0, line.IndexOf(':')).Trim('#').Trim(':').ToUpper(); // if line = "#TITLE:Gangnam Style;" -> TITLE switch (key) { case "TITLE": @@ -159,7 +159,7 @@ public Metadata Parse(string newFilePath) if (inNotes) { - //Skip dance-double for now + //Skip lines about dance-double if (line.ToLower().Contains("dance-double")) { for(int j = i; j < fileData.Count; j++) @@ -168,7 +168,7 @@ public Metadata Parse(string newFilePath) i = j - 1; break; } - + } //Check if it's a difficulty @@ -190,7 +190,6 @@ public Metadata Parse(string newFilePath) break; } else noteChart.Add(noteLine); - } switch (difficulty.ToLower().Trim()) @@ -249,22 +248,24 @@ private NoteData ParseNotes(List notes) else if (line.EndsWith(":")) continue; else if (line.Length >= 4) { + // init default note values Notes note = new Notes(); note.left = false; note.down = false; note.up = false; note.right = false; - + + // add values if (line[0] != '0') note.left = true; if (line[1] != '0') note.down = true; - if (line[2] != '0') note.up = true; + if (line[2] != '0') note.up = true; if (line[3] != '0') note.right = true; - - bar.Add(note); + + bar.Add(note); // add this note to the list } } - return noteData; + return noteData; // returned parsed notes } /// @@ -274,9 +275,9 @@ private NoteData ParseNotes(List notes) /// true or false public static bool IsNullOrWhiteSpace(string value) { - if (value != null) - for (int i = 0; i < value.Length; i++) - if (!char.IsWhiteSpace(value[i])) return false; + if (value != null) + for (int i = 0; i < value.Length; i++) + if (!char.IsWhiteSpace(value[i])) return false; return true; } } diff --git a/src/DesktopApp/ArduinoDancer/Assets/Scripts/Song_Selector.cs b/src/DesktopApp/ArduinoDancer/Assets/Scripts/Song_Selector.cs index a6e559a..b3cd100 100755 --- a/src/DesktopApp/ArduinoDancer/Assets/Scripts/Song_Selector.cs +++ b/src/DesktopApp/ArduinoDancer/Assets/Scripts/Song_Selector.cs @@ -23,7 +23,7 @@ void Start () { audioSource = GetComponent(); Parse(); } - + void Update () { if (audioSource.time >= (audioStartTime + audioLength)) { @@ -31,9 +31,9 @@ void Update () { audioSource.time = audioStartTime; } - if (audioSource.volume < 0.95f) audioSource.volume = Mathf.Lerp(audioSource.volume, 1.0f, Time.deltaTime); - else audioSource.volume = 1.0f; - + if (audioSource.volume < 0.95f) audioSource.volume = Mathf.Lerp(audioSource.volume, 1.0f, Time.deltaTime); //smooth transition + else audioSource.volume = 1.0f; //direct transition + } /// /// get song data when song is selected @@ -42,7 +42,7 @@ void Parse() { DirectoryInfo info = new DirectoryInfo(Game_Data.songDirectory); FileInfo[] smFiles = info.GetFiles("*.sm", SearchOption.AllDirectories); - for (int i = 0; i < smFiles.Length; i++) + for (int i = 0; i < smFiles.Length; i++) // loop all files { Song_Parser parser = new Song_Parser(); Song_Parser.Metadata songData = parser.Parse(smFiles[i].FullName); @@ -52,27 +52,47 @@ void Parse() if (songData.valid) { + //Instantiate(Object original, Vector3 position, Quaternion rotation); + // Quaternion.identity - "no rotation" - the object is perfectly aligned with the world or parent axes GameObject songObj = (GameObject)Instantiate(songSelectionTemplate, songSelectionList.transform.position, Quaternion.identity); - songObj.GetComponentInChildren().text = songData.title + " - " + songData.artist; - songObj.transform.parent = songSelectionList.transform; - songObj.transform.localScale = new Vector3(1, 1, 1); + songObj.GetComponentInChildren().text = songData.title + " - " + songData.artist; //the text that is inside the new songObj becomes titl - artist (example - Bad Romance - Gaga) + + + songObj.transform.parent = songSelectionList.transform;//the songObj depends on the whole list position + + songObj.transform.localScale = new Vector3(1, 1, 1); // scale of the transform relative to the parent. + //Get access to the button control Button songBtn = songObj.GetComponentInChildren