Exemplo n.º 1
0
    // ----------------------
    // Update()
    // ----------------------

    void Update()
    {
        if (this.ctrl != null)
        {
            // ----------------------
            // Stick and Zone Variables
            // ----------------------

            TouchStick walkStick  = this.ctrl.GetStick(STICK_WALK);
            TouchZone  actionZone = this.ctrl.GetZone(ZONE_ACTION);
            TouchZone  screenZone = this.ctrl.GetZone(ZONE_SCREEN);


            // ----------------------
            // Input Handling Code
            // ----------------------

            // ----------------
            // Stick 'Walk'...
            // ----------------

            if (walkStick.Pressed())
            {
                Vector2             walkVec      = walkStick.GetVec();
                float               walkTilt     = walkStick.GetTilt();
                float               walkAngle    = walkStick.GetAngle();
                TouchStick.StickDir walkDir      = walkStick.GetDigitalDir(true);
                Vector3             walkWorldVec = walkStick.GetVec3d(false, 0);

                // Your code here.
            }

            // ----------------
            // Zone 'Action'...
            // ----------------

            if (actionZone.JustUniPressed())
            {
                Vector2 actionPressStartPos = actionZone.GetUniStartPos(TouchCoordSys.SCREEN_PX);
            }

            // Uni-touch pressed...

            if (actionZone.UniPressed())
            {
                float   actionUniDur          = actionZone.GetMultiTouchDuration();
                Vector2 actionUniPos          = actionZone.GetMultiPos();
                Vector2 actionUniDragDelta    = actionZone.GetMultiDragDelta();
                Vector2 actionUniRawDrawDelta = actionZone.GetMultiDragDelta(true);
            }


            // Uni-Touch Just Released

            if (actionZone.JustUniReleased())
            {
                Vector2 actionUniRelStartPos = actionZone.GetReleasedUniStartPos();
                Vector2 actionUniRelEndPos   = actionZone.GetReleasedUniEndPos();
                int     actionUniRelStartBox = TouchZone.GetBoxPortion(2, 2, actionZone.GetReleasedUniStartPos(TouchCoordSys.SCREEN_NORMALIZED));
                int     actionUniRelEndBox   = TouchZone.GetBoxPortion(2, 2, actionZone.GetReleasedUniEndPos(TouchCoordSys.SCREEN_NORMALIZED));

                Vector2 actionUniRelDragVel = actionZone.GetReleasedUniDragVel();
                Vector2 actionUniRelDragVec = actionZone.GetReleasedUniDragVec();
            }


            // ----------------
            // Zone 'SCREEN'...
            // ----------------

            if (screenZone.JustMultiPressed())
            {
                Vector2 screenMultiPressStartPos = screenZone.GetMultiStartPos(TouchCoordSys.SCREEN_PX);
            }

            if (screenZone.JustUniPressed())
            {
                Vector2 screenPressStartPos = screenZone.GetUniStartPos(TouchCoordSys.SCREEN_PX);
            }

            // Multi-Pressed...

            if (screenZone.MultiPressed())
            {
                float   screenMultiDur          = screenZone.GetMultiTouchDuration();
                Vector2 screenMultiPos          = screenZone.GetMultiPos();
                Vector2 screenMultiDragDelta    = screenZone.GetMultiDragDelta();
                Vector2 screenMultiRawDrawDelta = screenZone.GetMultiDragDelta(true);


                // Multi-touch drag...

                if (screenZone.JustMultiDragged())
                {
                }

                if (screenZone.MultiDragged())
                {
                }

                // Just Twisted...

                if (screenZone.JustTwisted())
                {
                }

                // Twisted...

                if (screenZone.Twisted())
                {
                    float screenTwistDelta = screenZone.GetTwistDelta();
                    float screenTwistTotal = screenZone.GetTotalTwist();
                }

                // Just Pinched...

                if (screenZone.JustPinched())
                {
                }

                // Pinched...

                if (screenZone.Pinched())
                {
                    float screenPinchRelScale = screenZone.GetPinchRelativeScale();
                    float screenPinchAbsScale = screenZone.GetPinchScale();
                    float screenFingerDist    = screenZone.GetPinchDist();
                }
            }

            // Uni-touch pressed...

            if (screenZone.UniPressed())
            {
                float   screenUniDur          = screenZone.GetMultiTouchDuration();
                Vector2 screenUniPos          = screenZone.GetMultiPos();
                Vector2 screenUniDragDelta    = screenZone.GetMultiDragDelta();
                Vector2 screenUniRawDrawDelta = screenZone.GetMultiDragDelta(true);


                // Just Uni-touch dragged...

                if (screenZone.JustUniDragged())
                {
                }

                // Uni-Touch drag...

                if (screenZone.UniDragged())
                {
                }
            }


            // Multi-Touch Just Released

            if (screenZone.JustMultiReleased())
            {
                Vector2 screenMultiRelStartPos = screenZone.GetReleasedMultiStartPos();
                Vector2 screenMultiRelEndPos   = screenZone.GetReleasedMultiEndPos();
                int     screenMultiRelStartBox = TouchZone.GetBoxPortion(2, 2, screenZone.GetReleasedMultiStartPos(TouchCoordSys.SCREEN_NORMALIZED));
                int     screenMultiRelEndBox   = TouchZone.GetBoxPortion(2, 2, screenZone.GetReleasedMultiEndPos(TouchCoordSys.SCREEN_NORMALIZED));

                Vector2 screenMultiRelDragVel = screenZone.GetReleasedMultiDragVel();
                Vector2 screenMultiRelDragVec = screenZone.GetReleasedMultiDragVec();

                // Released multi-touch was dragged...

                if (screenZone.ReleasedMultiDragged())
                {
                }

                // Released multi-touch was twisted...

                if (screenZone.ReleasedTwisted())
                {
                    float screenRelTwistAngle = screenZone.GetReleasedTwistAngle();
                    float screenRelTwistVel   = screenZone.GetReleasedTwistVel();
                }

                // Released multi-touch was pinched...

                if (screenZone.ReleasedPinched())
                {
                    float screenRelPinchStartDist = screenZone.GetReleasedPinchStartDist();
                    float screenRelPinchEndDist   = screenZone.GetReleasedPinchEndDist();
                    float screenRelPinchScale     = screenZone.GetReleasedPinchScale();
                }
            }


            // Uni-Touch Just Released

            if (screenZone.JustUniReleased())
            {
                Vector2 screenUniRelStartPos = screenZone.GetReleasedUniStartPos();
                Vector2 screenUniRelEndPos   = screenZone.GetReleasedUniEndPos();
                int     screenUniRelStartBox = TouchZone.GetBoxPortion(2, 2, screenZone.GetReleasedUniStartPos(TouchCoordSys.SCREEN_NORMALIZED));
                int     screenUniRelEndBox   = TouchZone.GetBoxPortion(2, 2, screenZone.GetReleasedUniEndPos(TouchCoordSys.SCREEN_NORMALIZED));

                Vector2 screenUniRelDragVel = screenZone.GetReleasedUniDragVel();
                Vector2 screenUniRelDragVec = screenZone.GetReleasedUniDragVec();


                // Released uni-touch was dragged...

                if (screenZone.ReleasedUniDragged())
                {
                }
            }


            // Double multi-finger tap...

            if (screenZone.JustMultiDoubleTapped())
            {
                Vector2 screenMultiDblTapPos = screenZone.GetMultiTapPos();
            }
            else

            // Simple multi-finger tap...

            if (screenZone.JustMultiTapped())
            {
                Vector2 screenMultiTapPos = screenZone.GetMultiTapPos();
            }
            else

            // Double one-finger tap...

            if (screenZone.JustDoubleTapped())
            {
                Vector2 screenDblTapPos = screenZone.GetTapPos();
            }
            else

            // Simple one-finger tap...

            if (screenZone.JustTapped())
            {
                Vector2 screenTapPos = screenZone.GetTapPos();
            }
        }
    }
// ----------------
    private void Update()
    {
        if (Input.GetKeyUp(KeyCode.Escape))
        {
            DemoSwipeMenuCS.LoadMenuScene();
            return;
        }

        // Manually poll and update the controller...

        this.ctrl.PollController();
        this.ctrl.UpdateController();


        // Control and update the player controller...

        if (this.player != null)
        {
            this.player.ControlByTouch(this.ctrl, this);
            this.player.UpdateChara();
        }



        // Popup box update...

        if (this.popupBox != null)
        {
            if (!this.popupBox.IsVisible())
            {
                if (Input.GetKeyDown(KeyCode.Space))
                {
                    this.popupBox.Show(INSTRUCTIONS_TITLE, INSTRUCTIONS_TEXT,
                                       INSTRUCTIONS_BUTTON_TEXT);
                }
            }
            else
            {
                if (Input.GetKeyDown(KeyCode.Space))
                {
                    this.popupBox.Hide();
                }
            }
        }



        // Control camera...

        TouchZone  zoneScreen = this.ctrl.GetZone(ZONE_SCREEN);
        TouchStick stickWalk  = this.ctrl.GetStick(STICK_WALK);



        // If screen is pressed by two fingers (excluding mid-frame press and release).

        if (zoneScreen.MultiPressed(false, true))
        {
            if (!this.isMultiTouching)
            {
                // If we just started multi-touch, store initial zoom factor.

                this.pinchStartZoom  = this.camZoom;
                this.isMultiTouching = true;

                // Cancel stick's touch if it's shared with our catch-all zone...

                zoneScreen.TakeoverTouches(stickWalk);
            }


            // If pinching is active...

            if (zoneScreen.Pinched())
            {
                // Get pinch distance delta in centimeters (screen-size independence!),
                // then add it to our non-clamped state variable...

                this.pinchStartZoom += this.zoomFactorPerCm *
                                       zoneScreen.GetPinchDistDelta(TouchCoordSys.SCREEN_CM);

                // ... and pass it to proper function when zoom factor will be clamped.

                this.SetZoom(this.pinchStartZoom);
            }
        }

        // If less than two fingers are touching the zone...
        else
        {
            this.isMultiTouching = false;
        }



        // Update camera...

        this.camZoom           = Mathf.Clamp01(this.camZoom);
        this.camZoomForDisplay = Mathf.SmoothDamp(this.camZoomForDisplay, this.camZoom,
                                                  ref this.camZoomVel, this.camSmoothingTime);



        // Place camera...

        this.PlaceCamera();
    }
    // ---------------------
    private void Update()
    {
        if (Input.GetKeyUp(KeyCode.Escape))
        {
            DemoSwipeMenuCS.LoadMenuScene();
            //DemoMenu.LoadMenuLevel();
            return;
        }


        // Popup box update...

        if (this.popupBox != null)
        {
            if (!this.popupBox.IsVisible())
            {
                if (Input.GetKeyDown(KeyCode.Space))
                {
                    this.popupBox.Show(
                        INSTRUCTIONS_TITLE,
                        INSTRUCTIONS_TEXT,
                        INSTRUCTIONS_BUTTON_TEXT);
                }
            }
            else
            {
                if (Input.GetKeyDown(KeyCode.Space))
                {
                    this.popupBox.Hide();
                }
            }
        }


        // Touch controls...

        // Get first zone, since there's only one...

        TouchZone zone = this.touchCtrl.GetZone(0);


        // If two fingers are touching, handle twisting and pinching...

        if (zone.MultiPressed(false, true))
        {
            // Get current mulit-touch position (center) as a pivot point for zoom and rotation...

            Vector2 pivot = zone.GetMultiPos(TouchCoordSys.SCREEN_PX);


            // If pinched, scale map by non-raw relative pinch scale..

            if (zone.Pinched())
            {
                this.ScaleMap(zone.GetPinchRelativeScale(false), pivot);
            }


            // If twisted, rotate map by this frame's angle delta...

            if (zone.Twisted())
            {
                this.RotateMap(zone.GetTwistDelta(false), pivot);
            }
        }

        // If one finger is touching the screen...

        else
        {
            // Single touch...

            if (zone.UniPressed(false, true))
            {
                if (zone.UniDragged())
                {
                    // Drag the map by this frame's unified touch drag delta...

                    Vector2 delta = zone.GetUniDragDelta(TouchCoordSys.SCREEN_PX, false);

                    this.SetMapOffset(this.mapOfs + delta);
                }
            }

            // Double tap with two fingers to zoom-out...

            if (zone.JustMultiDoubleTapped())
            {
                this.ScaleMap(0.5f, zone.GetMultiTapPos(TouchCoordSys.SCREEN_PX));
            }

            // Double tap with one finger to zoom in...

            else if (zone.JustDoubleTapped())
            {
                this.ScaleMap(2.0f, zone.GetTapPos(TouchCoordSys.SCREEN_PX));
            }
        }



        // Keep map on the screen...

        if (this.keepMapInside)
        {
            this.marginFactor = Mathf.Clamp(this.marginFactor, 0, 0.5f);

            Rect safeRect = new Rect(
                Screen.width * this.marginFactor,
                Screen.height * this.marginFactor,
                Screen.width * (1.0f - (2.0f * this.marginFactor)),
                Screen.height * (1.0f - (2.0f * this.marginFactor)));


            Rect mapRect = this.GetMapBoundingBox();


            if (mapRect.xMax < safeRect.xMin)
            {
                this.mapOfs.x -= (mapRect.xMax - safeRect.xMin);
            }

            else if (mapRect.xMin > safeRect.xMax)
            {
                this.mapOfs.x -= (mapRect.xMin - safeRect.xMax);
            }

            if (mapRect.yMax < safeRect.yMin)
            {
                this.mapOfs.y -= (mapRect.yMax - safeRect.yMin);
            }

            else if (mapRect.yMin > safeRect.yMax)
            {
                this.mapOfs.y -= (mapRect.yMin - safeRect.yMax);
            }
        }



        // Smooth map transform...

        if ((Time.deltaTime >= this.smoothingTime))
        {
            this.SnapDisplayTransform();
        }
        else
        {
            float st = (Time.deltaTime / this.smoothingTime);

            this.displayOfs   = Vector2.Lerp(this.displayOfs, this.mapOfs, st);
            this.displayScale = Mathf.Lerp(this.displayScale, this.mapScale, st);
            this.displayAngle = Mathf.Lerp(this.displayAngle, this.mapAngle, st);
        }

        //this.TransformMap();
    }