예제 #1
0
    ///<summary>Converts the angle of a swipe on the screen into a direction for the snake (`Snake.DIRECTION_*`).
    /// If a direction cannot be determined, returns -1.</summary>
    public int directionForSwipe(float swipeAngle)
    {
        // use the onscreen angles from the center of the direction cube to the corners as boundaries for the directions
        Vector2 centerScreenPoint   = this.smallCamera.WorldToScreenPoint(this.center.position); // (implicit conversion from Vector3)
        Vector2 posXposZScreenPoint = this.smallCamera.WorldToScreenPoint(this.posXposZ.position);
        Vector2 negXposZScreenPoint = this.smallCamera.WorldToScreenPoint(this.negXposZ.position);
        Vector2 negXnegZScreenPoint = this.smallCamera.WorldToScreenPoint(this.negXnegZ.position);
        Vector2 posXnegZScreenPoint = this.smallCamera.WorldToScreenPoint(this.posXnegZ.position);
        float   posXposZAngle       = Swipes.angleBetweenPoints(centerScreenPoint, posXposZScreenPoint);
        float   negXposZAngle       = Swipes.angleBetweenPoints(centerScreenPoint, negXposZScreenPoint);
        float   negXnegZAngle       = Swipes.angleBetweenPoints(centerScreenPoint, negXnegZScreenPoint);
        float   posXnegZAngle       = Swipes.angleBetweenPoints(centerScreenPoint, posXnegZScreenPoint);

        //Debug.Log("+x+z: " + posXposZAngle + ", -x+z: " + negXposZAngle + ", -x-z: " + negXnegZAngle + ", +x-z: " + posXnegZAngle);

        if (this.angleIsInRange(swipeAngle, posXposZAngle, negXposZAngle))
        {
            return(Snake.DIRECTION_POS_Z);
        }
        else if (this.angleIsInRange(swipeAngle, negXposZAngle, negXnegZAngle))
        {
            return(Snake.DIRECTION_NEG_X);
        }
        else if (this.angleIsInRange(swipeAngle, negXnegZAngle, posXnegZAngle))
        {
            return(Snake.DIRECTION_NEG_Z);
        }
        else if (this.angleIsInRange(swipeAngle, posXnegZAngle, posXposZAngle))
        {
            return(Snake.DIRECTION_POS_X);
        }
        return(-1);
    }