Exemplo n.º 1
0
    private void OnNoteHit(int id, InputDevice device)
    {
        // Calculate time difference.
        float correctTime        = CorrectTime(id, device);
        int   timeDifferenceInMs = Mathf.FloorToInt(
            Mathf.Abs(clampedNoteTime - correctTime) * 1000f);

        // The usual stuff: explosion and keysound.
        if (timeDifferenceInMs <= 200)
        {
            GameObject vfx = Instantiate(
                explosionPrefab, explosionContainer);
            RectTransform rect = vfx.GetComponent <RectTransform>();
            rect.pivot     = new Vector2(0.5f, 0.5f);
            rect.sizeDelta = new Vector2(
                laneHeight * 3f, laneHeight * 3f);
            rect.position = notes[id].transform.position;

            if (lanes[id] == 0)
            {
                audioSourceManager.PlayKeysound(snare,
                                                hiddenLane: false);
            }
            else
            {
                audioSourceManager.PlayKeysound(kick,
                                                hiddenLane: false);
            }
        }

        // Write timing history.
        string historyLine;
        char   deviceLetter = device.ToString()[0];

        if (clampedNoteTime < correctTime)
        {
            historyLine = $"{deviceLetter} {timeDifferenceInMs}ms <color=#{ColorUtility.ToHtmlStringRGB(earlyColor)}>early</color>";
        }
        else
        {
            historyLine = $"{deviceLetter} {timeDifferenceInMs}ms <color=#{ColorUtility.ToHtmlStringRGB(lateColor)}>late</color>";
        }

        timingHistory[id].Add(historyLine);
        StringBuilder history    = new StringBuilder();
        int           lowerBound = Mathf.Max(0, timingHistory[id].Count - 5);

        for (int i = timingHistory[id].Count - 1; i >= lowerBound; i--)
        {
            history.AppendLine(timingHistory[id][i]);
        }
        historyDisplay[id].text = history.ToString();
    }