コード例 #1
0
    /** Called when any of the on-screen buttons are clicked.
     * buttIndex is unique to each button and corresponds to that button's lane.
     * Checks if any note is in the clickableNotes array for this button's lane. If
     * so, disable it and check how lined up to the button the note was for scoring.
     */
    public void ClickedButt(int buttIndex)
    {
        // hitEffect gameObject that will be generated will appear at coordinates around the pressed button
        float effectX = buttTransforms[buttIndex].position.x + Random.Range(-0.5f * buttTransforms[0].sizeDelta.x, 0.5f * buttTransforms[0].sizeDelta.x);
        float effectY = buttTransforms[buttIndex].position.y + Random.Range(-0.5f * buttTransforms[0].sizeDelta.y, 0.5f * buttTransforms[0].sizeDelta.y);

        if (instance.clickableNotes != null && instance.clickableNotes[buttIndex] != null) // hit a note
        {
            NoteObject n = instance.clickableNotes[buttIndex];                             // get the note we just clicked & deactivate it
            instance.clickableNotes[buttIndex] = null;                                     // stop note occupying this space
            float dist = Mathf.Abs(n.GetY() - instance.buttTransforms[0].anchoredPosition.y);
            DestroyImmediate(n.gameObject);

            // Resolve note
            if (dist <= 15)
            {
                Debug.Log("PERFECT!");
                GenerateHitEffect(3, effectX, effectY);
                UpdateScore(3);
            }
            else if (dist <= 30)
            {
                Debug.Log("Good!");
                GenerateHitEffect(2, effectX, effectY);
                UpdateScore(2);
            }
            else
            {
                Debug.Log("Okay!");
                GenerateHitEffect(1, effectX, effectY);
                UpdateScore(1);
            }
        }
        else   // hit nothing
        {
            Debug.Log("PENALTY");
            GenerateHitEffect(0, effectX, effectY);
            UpdateScore(-1);
        }
    }