Advertisement
Jon50

Manos Okouzpulos's GameManager

Jan 9th, 2020
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.49 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEditor;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7.  
  8. public class GameManager : MonoBehaviour //Class names use always PascalCase.
  9. {
  10.     //Public variables are used in very specific cases, if you need to show in the inspector, use the SerializeField property.
  11.     //Keep this in mind also for methods, only mark them as public, if it is really needed.
  12.     //Also initializing your variables, even to null and/or 0, you will get rid of all the warnings in the unity's console.
  13.     [SerializeField] private List<string> myList = new List<string>();
  14.     [SerializeField] private AudioSource[] soundNotes = null;
  15.     [SerializeField] private GameObject[] noteDot = null;
  16.     [SerializeField] private InputField inputNotes = null;
  17.     //[SerializeField] private string LastCharacterofGetList = null; //Not in use.
  18.     [SerializeField] private float notesDelay = 0f;
  19.     [SerializeField] private int counterx = 0;
  20.  
  21.     //private List<string> GetList = new List<string>(); //Not in use anymore.
  22.  
  23.     private void Awake()
  24.     {
  25.         //Use this if you want to keep your methods private, the other way is to make InputfieldToUpperCase a public method
  26.         //and in the InputField object add an "On Value Changed" event, reference this script and call the method.
  27.  
  28.         //How you want to do it is up to you.
  29.  
  30.         //This will add a listener to the InputField and whenever you type in it, it will call InputfieldToUpperCase, so you don't have to call it every frame.
  31.         inputNotes.onValueChanged.AddListener(delegate { InputfieldToUpperCase(); });
  32.     }
  33.  
  34.     void Update()
  35.     {
  36.         //make inputnotes To UpperCase
  37.         //InputfieldToUpperCase();
  38.  
  39.         if (Input.GetKeyDown(KeyCode.V)) //This is my personal opinion but I think it is much better to use KeyCode instead of a string.
  40.         {
  41.  
  42.         }
  43.     }
  44.  
  45.     public void DoSomethingWithNotes()
  46.     {
  47.         //AddInListTheInputNotesEachSeparateLetter(inputNotes.text);    //This method doesn't make sense to me (unless you are using it elsewhere).
  48.         //You are adding each character to a list and later you're converting it back to a string.
  49.         //Just use the inputNotes.text inside the ReturnNotes method.
  50.         AddsTheNotesTomyList();
  51.         StartCoroutine(SetActiveWithString(myList));
  52.     }
  53.  
  54.     //this method adds in a list each letter
  55.     // public void AddInListTheInputNotesEachSeparateLetter(string inputCharacters) //Vague names make things confusing. Changed from "x" to "inputCharacters".
  56.     // {
  57.     //     for (int i = 0; i < inputCharacters.Length; i++)
  58.     //     {
  59.     //         GetList.Add(inputCharacters[i].ToString());
  60.     //     }
  61.     // }
  62.  
  63.     void AddsTheNotesTomyList()
  64.     {
  65.         //Here makes sense to have a local variable for the ReturnNotes, so you just call it once.
  66.         string notes = ReturnNotes();
  67.  
  68.         //adds the outputName(refers to the each letter in inputnotes field) to the List one by one
  69.         for (int i = 0; i < notes.Length; i++)
  70.         {
  71.             myList.Add(notes[i].ToString());
  72.         }
  73.  
  74.         for (int i = 0; i < myList.Count; i++)
  75.         {
  76.             //where the list contains #
  77.             if (myList[i].Contains("#"))
  78.             {
  79.                 //go to the previous element and merry that together like if
  80.                 //element 1 is A and element 2 is #
  81.                 //Then it will become A#
  82.                 myList[i - 1] = myList[i - 1] + myList[i];
  83.  
  84.                 //This line here just removes the # from the second element(given the example above) after is added to the list.
  85.                 myList.RemoveAt(i);
  86.             }
  87.         }
  88.     }
  89.  
  90.  
  91.     //Returns the notes without Space
  92.     public string ReturnNotes()
  93.     {
  94.         //string outputName = "";
  95.         string withoutSpace = "";
  96.  
  97.         //first we use the outputname to add the whole list into one string
  98.         // for (int i = 0; i < GetList.Count; i++)
  99.         // {
  100.         //     outputName += GetList[i];
  101.         // }
  102.  
  103.         //removes the space from the outputName so something like A B C it converts it to ABC
  104.  
  105.         foreach (var letter in inputNotes.text) //You can use inputNotes.text directly here, having AddInListTheInputNotesEachSeparateLetter method is confusing.
  106.         {                                       //Since we don't make use of the GetList anymore, the outputName and the for loop are not needed.
  107.             if (letter == ' ')
  108.             {
  109.                 continue;
  110.             }
  111.             withoutSpace += letter;
  112.         }
  113.  
  114.         return withoutSpace;
  115.     }
  116.  
  117.  
  118.     //this method just makes the Inputfield to UpperCase
  119.     void InputfieldToUpperCase()
  120.     {
  121.         //I really think this check is not needed, you can keep it but I think the messege is clearer and more simple without it.
  122.         inputNotes.text = inputNotes.text.ToUpper();
  123.  
  124.         // string text = inputNotes.text;
  125.         // if (text != inputNotes.text.ToUpper())
  126.         // {
  127.         // }
  128.     }
  129.  
  130.     public void Clear()
  131.     {
  132.         //GetList.Clear(); //Not in use anymore.
  133.         myList.Clear();
  134.         counterx = 0;
  135.     }
  136.  
  137.     IEnumerator SetActiveWithString(List<string> texts)
  138.     {
  139.         for (int i = 0; i < texts.Count; i++)
  140.         {
  141.             switch (texts[i])
  142.             {
  143.                 case "C":
  144.                     {
  145.                         if (noteDot[0].activeSelf)
  146.                         {
  147.                             noteDot[0].SetActive(false);
  148.                         }
  149.  
  150.                         counterx++;
  151.  
  152. #if UNITY_EDITOR //https://docs.unity3d.com/Manual/PlatformDependentCompilation.html
  153.  
  154.                         if (counterx > 1)
  155.                         {
  156.                             Debug.Log("counterx is " + counterx);
  157.                         }
  158. #endif
  159.  
  160.                         yield return PlayNote(soundNotes[0], noteDot[0]);
  161.                         break;
  162.                     }
  163.  
  164.                 case "D":
  165.                     yield return PlayNote(soundNotes[1], noteDot[1]);
  166.                     break;
  167.                 case "E":
  168.                     yield return PlayNote(soundNotes[2], noteDot[2]);
  169.                     break;
  170.                 case "F":
  171.                     yield return PlayNote(soundNotes[3], noteDot[3]);
  172.                     break;
  173.                 case "G":
  174.                     yield return PlayNote(soundNotes[4], noteDot[4]);
  175.                     break;
  176.                 case "G#":
  177.                     yield return PlayNote(soundNotes[5], noteDot[10]);
  178.                     break;
  179.                 case "A":
  180.                     yield return PlayNote(soundNotes[6], noteDot[5]);
  181.                     break;
  182.                 case "B":
  183.                     yield return PlayNote(soundNotes[7], noteDot[6]);
  184.                     break;
  185.                 case "A#":
  186.                     yield return PlayNote(soundNotes[8], noteDot[9]);
  187.                     break;
  188.                 case "C#":
  189.                     yield return PlayNote(soundNotes[9], noteDot[7]);
  190.                     break;
  191.                 case "F#":
  192.                     yield return PlayNote(soundNotes[10], noteDot[8]);
  193.                     break;
  194.             }
  195.         }
  196.  
  197.         yield return null;
  198.     }
  199.  
  200.     private IEnumerator PlayNote(AudioSource soundNotes, GameObject noteDot)
  201.     {
  202.         soundNotes.Play();
  203.         noteDot.SetActive(true);
  204.         yield return new WaitForSeconds(notesDelay);
  205.         noteDot.SetActive(false);
  206.     }
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement