SpawnKeys.cs 7.13 KB
Newer Older
Mitchie Maluschnig's avatar
Mitchie Maluschnig committed
1
2
3
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
4
using UnityEngine.UI;
Mitchie Maluschnig's avatar
Mitchie Maluschnig committed
5
6
7
8
9

public class SpawnKeys : MonoBehaviour {

    public bool ComputerCheck = false;
    public TextAsset songFile;
nedboy3's avatar
nedboy3 committed
10
11
12
13
    public GameObject Q;
    public GameObject W;
    public GameObject E;
    public GameObject R;
14
15
16
17
    public GameObject up;
    public GameObject down;
    public GameObject left;
    public GameObject right;
Mitchie Maluschnig's avatar
Mitchie Maluschnig committed
18
19
    public float skip;
    public float volumeFade;
20
    public bool qwer = true;
astatham's avatar
astatham committed
21
    public float playSpeed = 1.0f;
Mitchie Maluschnig's avatar
Mitchie Maluschnig committed
22

gstuitje's avatar
gstuitje committed
23
    private bool songSet = false;
Mitchie Maluschnig's avatar
Mitchie Maluschnig committed
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
    private string song = "default";
    private float startTime;
    private bool endSong = false;
    private AudioSource music;

    Queue<string[]> songKeys = new Queue<string[]>();
    string[] nextKey;

    int pointer = 0;

    float songWaitTime;

    private LevelManager levelManager;

	// Use this for initialization
	void Start () {
astatham's avatar
astatham committed
40
        print("Speed = " + playSpeed);
Mitchie Maluschnig's avatar
Mitchie Maluschnig committed
41
42
    }
	
43
44
45
46
    public void startTutorial()
    {
        songSet = true;
    }
Mitchie Maluschnig's avatar
Mitchie Maluschnig committed
47
48
49
    //plays the song after the first arrow reaches a barrier.
    private IEnumerator playSong(AudioClip musicClip, float waitTime){
        yield return new WaitForSeconds(waitTime);
gstuitje's avatar
gstuitje committed
50
        music = transform.GetComponent<AudioSource>();
51
        music.clip = musicClip;
Mitchie Maluschnig's avatar
Mitchie Maluschnig committed
52
53
54
        music.Play();
        music.time = skip;
    }
gstuitje's avatar
gstuitje committed
55

56
    // Update is called once per frame
Mitchie Maluschnig's avatar
Mitchie Maluschnig committed
57
58
59
60
    private IEnumerator testNote(string arrow){
        yield return new WaitForSeconds(songWaitTime);
        TriggerManager tester = GameObject.FindWithTag("TriggerManager").GetComponent<TriggerManager>();
        tester.test(arrow);
61
62
    }

Mitchie Maluschnig's avatar
Mitchie Maluschnig committed
63
64
65
66
67
    private IEnumerator silasDance(string arrow)
    {
        yield return new WaitForSeconds(songWaitTime);
        TriggerManager tester = GameObject.FindWithTag("TriggerManager").GetComponent<TriggerManager>();
        tester.SilasDance(arrow);
68
69
    }

70
71
72
73
74
75
76
77
78
79
80
    private IEnumerator tutorialpause()
    {
        yield return new WaitForSeconds(2.1f);
        print("Something" + songWaitTime);
        Time.timeScale = 0f;
        music.Pause();
        GameManager.SilasNoteInPlace = true;
    }


 
Mitchie Maluschnig's avatar
Mitchie Maluschnig committed
81
    void Update () {
gstuitje's avatar
gstuitje committed
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
        if(songSet == true){
            levelManager = GetComponent<LevelManager>();
            startTime = Time.time;

            int count = 0;

            //Same as above but uses queues, will have to parse
            //string of timings into a float later on.
            print("Queue version!");
            foreach (string line in songFile.text.Split('\n'))
            {
                if (count != 0)
                {
                    songKeys.Enqueue(line.Split(' '));
                }
                else
                {
                    song = line.Split(' ')[0];
                    print("Song: " + song);
                    songWaitTime = float.Parse(line.Split(' ')[1]);
                }
                count++;
            }
            nextKey = songKeys.Dequeue();
            //print out queue
            //while (songKeys.Count > 0)
            //{
            //    string[] x = songKeys.Dequeue();
            //    print(x[0] + " " + x[1]);
            //}


            AudioClip songAudio = Resources.Load<AudioClip>(song);
nedboy3's avatar
nedboy3 committed
115
            songWaitTime = songWaitTime / Q.GetComponent<Key>().speed;
gstuitje's avatar
gstuitje committed
116
117
118
            StartCoroutine(playSong(songAudio, songWaitTime));
            songSet = false;
        }
119
        if(endSong == true && music != null){
Mitchie Maluschnig's avatar
Mitchie Maluschnig committed
120
121
            music.volume  -= (volumeFade * Time.deltaTime);
        }
astatham's avatar
astatham committed
122
        while (nextKey != null && Time.time > startTime + (float.Parse(nextKey[1])/playSpeed) - skip)
Mitchie Maluschnig's avatar
Mitchie Maluschnig committed
123
        {
124
125
126
127
128
129
130
            if (float.Parse(nextKey[1]) >= skip)
            {
                switch (nextKey[0])
                {

                    case "left":
                        //print("left");
131
132
133
134
135
136
137
                        if (qwer)
                        {
                            Instantiate(Q, new Vector3(4.85f, 4f, 1f), Quaternion.identity);
                        } else
                        {
                            Instantiate(left, new Vector3(4.85f, 4f, 1f), Quaternion.identity);
                        }
Mitchie Maluschnig's avatar
Mitchie Maluschnig committed
138
139
140
141
                        if(ComputerCheck == true){
                            StartCoroutine(testNote("Left"));
                        }
                        StartCoroutine(silasDance("Left"));
142

143
144
145
                        break;
                    case "right":
                        //print("right");
146
147
148
149
150
151
152
                        if (qwer)
                        {
                            Instantiate(R, new Vector3(8.82f, 4f, 1f), Quaternion.identity);
                        } else
                        {
                            Instantiate(right, new Vector3(8.82f, 4f, 1f), Quaternion.identity);
                        }
Mitchie Maluschnig's avatar
Mitchie Maluschnig committed
153
154
155
                        if (ComputerCheck == true)
                        {
                            StartCoroutine(testNote("Right"));
156
                        }
157
                        StartCoroutine(silasDance("Right"));
158
159
160
                        break;
                    case "up":
                        //print("up");
161
162
163
164
165
166
167
                        if (qwer)
                        {
                            Instantiate(W, new Vector3(6.18f, 4f, 1f), Quaternion.identity);
                        } else
                        {
                            Instantiate(up, new Vector3(6.18f, 4f, 1f), Quaternion.identity);
                        }
Mitchie Maluschnig's avatar
Mitchie Maluschnig committed
168
169
170
                        if (ComputerCheck == true)
                        {
                            StartCoroutine(testNote("Up"));
171
                        }
172
                        StartCoroutine(silasDance("Up"));
173
174
175
                        break;
                    case "down":
                        //print("DOWN");
176
177
178
179
180
181
182
                        if (qwer)
                        {
                            Instantiate(E, new Vector3(7.51f, 4f, 1f), Quaternion.identity);
                        } else
                        {
                            Instantiate(down, new Vector3(7.51f, 4f, 1f), Quaternion.identity);
                        }
Mitchie Maluschnig's avatar
Mitchie Maluschnig committed
183
184
185
                        if (ComputerCheck == true)
                        {
                            StartCoroutine(testNote("Down"));
186
                        }
187
                        StartCoroutine(silasDance("Down"));
188
189
190
                        break;
                    default:
                        break;
Mitchie Maluschnig's avatar
Mitchie Maluschnig committed
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
                }
            }
            if (songKeys.Count > 0)
            {
                nextKey = songKeys.Dequeue();
            }
            else
            {
                if(!endSong){
                    levelManager.SongFinished();
                }
                //no more keys
                nextKey = null;
                endSong = true;
            }
        }
    }
208
209

    public void Bavaria(){
gstuitje's avatar
gstuitje committed
210
211
212
        songFile = Resources.Load<TextAsset>("Bavaria");
        print(songFile);
        songSet = true;
213
214
215
    }

    public void Tarantell(){
gstuitje's avatar
gstuitje committed
216
217
        songFile = Resources.Load<TextAsset>("Tarantella");
        songSet = true;
218
    }
219

220
221
222
223
224
225
    public void Meatball()
    {
        songFile = Resources.Load<TextAsset>("Meatball");
        songSet = true;
    }

226
227
228
229
    public void setVolume(float volume)
    {
        music.volume = volume;
    }
Mitchie Maluschnig's avatar
Mitchie Maluschnig committed
230
}