void Update() { if (MidiInput.GetKeyDown(keyNumber + 21)) { isDown = true; if (keyNumber == song.noteList[0].noteNum) { // If the MIDI input matches the current target note... material.color = Color.Lerp(material.color, Color.blue, 1 - Time.deltaTime); if (song.noteList.Count == 1) { text.Invoke("Complete", 0); } else { // Pop queue, trigger corresponding effects, update text front.Invoke("PopEffect", 0f); song.noteList.RemoveAt(0); text.text_data.RemoveAt(0); text.Invoke("Refresh", 0); } } else { // If note played is incorrect, change color of both the note text and corresponding // piano key to red material.color = Color.Lerp(material.color, Color.red, 1 - Time.deltaTime); front.front.color = Color.Lerp(front.text.color, Color.red, 1 - Time.deltaTime); Debug.LogFormat("WRONG NOTE! Expected: {0} {1}", song.noteList[0].noteName, song.noteList[0].noteOctave, song.noteList[0].duration); } } else if (MidiInput.GetKeyUp(keyNumber + 21)) { // Reset colors to default if no note is played isDown = false; material.color = Color.Lerp(material.color, col, 1 - Time.deltaTime); front.front.color = Color.Lerp(front.text.color, Color.green, 1 - Time.deltaTime); } }
// Update is called once per frame void Update() { for (int i = 0; i < 128; i++) { if (MidiInput.GetKeyUp(i)) { foreach (var glow in glows) { if (glow.note == i) { glow.note = -1; glow.renderer.enabled = false; } } // Debug.Log("Releasing " + i + "!"); } if (MidiInput.GetKeyDown(i)) { hasPressedDown = true; foreach (var glow in glows) { if (glow.note == -1) { glow.note = i; glow.renderer.enabled = true; int offsetIndex = GetOffsetFromMiddleC(i); //60 is middleC glow.offsetFromMiddleC = offsetIndex; glow.transform.localPosition = new Vector3(0, MIDDLEY + (float)offsetIndex * SPREADY / 2f, 0); glow.audioSource.Stop(); glow.audioSource.pitch = QNoteHelper.GetFrequencyForIndex(i) / QNoteHelper.GetFrequencyForIndex(60); glow.audioSource.Play(); break; } } // Debug.Log("Pressing " + i + "!"); } } NoteTarget currentTarget = null; int futureTargets = 0; for (int r = 0; r < targets.Count; r++) { var target = targets[r]; //remove far left targets here if (targets.Count > TOTAL_TARGETS) { targets.Remove(target); target.Remove(); r--; } else { if (!target.hasBeenPlayed) { if (currentTarget == null) { currentTarget = target; } else { futureTargets++; } } } } if (currentTarget == null) { currentTarget = CreateTarget(); } while (futureTargets < FUTURE_TARGETS) { CreateTarget(); futureTargets++; } bool wasNoteTouched = false; if (hasPressedDown) //must press before we start to touch the note { foreach (var glow in glows) { if (glow.note != -1 && glow.offsetFromMiddleC == currentTarget.offsetFromMiddleC) { currentTarget.energy -= Time.deltaTime; wasNoteTouched = true; } } } if (wasNoteTouched) { currentTarget.renderer.color = Color.green; } else { currentTarget.renderer.color = Color.white; currentTarget.energy = TIME_TO_SELECT; } if (currentTarget.energy <= 0) //played the note! { currentTarget.hasBeenPlayed = true; scrollXTarget -= SPREADX; //scroll timeline hasPressedDown = false; } scrollX += (scrollXTarget - scrollX) * 0.1f; //ease towards scroll target foreach (var target in targets) { target.transform.localPosition = new Vector3(scrollX + target.baseX, target.baseY, 0); } }