protected virtual void OnSwipeState(ref TouchQueueInfomation info, long time) { int x = 0; int y = 0; info.touchQueue.GetTrackStartingPosition(ref x, ref y); if (!info.touchQueue.IsActived() || (info.touchQueue.GetCurrentDuration(time) >= mMaxSwipeDuration)) { TouchArcShape arcType = TouchArcShape.ARC_NONE; TouchDirection direction = TouchDirection.DIR_NONE; bool isArc = info.touchQueue.IsArcTrack(mMinXDistanceForArc, mMinYChangePersentForArc, ref arcType, ref direction); if (isArc) { mCurrentGestureEvent = new GestureArcEvent(x, y, time, 1, arcType, direction); } else { mCurrentGestureEvent = new GestureSwipeEvent(x, y, time, 1, direction); } //force deactive the touch queue when it expired if (info.touchQueue.IsActived()) { info.touchQueue.ForceReleaseTouch(); } } else { mChangedTouchQueues.AddLast(info); } }
/// <summary> /// 选择方向 /// </summary> /// <param name="inputPos">输入当前位置</param> /// <returns>方向</returns> private TouchDirection switchDirection(Vector3 inputPos) { durationPos = inputPos - previousPos;//方向 if (Mathf.Abs(durationPos.x) > Mathf.Abs(durationPos.y)) { if (durationPos.x > 0) { touchDir = TouchDirection.Right; } if (durationPos.x < 0) { touchDir = TouchDirection.Left; } } else if (Mathf.Abs(durationPos.y) > Mathf.Abs(durationPos.x)) { if (durationPos.y > 0) { touchDir = TouchDirection.Up; } if (durationPos.y < 0) { touchDir = TouchDirection.Down; } } return(touchDir); }
/// <summary> /// Rotate . /// </summary> /// <param name="direction"></param> public void Rotate(TouchDirection direction) { switch (direction) { case TouchDirection.Left: transform.Rotate(0f, xSpeed, 0f); if (!resetCamera.gameObject.activeInHierarchy) { resetCamera.gameObject.SetActive(true); } break; case TouchDirection.Right: transform.Rotate(0f, -xSpeed, 0f); if (!resetCamera.gameObject.activeInHierarchy) { resetCamera.gameObject.SetActive(true); } break; case TouchDirection.Up: break; case TouchDirection.Down: break; default: throw new ArgumentOutOfRangeException("direction", direction, null); } }
// Update is called once per frame void Update() { if (Input.touchCount > 0) { Touch touch = Input.GetTouch(0); switch (touch.phase) { case TouchPhase.Began: startPos = touch.position; break; case TouchPhase.Moved: direction = touch.position - startPos; break; case TouchPhase.Ended: if (direction.magnitude < 50) { return; //touchResult = TouchDirection.none; } else if (Mathf.Abs(direction.x) > Mathf.Abs(direction.y)) { if (direction.x > 0) { touchResult = TouchDirection.right; } else { touchResult = TouchDirection.left; } } else { if (direction.y > 0) { touchResult = TouchDirection.up; } else { touchResult = TouchDirection.down; } } gotTouch = true; break; } } }
void MoveControl() { TouchDirection direction = GetTouchDirection(); //Debug.Log (direction); if (targetLaneIndex != nowLaneIndex) { float moveLength = Mathf.Lerp(0, moveHorizontal, moveHorizontalSpeed * Time.deltaTime); transform.position = new Vector3(transform.position.x + moveLength, transform.position.y, transform.position.z); moveHorizontal -= moveLength; if (Mathf.Abs(moveHorizontal) < 0.5f) { transform.position = new Vector3(transform.position.x + moveHorizontal, transform.position.y, transform.position.z); moveHorizontal = 0; nowLaneIndex = targetLaneIndex; } } if (isSliding) { slidTimer += Time.deltaTime; if (slidTimer > slidTime) { slidTimer = 0; isSliding = false; } } if (isJumping) { float yMove = jumpSpeed * Time.deltaTime; if (isUp) { prisoner.position = new Vector3(prisoner.position.x, prisoner.position.y + yMove, prisoner.position.z); haveJumpHeight += yMove; if (Mathf.Abs(jumpHeight - haveJumpHeight) < 0.5f) { prisoner.position = new Vector3(prisoner.position.x, prisoner.position.y + jumpHeight - haveJumpHeight, prisoner.position.z); isUp = false; haveJumpHeight = jumpHeight; } } else { prisoner.position = new Vector3(prisoner.position.x, prisoner.position.y - yMove, prisoner.position.z); haveJumpHeight -= yMove; if (Mathf.Abs(haveJumpHeight - 0) < 0.5f) { prisoner.position = new Vector3(prisoner.position.x, prisoner.position.y - (haveJumpHeight), prisoner.position.z); isJumping = false; haveJumpHeight = 0; jumpLandMusic.Play(); } } } }
void ReturnEvent() { double angle = GetAngle(beginPosition, endPosition); TouchDirection TouchDirection = GetTouchDirection(angle); if (touchDelegate != null) { touchDelegate(TouchDirection); } }
private void switchDirection(Vector2 cPos, Vector2 pPos) { //the Horizontal Axis if (Mathf.Abs(cPos.x - pPos.x) > Mathf.Abs(cPos.y - pPos.y)) { if (cPos.x - pPos.x > 0) { currentDir = TouchDirection.Right; } if (cPos.x - pPos.x < 0) { currentDir = TouchDirection.Left; } } //the Vertical Axis else { if (cPos.y - pPos.y > 0) { currentDir = TouchDirection.Up; } if (cPos.y - pPos.y < 0) { currentDir = TouchDirection.Down; } } distanceVector3 = new Vector3(cPos.x - pPos.x, cPos.y - pPos.y, 0); //means control finger move up,target to rotate AxisX rotationXAxis += distanceVector3.y; //means control finger move right,target to rotate AxisY rotationYAxis -= distanceVector3.x;//use the anti direction cause negative number //just rotate target between 180 to -180 rotationXAxis = ClampAngle(rotationXAxis, yMinLimit, yMaxLimit); Quaternion toRotation = Quaternion.Euler(rotationXAxis, rotationYAxis, 0); Vector3 negDistance = new Vector3(0, 0, -startDistance); //camera new position Vector3 tempPos = toRotation * negDistance + target.transform.position; viewCamera.rotation = toRotation; viewCamera.position = tempPos; //Trail compute distanceVector3.x = Mathf.Lerp(distanceVector3.x, 0, Time.deltaTime * smoothTime); distanceVector3.y = Mathf.Lerp(distanceVector3.y, 0, Time.deltaTime * smoothTime); }
/// <summary> /// Method called when we touch the screen (or click on desktop). /// </summary> void OnTouched(TouchDirection td) { if (isGameOver) { return; } if (!isGameOver) { if (Input.GetMouseButtonDown(0)) { DOMoveDots(); } } }
void OnTouched(TouchDirection td) { if (td == TouchDirection.none) { return; } if (td == TouchDirection.left) { OnTouchLeft(); } else { OnTouchRight(); } }
/// <summary> /// Get touch control swipe direction for mobile devices. /// </summary> TouchDirection GetTouchDirection(Vector2 swipeMovement) { TouchDirection swipeDir = TouchDirection.NONE; // Horizontal Swipe if (Mathf.Abs(swipeMovement.x) > Mathf.Abs(swipeMovement.y)) { swipeDir = (swipeMovement.x >= 0) ? TouchDirection.RIGHT : TouchDirection.LEFT; } // Vertical Swipe else { swipeDir = (swipeMovement.y >= 0) ? TouchDirection.UP : TouchDirection.DOWN; } return(swipeDir); }
private void MoveControl() { TouchDirection dir = GetTouchDir(); switch (dir) { case TouchDirection.None: break; case TouchDirection.Right: if (targetIndex < 2) { targetIndex++; moveDistance = 13; } break; case TouchDirection.Left: if (targetIndex > 0) { targetIndex--; moveDistance = -13; } break; case TouchDirection.Top: if (isJump == false) { isJump = true; isUp = true; HaveJumpHeight = 0; } break; case TouchDirection.Bottom: if (!isJump) { isSlide = true; SlideTimer = 0; //清空计时器 } break; } }
void OnTouched(TouchDirection td) { if (_gm.isGameOver) { return; } if (td == TouchDirection.none) { exit.SetActive(false); return; } if (td == TouchDirection.left) { OnTouchLeft(); } else { OnTouchRight(); } }
/// <summary> /// 旋转目标 /// </summary> /// <param name="rotateSpeed"></param> /// <param name="dir"></param> public void RotateTarget(Vector3 durationPos, TouchDirection dir) { float rotateValue = 0; float AxisUpDownDir = IsAxisUpDownResverse ? -1 : 1; switch (dir) { case TouchDirection.Right: rotateValue = durationPos.x; transform.RotateAround(targetRotate.position, Vector3.up, RotateSpeed * rotateValue * Time.deltaTime); break; case TouchDirection.Left: rotateValue = durationPos.x; transform.RotateAround(targetRotate.position, Vector3.up, RotateSpeed * rotateValue * Time.deltaTime); break; case TouchDirection.Up: rotateValue = durationPos.y; transform.RotateAround(targetRotate.position, transform.right, AxisUpDownDir * RotateSpeed * rotateValue * Time.deltaTime); break; case TouchDirection.Down: rotateValue = durationPos.y; transform.RotateAround(targetRotate.position, transform.right, AxisUpDownDir * RotateSpeed * rotateValue * Time.deltaTime); break; case TouchDirection.None: break; default: break; } }
void OnTouched(TouchDirection td) { isGameOver = true; DOStart(); }
/// <summary> /// Handles swipe control end event movement for mobile devices. /// </summary> private void SwipeEndHandler(Vector2 swipeMovement) { _swipeEndDirection = GetTouchDirection(swipeMovement); }
/// <summary> /// Track player's key input or touch input for tetromino movement. /// </summary> void PlayerInputMovement() { // Keyboard Input if (Input.GetKeyDown(KeyCode.UpArrow)) { // Hard Drop curTetroController.HardDropTetromino(); isLastRotation = false; } else if (Input.GetKeyDown(KeyCode.DownArrow)) { // Move down one block MoveTetromino(Vector2Int.down); isLastRotation = false; PlayTetrominoSound(); } else if (Input.GetKeyDown(KeyCode.LeftArrow)) { // Move left one block MoveTetromino(Vector2Int.left); isLastRotation = false; PlayTetrominoSound(); } else if (Input.GetKeyDown(KeyCode.RightArrow)) { // Move right one block MoveTetromino(Vector2Int.right); isLastRotation = false; PlayTetrominoSound(); } // Perform SRS Rotation else if (Input.GetKeyDown(KeyCode.C)) { // Clockwise operation curTetroController.RotateTetromino(true); isLastRotation = true; PlayTetrominoSound(); } else if (Input.GetKeyDown(KeyCode.X)) { // Counter-clockwise operation curTetroController.RotateTetromino(false); isLastRotation = true; PlayTetrominoSound(); } else if (Input.GetKeyDown(KeyCode.Z)) { // Hold tetromino tetroSystem.HoldCurrentTetromino(); PlayTetrominoSound(); } // Touch Movement else if (_swipeEndDirection == TouchDirection.RIGHT && Time.time > _timeToNextSwipe) { // Rotate right // Clockwise operation curTetroController.RotateTetromino(true); isLastRotation = true; PlayTetrominoSound(); _timeToNextSwipe = Time.time + _timeToSwipe; } else if (_swipeEndDirection == TouchDirection.LEFT && Time.time > _timeToNextSwipe) { // Rotate left // Counter-clockwise operation curTetroController.RotateTetromino(false); isLastRotation = true; PlayTetrominoSound(); _timeToNextSwipe = Time.time + _timeToSwipe; } else if (_swipeEndDirection == TouchDirection.UP && Time.time > _timeToNextSwipe) { // Hold tetromino tetroSystem.HoldCurrentTetromino(); PlayTetrominoSound(); _timeToNextSwipe = Time.time + _timeToSwipe; } else if (_swipeEndDirection == TouchDirection.DOWN && Time.time > _timeToNextSwipe) { // Hard drop tetromino curTetroController.HardDropTetromino(); isLastRotation = false; _timeToNextSwipe = Time.time + _timeToSwipe; } else if (_isTapped == true && _isLeftTap == true && _isBottomTap == false) { // Move left MoveTetromino(Vector2Int.left); PlayTetrominoSound(); } else if (_isTapped == true && _isLeftTap == false && _isBottomTap == false) { // Move right MoveTetromino(Vector2Int.right); PlayTetrominoSound(); } else if (_isTapped == true && _isBottomTap == true) { // Move down MoveTetromino(Vector2Int.down); PlayTetrominoSound(); } // Reset touch control _swipeDirection = TouchDirection.NONE; _swipeEndDirection = TouchDirection.NONE; _isTapped = false; _isBottomTap = false; _isLeftTap = false; }
public GestureSwipeEvent(int x, int y, long time, int touchCount, TouchDirection direction) : base(x, y, time, touchCount) { mEventType = GestureEventType.GESTURE_LONG_TAP; mIntParameter = (int)direction; }
//获取一次滑动行为 public TouchDirection GetTouchMoveDirection(TouchDirection dirResult) { if (TouchCount == 1) { //实时检测触碰到UI if (_currentEventSys.IsPointerOverGameObject(Input.touches[0].fingerId)) { dirResult |= TouchDirection.OnUI; IsTouchedOnUICallBack?.Invoke(CurrentIsOnUI); } if (Input.touches[0].phase != TouchPhase.Canceled) { Vector2 inputPos = Input.touches[0].position; switch (Input.touches[0].phase) { case TouchPhase.Began: if (_config.TouchDetectUI && CurrentIsOnUI) { return(dirResult); } _touchBeginPos = inputPos; TouchBeganCallback?.Invoke(inputPos); break; case TouchPhase.Ended: _touchEndPos = inputPos; if (_config.TouchDetectUI && CurrentIsOnUI) { return(dirResult = TouchDirection.None); } TouchEndCallback?.Invoke(inputPos); if (Vector2.Distance(_touchBeginPos, _touchEndPos) < MOVE_MIX_DISTANCE) { return(dirResult); } float offSetX = _touchEndPos.x - _touchBeginPos.x; float offSetY = _touchEndPos.y - _touchBeginPos.y; float angle = Mathf.Atan2(offSetY, offSetX) * Mathf.Rad2Deg; //顺时针偏移 angle += _clockWiseDegree; if (angle > 180) { angle = 360 - angle; } else if (angle < -180) { angle += 360; } if (angle <= 45f && angle > -45f) { dirResult |= TouchDirection.Right; } else if (angle > 45f && angle <= 135f) { dirResult |= TouchDirection.Up; } else if ((angle > 135f && angle < 180f) || (angle > -180f && angle <= -135f)) { dirResult |= TouchDirection.Left; } else if (angle > -135f && angle <= -45f) { dirResult |= TouchDirection.Down; } break; case TouchPhase.Moved: if (_config.TouchDetectUI && CurrentIsOnUI) { return(dirResult); } TouchMoveCallback?.Invoke(inputPos); break; case TouchPhase.Stationary: if (_config.TouchDetectUI && CurrentIsOnUI) { return(dirResult); } TouchStationaryCallback?.Invoke(inputPos); break; } } else { dirResult = TouchDirection.None; } return(dirResult); } else if (TouchCount > 1) { if (Input.GetTouch(0).phase == TouchPhase.Began) { _oldPosition1 = Input.GetTouch(0).position; } if (Input.GetTouch(1).phase == TouchPhase.Began) { _oldPosition2 = Input.GetTouch(1).position; } if (Input.GetTouch(0).phase == TouchPhase.Moved || Input.GetTouch(1).phase == TouchPhase.Moved) { //计算出当前两点触摸点的位置 var tempPosition1 = Input.GetTouch(0).position; var tempPosition2 = Input.GetTouch(1).position; FingerRotateCallBack?.Invoke(LMath.CalcIncludedAngle2D(_oldPosition2 - _oldPosition1, tempPosition2 - tempPosition1)); //缩放数据 float currentTouchDistance = Vector3.Distance(tempPosition1, tempPosition2); float lastTouchDistance = Vector3.Distance(_oldPosition1, _oldPosition2); FingerZoomCallback?.Invoke((lastTouchDistance - currentTouchDistance) * Time.deltaTime); //备份上一次触摸点的位置,用于对比 _oldPosition1 = tempPosition1; _oldPosition2 = tempPosition2; } } return(dirResult = TouchDirection.None); }
// Update is called once per frame void Update() { #if UNITY_STANDALONE || UNITY_EDITOR if (this.playerAction == PlayerAction.None) { if (Input.GetButtonDown("Left")) { this.playerAction = PlayerAction.TurnLeft; } else if (Input.GetButtonDown("Right")) { this.playerAction = PlayerAction.TurnRight; } else if (Input.GetButtonDown("Item")) { this.playerAction = PlayerAction.UseItem; } else if (Input.GetButtonDown("Cancel")) { Application.Quit(); } } #endif if (!(this.gameState.useOnScreenButtons)) { foreach (Touch touch in Input.touches) { //if (this.fingerId >= 0 && touch.fingerId != this.fingerId) // continue; if (/*touch.phase == TouchPhase.Moved && */ touch.deltaPosition.magnitude / touch.deltaTime >= this.swipeSpeed /*&& this.fingerId != touch.fingerId*/) { TouchDirection direction = this.getTouchDirection(touch); if (null == this.lastDirection || !this.lastDirection.Equals(direction)) { this.lastDirection = direction; this.timeSinceLastTouch = Time.time; if (direction.Equals(TouchDirection.LEFT)) { this.playerAction = PlayerAction.TurnLeft; } else if (direction.Equals(TouchDirection.RIGHT)) { this.playerAction = PlayerAction.TurnRight; } else if (direction.Equals(TouchDirection.UP)) { this.playerAction = PlayerAction.UseItem; } } /*if (direction > -1.0f && direction < 1.0f) { if (touch.deltaPosition.x < 0.0f) this.playerAction = PlayerAction.TurnLeft; else this.playerAction = PlayerAction.TurnRight; } else if (touch.deltaPosition.y > 0.0f) this.playerAction = PlayerAction.UseItem; */ this.fingerId = touch.fingerId; } if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled && this.fingerId != touch.fingerId) { this.fingerId = -1; this.lastDirection = TouchDirection.NONE; } } float delta = Time.time - this.timeSinceLastTouch; if (delta > 0.5f) { this.lastDirection = TouchDirection.NONE; } } else { if (this.playerAction == PlayerAction.None) { if (screenButtonLeft.buttonClicked) this.playerAction = PlayerAction.TurnLeft; else if (screenButtonRight.buttonClicked) this.playerAction = PlayerAction.TurnRight; else if (screenButtonItem1.buttonClicked) this.playerAction = PlayerAction.UseItem; else if (screenButtonItem2.buttonClicked) this.playerAction = PlayerAction.UseItem; } } }
// Start is called before the first frame update void Start() { touchResult = TouchDirection.idle; }
void OnTouched(TouchDirection td) { DOStart(); }
private void InputUpdateHandler() { //点击返回 if (Input.GetKeyDown(KeyCode.Escape)) { EscapeBtnClick(); } if (_isInit && IsEnable) { if (_currentPlatform == PLATFORM_ANDROID || _currentPlatform == PLATFORM_IOS) { //是否点击到UI界面 if (TouchCount > 0) { _touchResult = GetTouchMoveDirection(_touchResult); } else { _touchResult = TouchDirection.None; } } else if (_currentPlatform == PLATFORM_WINDOWEDITOR || _currentPlatform == PLATFORM_IOSEDITOR) { var wheel = Input.GetAxis("Mouse ScrollWheel"); FingerZoomCallback?.Invoke(wheel); if (Input.GetMouseButtonDown(0)) { //点击UI检测 if (EventSystem.current.IsPointerOverGameObject()) { _touchResult |= TouchDirection.OnUI; } IsTouchedOnUICallBack?.Invoke(CurrentIsOnUI); if (_config.TouchDetectUI && CurrentIsOnUI) { return; } //按压计时 CalculateTimeByPressStart(Input.mousePosition); //输出开始点击事件 TouchBeganCallback?.Invoke(Input.mousePosition); } if (Input.GetButton("Fire1")) { if (_config.TouchDetectUI && CurrentIsOnUI) { return; } if (_recordedPCPos == Vector3.zero) { _pressTimeCount = 0f; _recordedPCPos = Input.mousePosition; return; } if (_recordedPCPos != Input.mousePosition) { _pressTimeCount = 0f; _recordedPCPos = Input.mousePosition; TouchMoveCallback?.Invoke(Input.mousePosition); } else { _pressTimeCount += Time.deltaTime; if (_pressTimeCount >= _config.TouchStationaryTime) { TouchStationaryCallback?.Invoke(Input.mousePosition); } } } if (Input.GetMouseButtonUp(0)) { _recordedPCPos = Vector2.zero; if (_config.TouchDetectUI && CurrentIsOnUI) { _touchResult = TouchDirection.None; return; } TouchEndCallback?.Invoke(Input.mousePosition); CalculateTimeByPressOver(Input.mousePosition); _touchResult = TouchDirection.None; } } } }