コード例 #1
0
    // placing/tweaking notes
    void Interaction()
    {
        RaycastHit hit = new RaycastHit();

        activeRing = null;

        // first if we're looking at a Note
        if (Physics.Raycast(transform.position, cam.forward, out hit, Mathf.Infinity, 1 << 12)) // 12 == Note
        {
            activeNote = hit.transform.GetComponent <Note>();
            int n = (int)(activeNote.key / AllInstruments.noteLengths[activeNote.instrumentName]);
            n = n % 12;
            noteIDText.text   = AllInstruments.instrumentNoteIDToName[n]; // this turns the key into an A to G#
            noteNameText.text = activeNote.instrumentName;

            if (Input.GetKeyUp(KeyCode.UpArrow))
            {
                activeNote.IncrementNote();
                activeNote.PlayNote(false, true);
            }

            if (Input.GetKeyUp(KeyCode.DownArrow))
            {
                activeNote.DecrementNote();
                activeNote.PlayNote(false, true);
            }

            if (Input.GetKeyUp(KeyCode.Delete) || Input.GetKeyUp(KeyCode.Backspace))
            {
                activeNote.DeleteNote();
                noteIDText.text = "";
            }

            if (noteLocator.locatorOn)
            {
                noteLocator.AnimateOff();
            }
        } // now if we're looking at a Ring (placing a note)
        else if (Physics.Raycast(transform.position, cam.forward, out hit, Mathf.Infinity, 1 << 10)) // 10 == Ring
        {
            activeRing = hit.transform.GetComponentInParent <Ring>(); // so it's always important the collision object is the child of the ring


            // figure out which note spot we're hitting
            int cellNumber = hit.triangleIndex;
            cellNumber = Mathf.FloorToInt((float)cellNumber / 2f);

            List <Vector3> noteSpots = CalculateNoteSpots(hit);

            int     spotID          = 0;
            Vector3 closestSpotPos  = new Vector3();
            float   closestDistance = Mathf.Infinity;
            for (int i = 0; i < noteSpots.Count; i++)
            {
                float distance = (hit.point - noteSpots[i]).magnitude;
                if (distance < closestDistance)
                {
                    closestSpotPos  = noteSpots[i];
                    closestDistance = distance;
                    spotID          = i;
                }
            }

            noteLocator.transform.position  = closestSpotPos;
            noteLocator.transform.position += (hit.normal * 0.025f);
            var normal = hit.normal;
            noteLocator.transform.rotation = Quaternion.FromToRotation(Vector3.up, normal);// * transform.rotation;


            // drop a note if its empty
            if (Input.GetMouseButtonUp(0))
            {
                if (instrumentPalette.activeInstrumentOption != null && !paused)
                {
                    CreateNote(cellNumber, spotID, closestSpotPos, instrumentPalette.activeInstrumentOption);
                }
            }

            // toggle offset
            if (Input.GetKeyUp(KeyCode.O))
            {
                activeRing.ToggleOffset();
            }

            // destroy <-- TO DO: bring every ring down that's above the one destroyed
            if (Input.GetKeyUp(KeyCode.T))
            {
                rings.Remove(activeRing);
                Destroy(activeRing.gameObject);

                StartCoroutine(MoveRingsToPosition());
            }

            if (noteLocator.lastLocation != (cellNumber * 4) + spotID)
            {
                noteLocator.AnimateOn();
                noteLocator.lastLocation = (cellNumber * 4) + spotID;
            }
        } // end if (looking at ring)
        else
        { // if we're not looking at anything (so far)
            if (Input.GetKeyUp(KeyCode.UpArrow))
            {
                instrumentPalette.activeInstrumentOption.IncrementNote();
                instrumentPalette.OptionClicked(instrumentPalette.activeInstrumentOption);
            }

            if (Input.GetKeyUp(KeyCode.DownArrow))
            {
                instrumentPalette.activeInstrumentOption.DecrementNote();
                instrumentPalette.OptionClicked(instrumentPalette.activeInstrumentOption);
            }

            if (noteLocator.locatorOn)
            {
                noteLocator.AnimateOff();
            }
        }
    }