Example #1
0
        public void Update()
        {
            if (RuntimeSettings.LOAD_SAVED_SESSION_AT_STARTUP)
            {
                Debug.Log("Loading a saved session");
                RuntimeSettings.LOAD_SAVED_SESSION_AT_STARTUP = false;
                scoreView.SaveScoresAndViewFeedback(RuntimeSettings.CACHED_SESSION, false);
                RuntimeSettings.CACHED_SESSION = null;
                ClearPianoRoll();
                startTime = -1f;
            }

            if (startTime < 0f)
            {
                return;
            }
            var deltaT = Time.time - startTime;

            var minDistDict = new Dictionary <PianoKey, float>();

            foreach (var i in PianoKeys.GetAllKeys())
            {
                minDistDict[i] = 2f;
            }
            noteDurations.ForEach(note =>
            {
                if (!note.hasKeyBeenActivated && deltaT >= note.start - note.duration && deltaT < note.end - note.duration)
                {
                    piano.ActivateKey(note.keyNum, Color.red, note.duration);
                    note.hasKeyBeenActivated = true;
                }

                if (deltaT >= note.start - note.duration && deltaT < note.end - note.duration)
                {
                    minDistDict[PianoKeys.GetKeyFor(note.keyNum)] = 0;
                    return;
                }

                if (deltaT > note.end)
                {
                    return;
                }

                if (deltaT >= note.start - 2f && deltaT <= note.start)
                {
                    minDistDict[PianoKeys.GetKeyFor(note.keyNum)] = Mathf.Min(Mathf.Abs(note.start - deltaT),
                                                                              minDistDict[PianoKeys.GetKeyFor(note.keyNum)]);
                }
            });
            foreach (var item in minDistDict)
            {
                float h, s, v;
                Color.RGBToHSV(colorDict[item.Key], out h, out s, out v);
                if (item.Value == 2f)
                {
                    s = 0f;
                }
                else
                {
                    s = (2 - item.Value) / 2 * s;
                }
                var newc = Color.HSVToRGB(h, s, v);
                newc.a = (2 - item.Value) / 2;
                if (newc.a >= 1f)
                {
                    shouldKeyBeOn[item.Key] = true;
                }
                else
                {
                    shouldKeyBeOn[item.Key] = false;
                }
                piano.UpdateDiskColor(item.Key, newc);
            }

            if (noteDurations.Last().hasKeyBeenActivated || Input.GetKeyDown(KeyCode.Escape))
            {
                scoreView.ConvertEventsSaveScoresAndViewFeedback(_midiDeviceController.GetMidiEvents(),
                                                                 noteDurations.Select(x => new CompressedNoteDuration(x)).ToList(), notesScale,
                                                                 RuntimeSettings.GAME_SPEED, startTime);
                ClearPianoRoll();
                startTime = -1f;
            }

            if (gameStarted)
            {
                var step = RuntimeSettings.GAME_SPEED * Time.deltaTime;
                foreach (var item in pianoRollDict)
                {
                    if (item.Value.Count == 0)
                    {
                        continue;
                    }
                    var lmr = piano.GetLMRAwayVectorsForKey(item.Key);
                    foreach (var obj in item.Value)
                    {
                        obj.transform.Translate(0, -step, 0);
                        if (obj.transform.childCount > 0)
                        {
                            var co         = obj.transform.GetChild(0);
                            var childScale = co.transform.localScale;
                            var mag        = (obj.transform.position - lmr.centre).magnitude;
                            if (mag < childScale.y)
                            {
                                var cs = co.transform.localScale.y;

                                obj.transform.localScale = new Vector3(1f, mag / cs, 1f);
                            }

                            if (mag < 0.01f)
                            {
                                DestroyImmediate(co.gameObject);
                            }
                        }
                    }
                }

                foreach (var obj in fineLines)
                {
                    var center     = PianoKeys.GetKeyFor(PianoBuilder.CENTRE);
                    var centerAway = keyAwayDir[center];
                    obj.transform.position =
                        Vector3.MoveTowards(obj.transform.position, centerAway.transform.position, step);
                }
            }
        }