예제 #1
0
    // Update:
    void Update()
    {
        float escape = Input.GetAxis("escape");

        if (escape > 0.0f)
        {
            Application.Quit();
        }

        // Don't allow the player to make any gestures when the pixie is still
        // busy with the previous command.
        pixie.update();
        if (!pixie.action_finished)
        {
            return;
        }

        // If we're currently learning a new gesture, don't allow new input.
        if (gr.isTraining())
        {
            return;
        }

        // bool button_a_left = Input.GetButton("LeftControllerButtonA");
        // bool button_a_right = Input.GetButton("RightControllerButtonA");

        if (current_step == null)
        {
            return;
        }
        if (current_step.completed)
        {
            current_step = current_step.nextStep();
            current_step.init(ref this.gr);
        }

        float trigger_left  = Input.GetAxis("LeftControllerTrigger");
        float trigger_right = Input.GetAxis("RightControllerTrigger");

        // If the user is not yet dragging (pressing the trigger) on either controller, he hasn't started a gesture yet.
        if (active_controller == null)
        {
            // If the user presses either controller's trigger, we start a new gesture.
            if (trigger_right > 0.9)
            {
                // Right controller trigger pressed.
                active_controller = GameObject.Find("Right Hand");
            }
            else if (trigger_left > 0.9)
            {
                // Left controller trigger pressed.
                active_controller = GameObject.Find("Left Hand");
            }
            else
            {
                // If we arrive here, the user is pressing neither controller's trigger:
                // nothing to do.
                return;
            }
            // If we arrive here: either trigger was pressed, so we start the gesture.
            GameObject hmd   = GameObject.Find("Main Camera"); // alternative: Camera.main.gameObject
            Vector3    hmd_p = hmd.transform.position;
            Quaternion hmd_q = hmd.transform.rotation;
            current_step.dragStart(ref gr, hmd_p, hmd_q);
        }

        // If we arrive here, the user is currently dragging with one of the controllers.
        Vector3    p = active_controller.transform.position;
        Quaternion q = active_controller.transform.rotation;

        current_step.dragContd(ref gr, p, q);
        // Show the stroke by instatiating new objects
        {
            GameObject star_instance = Instantiate(GameObject.Find("star"));
            GameObject star          = new GameObject("stroke_" + stroke_index++);
            star_instance.name = star.name + "_instance";
            star_instance.transform.SetParent(star.transform, false);
            System.Random random = new System.Random();
            star.transform.position = new Vector3(p.x + (float)random.NextDouble() / 80, p.y + (float)random.NextDouble() / 80, p.z + (float)random.NextDouble() / 80);
            star.transform.rotation = new Quaternion((float)random.NextDouble() - 0.5f, (float)random.NextDouble() - 0.5f, (float)random.NextDouble() - 0.5f, (float)random.NextDouble() - 0.5f).normalized;
            //star.transform.rotation.Normalize();
            float star_scale = (float)random.NextDouble() + 0.3f;
            star.transform.localScale = new Vector3(star_scale, star_scale, star_scale);
            stroke.Add(star.name);
        }

        // Check if the user is still dragging or if he let go of the trigger button.
        if (trigger_left < 0.85 && trigger_right < 0.85)
        {
            // the user let go of the trigger, ending a gesture.
            active_controller = null;

            // Delete the objectes that we used to display the gesture.
            foreach (string star in stroke)
            {
                GameObject star_object = GameObject.Find(star);
                if (star_object != null)
                {
                    Destroy(star_object);
                }
            }
            stroke.Clear();
            stroke_index = 0;

            current_step.dragStop(ref gr, ref this.pixie);
        }
    }