Ejemplo n.º 1
0
        /// <summary>
        /// Calculate the pathfinding for the gesture
        /// </summary>
        public static NavNode CalculatePath(Vector3 from, Gesture.Gesture gesture)
        {
            if (EventSystem.current.IsPointerOverGameObject(-1) || EventSystem.current.IsPointerOverGameObject(0))
            {
                return(null);
            }

            Ray        ray      = new Ray(from, Vector3.down);
            RaycastHit startHit = default(RaycastHit);

            if (Physics.Raycast(ray, out startHit, RAY_LENGTH, 1 << TERRAIN_LAYERMASK))
            {
                //Gets the player position on the grid
                if (startHit.collider.name == MobileAI.Instance.m_navMesh.name)
                {
                    gesture.cellsHitten.Insert(0, startHit.triangleIndex);
                }
                else
                {
                    return(null);
                }
            }

            //Store the last node path
            NavNode path = MobileAI.Instance.m_navMesh.PathFinding(gesture.cellsHitten[0], gesture.cellsHitten[1] << 1);

            for (int index = 1; index < gesture.cellsHitten.Count - 1;)
            {
                NavNode tmp      = null;
                int     tmpIndex = index;

                //Since the player may have clicked outside the bound area or over an object,
                //this code assured a path between the last valid location and the final location
                while (tmp == null)
                {
                    if (++tmpIndex >= gesture.cellsHitten.Count)
                    {
                        break;
                    }

                    tmp = MobileAI.Instance.m_navMesh.PathFinding(gesture.cellsHitten[index] << 1, gesture.cellsHitten[tmpIndex] << 1);
                }

                //If the algorithm found a possible path, then concatenate it
                if (tmp != null)
                {
                    path = NavNode.Concatenate(path, tmp);
                }

                //Skip all the invalid index.
                index = tmpIndex;
            }

            return(path);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Method called whenever a registered input happens
 /// </summary>
 public void ValidGesture(Gesture info)
 {
     info.valid = 'y';
 }
Ejemplo n.º 3
0
 /// <summary>
 /// Method called whenever an unregistered input happens
 /// </summary>
 public void InvalidGesture(Gesture info)
 {
     info.valid = 'n';
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Updates the Gesture Manager
        /// </summary>
        public void Update()
        {
            //Get the information about the touches happening on the screen
            if (Input.touchCount > 0)
            {
                foreach (Touch touch in Input.touches)
                {
                    Gesture gesture;

                    //If it is within the list count, get the positional gesture
                    if (touch.fingerId < m_gestures.Count)
                    {
                        gesture = m_gestures[touch.fingerId];
                    }
                    else
                    {
                        gesture             = new Gesture();
                        gesture.cellsHitten = new List <int>();
                    }

                    //Set the gesture parameters
                    gesture.fingerId       = touch.fingerId;
                    gesture.position       = touch.position;
                    gesture.deltaPosition += new Vector3(touch.deltaPosition.x, touch.deltaPosition.y);
                    gesture.deltaTime     += Time.deltaTime;
                    gesture.gameTime       = Time.time;
                    gesture.details        = string.Empty;
                    gesture.ended          = touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled;

                    if (touch.fingerId < m_gestures.Count)
                    {
                        m_gestures[touch.fingerId] = gesture;
                    }
                    else
                    {
                        m_gestures.Add(gesture);
                    }
                }
            }
            else
            {
                Gesture gesture;

                if (m_gestures.Count == 0 && Input.GetMouseButtonDown(0))
                {
                    gesture               = new Gesture();
                    gesture.fingerId      = 0;
                    gesture.phase         = GestureType.Tap;
                    gesture.position      = Input.mousePosition;
                    gesture.deltaPosition = Vector3.zero;
                    gesture.cellsHitten   = new List <int>();
                    gesture.deltaTime     = 0;
                    gesture.gameTime      = Time.time;
                    gesture.details       = string.Empty;
                    gesture.ended         = false;

                    m_gestures.Add(gesture);
                }
                else if (m_gestures.Count == 1)
                {
                    gesture = m_gestures[0];
                    gesture.deltaPosition = Input.mousePosition - gesture.position;
                    gesture.position      = Input.mousePosition;
                    gesture.deltaTime    += Time.deltaTime;

                    if (Input.GetMouseButtonUp(0))
                    {
                        gesture.ended = true;
                    }

                    m_gestures[0] = gesture;
                }
            }

            //Updates the gestures information and rise the events
            for (int i = 0; i < m_gestures.Count; ++i)
            {
                Gesture currentGesture = m_gestures[i];

                if (currentGesture.phase == GestureType.Released)
                {
                    continue;
                }

                //Check if the gesture is in a cell. If so, add the cell to the list only if the cell is not the current one.
                int triangleId = MobileAI.GetTriangleID(currentGesture.position) >> 1;

                if (triangleId != -1)
                {
                    if (currentGesture.cellsHitten.Count == 0)
                    {
                        currentGesture.cellsHitten.Add(triangleId);
                    }
                    else
                    {
                        if (triangleId != currentGesture.cellsHitten[currentGesture.cellsHitten.Count - 1])
                        {
                            currentGesture.cellsHitten.Add(triangleId);
                        }
                    }
                }

                //Check if the drag or hold event has to be fired
                if (currentGesture.phase != GestureType.Drag)
                {
                    //Consider the finger position
                    if (currentGesture.deltaPosition.magnitude >= DRAG_THRESHOLD)
                    {
                        currentGesture.phase = GestureType.Drag;

                        if (OnDrag != null)
                        {
                            OnDrag(currentGesture);
                        }
                        else if (OnInvalidGesture != null)
                        {
                            OnInvalidGesture(currentGesture);
                        }

                        m_log.Add(currentGesture);

                        //Add the trail for the finger
                        if (m_debugTrail)
                        {
                            if (m_trailPrefab == null)
                            {
                                PrefabTrail();
                            }

                            GameObject trail = (GameObject)GameObject.Instantiate(m_trailPrefab, currentGesture.position, Quaternion.identity);
                            m_trails.Add(currentGesture.fingerId, trail);
                        }

                        Debug.Log("Drag");
                    }
                    else if (currentGesture.deltaTime >= CONSIDER_HOLD_AFTER && currentGesture.phase != GestureType.Hold)
                    {
                        currentGesture.phase = GestureType.Hold;

                        if (OnHold != null)
                        {
                            OnHold(currentGesture);

                            if (OnValidGesture != null)
                            {
                                OnValidGesture(currentGesture);
                            }
                        }
                        else if (OnInvalidGesture != null)
                        {
                            OnInvalidGesture(currentGesture);
                        }

                        m_log.Add(currentGesture);

                        Debug.Log("Hold");
                    }
                }
                else if (m_debugTrail)
                {
                    //m_trailRenderer.transform.position = currentGesture.position;
                    //Update the trial for the dragged finger
                    if (MobileAI.Instance.TargetCamera)
                    {
                        Vector3 trailPosition = MobileAI.Instance.TargetCamera.ScreenToWorldPoint(currentGesture.position);                        //new Vector3(currentGesture.position.x, 0, currentGesture.position.y));
                        trailPosition.y -= 10;

                        if (m_trails.ContainsKey(currentGesture.fingerId) != false)
                        {
                            m_trails[currentGesture.fingerId].transform.position = trailPosition;
                        }
                    }
                }

                //The input state change
                if (currentGesture.ended)
                {
                    if (currentGesture.phase == GestureType.Tap)
                    {
                        if (OnTap != null)
                        {
                            OnTap(currentGesture);

                            if (OnValidGesture != null)
                            {
                                OnValidGesture(currentGesture);
                            }
                        }
                        else if (OnInvalidGesture != null)
                        {
                            OnInvalidGesture(currentGesture);
                        }

                        m_log.Add(currentGesture);

                        Debug.Log("Click");
                    }
                    else
                    {
                        if (currentGesture.phase == GestureType.Drag && currentGesture.deltaTime < SWIPE_TIME_THRESHOLD)
                        {
                            if (OnSwipe != null)
                            {
                                OnSwipe(currentGesture);

                                if (OnValidGesture != null)
                                {
                                    OnValidGesture(currentGesture);
                                }
                            }
                            else if (OnInvalidGesture != null)
                            {
                                OnInvalidGesture(currentGesture);
                            }

                            m_log.Add(currentGesture);

                            Debug.Log("Swipe");
                        }
                        else
                        {
                            if (currentGesture.phase != GestureType.Hold)
                            {
                                if (OnRelease != null)
                                {
                                    OnRelease(currentGesture);

                                    if (OnValidGesture != null)
                                    {
                                        OnValidGesture(currentGesture);
                                    }
                                }
                                else if (OnInvalidGesture != null)
                                {
                                    OnInvalidGesture(currentGesture);
                                }

                                m_log.Add(currentGesture);

                                Debug.Log("Release");
                            }
                        }
                    }

                    //Remove the current gesture and continue
                    //currentGesture.phase = GestureType.Released;
                    currentGesture.ended = true;
                }

                m_gestures[i] = currentGesture;
            }

            //Iterate the gesture list backwards to eliminate the resolved touch information
            for (int i = m_gestures.Count - 1; i >= 0; --i)
            {
                if (m_gestures[i].ended)
                {
                    m_gestures.RemoveAt(i);

                    if (m_trails.ContainsKey(i) && m_debugTrail)
                    {
                        GameObject.Destroy(m_trails[i], 1);
                        m_trails.Remove(i);
                    }
                }
                else
                {
                    break;
                }
            }
        }