Beispiel #1
0
    void Update()
    {
        int touchCount = PlatformInput.g.touchCount;

        if (EventSystem.current != null)
        {
            if (!EventSystem.current.IsPointerOverGameObject())
            {
                for (int i = 0; i < touchCount; ++i)
                {
                    Touch      touch                        = PlatformInput.g.getTouch(i);
                    int        touch_fingerId               = touch.fingerId;
                    Ray        touch_world_ray              = m_Camera.ScreenPointToRay(touch.position);
                    GameObject touch_world_ray_hit_obj      = null;
                    Collider   touch_world_ray_hit_collider = null;
                    RaycastHit touch_world_ray_hit_info;

                    if (Physics.Raycast(touch_world_ray, out touch_world_ray_hit_info, float.MaxValue))
                    {
                        touch_world_ray_hit_collider = touch_world_ray_hit_info.collider;
                        touch_world_ray_hit_obj      = touch_world_ray_hit_collider.gameObject;
                    }

                    if (touch.phase == TouchPhase.Began)
                    {
                        m_fingerUpdateData[touch_fingerId].m_GO = null;
                        m_fingerUpdateData[touch_fingerId].m_GO_control_type = fingerUpdateData.EType.none;

                        if (touch_world_ray_hit_obj != null &&
                            !m_fingerUpdateData_isGOControlledByAnyFinger(touch_world_ray_hit_obj))
                        {
                            if (touch_world_ray_hit_obj.name == "HDialIndicator" ||
                                touch_world_ray_hit_obj.name == "VDialIndicator")
                            {
                                m_fingerUpdateData[touch_fingerId].m_GO = touch_world_ray_hit_obj;
                                m_fingerUpdateData[touch_fingerId].m_GO_control_type = fingerUpdateData.EType.moving_in_YZ_plane;

                                m_fingerUpdateData[touch_fingerId].m_moving_GO_in_YZ_plane_Plane = new Plane(Vector3.left, touch_world_ray_hit_obj.transform.position);

                                float ray_dist;
                                m_fingerUpdateData[touch_fingerId].m_moving_GO_in_YZ_plane_Plane.Raycast(touch_world_ray, out ray_dist);
                                m_fingerUpdateData[touch_fingerId].m_moving_GO_in_YZ_plane_Plane_start_hit_point = touch_world_ray.origin + touch_world_ray.direction * ray_dist;
                                m_fingerUpdateData[touch_fingerId].m_moving_GO_in_YZ_plane_GO_start_point        = touch_world_ray_hit_obj.transform.position;

                                if (touch_world_ray_hit_obj.name == "HDialIndicator")
                                {
                                    m_fingerUpdateData[touch_fingerId].m_moving_GO_in_YZ_plane_GO_bounds
                                        = new Bounds(new Vector3(0, (float)-m_WheelModel.rim_d / 2 - m_rim_profile_height * 3f / 4f, m_rim_profile_width * 2)
                                                     - m_HDialIndicator.getStickDefaultWorldOffset(),
                                                     new Vector3(0, m_rim_profile_height * 1.5f, m_rim_profile_width * 1.5f));
                                }
                                else
                                if (touch_world_ray_hit_obj.name == "VDialIndicator")
                                {
                                    m_fingerUpdateData[touch_fingerId].m_moving_GO_in_YZ_plane_GO_bounds
                                        = new Bounds(new Vector3(0, (float)-m_WheelModel.rim_d / 2 - m_rim_profile_height - m_VDialIndicator.getStickTravelMax(), 0)
                                                     - m_VDialIndicator.getStickWorldOffset(),
                                                     new Vector3(0, m_VDialIndicator.getStickTravelMax(), m_rim_profile_width / 2));
                                }
                            }
                            else
                            if (touch_world_ray_hit_obj.layer == layer_wheel_box)
                            {
                                if (!m_is_spoke_rolling & !m_is_wheel_rotating)
                                {
                                    m_fingerUpdateData[touch_fingerId].m_GO = m_wheel_GO;
                                    m_fingerUpdateData[touch_fingerId].m_GO_control_type         = fingerUpdateData.EType.wheel_rotate;
                                    m_fingerUpdateData[touch_fingerId].m_wheel_rotate_Y_modifier = 0.1f;
                                    m_is_wheel_rotating = true;

                                    m_wheel_angular_velocity = 0.0f;
                                }
                            }
                        }
                    }
                    else
                    if (touch.phase == TouchPhase.Moved)
                    {
                        if (m_fingerUpdateData[touch_fingerId].m_GO != null)
                        {
                            switch (m_fingerUpdateData[touch_fingerId].m_GO_control_type)
                            {
                            case fingerUpdateData.EType.moving_in_YZ_plane:
                            {
                                float ray_dist;
                                if (m_fingerUpdateData[touch_fingerId].m_moving_GO_in_YZ_plane_Plane.Raycast(touch_world_ray, out ray_dist))
                                {
                                    Vector3 hit_point = touch_world_ray.origin + touch_world_ray.direction * ray_dist;

                                    Vector3 diff = (hit_point - m_fingerUpdateData[touch_fingerId].m_moving_GO_in_YZ_plane_Plane_start_hit_point);

                                    Vector3 new_pos = m_fingerUpdateData[touch_fingerId].m_moving_GO_in_YZ_plane_GO_start_point + diff;

                                    new_pos.x = Mathf.Clamp(new_pos.x, m_fingerUpdateData[touch_fingerId].m_moving_GO_in_YZ_plane_GO_bounds.min.x,
                                                            m_fingerUpdateData[touch_fingerId].m_moving_GO_in_YZ_plane_GO_bounds.max.x);

                                    new_pos.y = Mathf.Clamp(new_pos.y, m_fingerUpdateData[touch_fingerId].m_moving_GO_in_YZ_plane_GO_bounds.min.y,
                                                            m_fingerUpdateData[touch_fingerId].m_moving_GO_in_YZ_plane_GO_bounds.max.y);

                                    new_pos.z = Mathf.Clamp(new_pos.z, m_fingerUpdateData[touch_fingerId].m_moving_GO_in_YZ_plane_GO_bounds.min.z,
                                                            m_fingerUpdateData[touch_fingerId].m_moving_GO_in_YZ_plane_GO_bounds.max.z);

                                    m_fingerUpdateData[touch_fingerId].m_GO.transform.position = new_pos;
                                }
                                break;
                            }

                            case fingerUpdateData.EType.wheel_rotate:
                            {
                                m_fingerUpdateData[touch_fingerId].m_GO.transform.Rotate(Vector3.forward, touch.deltaPosition.y * m_fingerUpdateData[touch_fingerId].m_wheel_rotate_Y_modifier);
                                break;
                            }
                            }
                        }
                    }
                    else
                    if (touch.phase == TouchPhase.Ended ||
                        touch.phase == TouchPhase.Canceled
                        )
                    {
                        switch (m_fingerUpdateData[touch_fingerId].m_GO_control_type)
                        {
                        case fingerUpdateData.EType.wheel_rotate:
                        {
                            m_wheel_angular_velocity = (m_wheel_angle - m_wheel_angle_prev) / Time.deltaTime;
                            m_is_wheel_rotating      = false;
                            break;
                        }
                        }


                        m_fingerUpdateData[touch_fingerId].m_GO = null;
                    }
                }
            }
        }

        m_wheel_angle_prev = m_wheel_angle;
        m_wheel_angle      = m_wheel_GO.transform.rotation.eulerAngles.z;

        m_current_spoke        = m_WheelModel.getNearestSpokeByAngle(180 + m_wheel_GO.transform.rotation.eulerAngles.z);
        m_current_spoke_rotate = m_WheelModel.getNippleRotate(m_current_spoke);

        if (m_wheel_angular_velocity == 0.0f)
        {
            m_wheel_spoke_wrench_GO.SetActive(true);
        }
        else
        {
            m_wheel_spoke_wrench_GO.SetActive(false);

            m_wheel_GO.transform.Rotate(Vector3.forward, m_wheel_angular_velocity * Time.deltaTime);

            m_wheel_angular_velocity = m_wheel_angular_velocity - m_wheel_angular_velocity * Time.deltaTime * m_wheel_angular_damping;

            if (Mathf.Abs(m_wheel_angular_velocity) < 0.005f)
            {
                m_wheel_angular_velocity = 0.0f;
            }
        }

        m_wheel_spoke_wrench_GO_updateTransformByCurrentSpoke();
    }