// ============================================================================== // Reset // ============================================================================== private void Reset() { RotationReset(); rotation = rotationOrg; // restore these values for a reset orbitCamDistance = orbitCamDistanceOrg; panVector = Vector3.zero; navState = NAVSTATE.IDLE; lastNavState = NAVSTATE.IDLE; lastTouchCount = 0; lastClickTime = -10e37f; inputState = INPUTSTATE.RELEASED; }
// ============================================================================== // HandleInput // ============================================================================== private void HandleInput() { // --- // --- Get the current state of the input device ---------------------- // --- inputState = INPUTSTATE.RELEASED; if (inputPressedLast == true) { inputState = INPUTSTATE.KEEPPRESSING; } if (inputPressed == true && inputPressedLast == false) { inputPressedLast = true; inputState = INPUTSTATE.JUSTPRESSED; } // --- device button Released ---- if (inputPressed == false && inputPressedLast == true) { inputPressedLast = false; inputState = INPUTSTATE.JUSTRELEASED; } navState = NAVSTATE.IDLE; // --- handle multitouch device ---- if (isMultitouchDevice == true) { if (steeringMode == STEERINGMODE.TWO_FINGERS_ZOOM_THREE_FINGERS_PAN) { if (touchCount <= 1) { navState = NAVSTATE.ROTATE; } else if (touchCount == 2) { navState = NAVSTATE.ZOOM; } else if (touchCount > 2) { navState = NAVSTATE.PAN; } } else { if (touchCount <= 1) { navState = NAVSTATE.ROTATE; } else if (touchCount >= 2) { navState = NAVSTATE.ZOOM_AND_PAN; } } } // --- handle singletouch device ---- else { if (touchCount <= 1) { if (Input.GetMouseButton(1) == false) // right mouse button clicked? { navState = NAVSTATE.ROTATE; } else { navState = NAVSTATE.PAN; } } if (mouseWheelValue != 0.0f) { navState = NAVSTATE.ZOOM; } } // --- handle the device state ---------------------------------------- // --- // --- ROTATE --- // --- if (navState == NAVSTATE.ROTATE) { if (inputState == INPUTSTATE.JUSTPRESSED) { SetRotationStart(mousePosition.x, mousePosition.y); } else if (inputState == INPUTSTATE.KEEPPRESSING) { SetRotationCurrent(mousePosition.x, mousePosition.y); } else if (inputState == INPUTSTATE.JUSTRELEASED) { RotationEnded(); } } // --- // --- ZOOM --- // --- else if (navState == NAVSTATE.ZOOM) { RotationStop(); if (isMultitouchDevice == true) { if (inputState == INPUTSTATE.JUSTPRESSED || lastNavState != navState) { zoomStartorbitCamDistance = orbitCamDistance; zoomStartTouchDistance = (touchPositions[1] - touchPositions[0]).magnitude; // distance between the 2 touch points } else if (inputState == INPUTSTATE.KEEPPRESSING) { float zoomEndTouchDistance = (touchPositions[1] - touchPositions[0]).magnitude; // distance between the 2 touch points float scale = (float)zoomStartTouchDistance / (float)zoomEndTouchDistance; // scale factor orbitCamDistance = zoomStartorbitCamDistance * scale; } } else { orbitCamDistance -= mouseWheelValue * mouseZoomSensitivity; } orbitCamDistance = Mathf.Clamp(orbitCamDistance, orbitCamDistanceMin, orbitCamDistanceMax); } // --- // --- PAN --- // --- else if (navState == NAVSTATE.PAN) { RotationStop(); if (inputState == INPUTSTATE.JUSTPRESSED || lastNavState != navState) { if (isMultitouchDevice == true) { panStartTouch = GetCenterOfTouches(); } else { panStartTouch = mousePosition; } } else if (inputState == INPUTSTATE.KEEPPRESSING) { if (isMultitouchDevice == true) { panEndTouch = GetCenterOfTouches(); } else { panEndTouch = mousePosition; } panDiffTouch = panEndTouch - panStartTouch; panStartTouch = panEndTouch; panVector += orbitCamera.transform.right * (-panDiffTouch.x * panningSensitivity); panVector += orbitCamera.transform.up * (panDiffTouch.y * panningSensitivity); } } // --- // --- ZOOM & PAN (multitouch only!) --- // --- else if (navState == NAVSTATE.ZOOM_AND_PAN) { RotationStop(); if (inputState == INPUTSTATE.JUSTPRESSED || lastNavState != navState) { zoomStartorbitCamDistance = orbitCamDistance; zoomStartTouchDistance = (touchPositions[1] - touchPositions[0]).magnitude; // distance between the 2 touch points panStartTouch = GetCenterOfTouches(); } else if (inputState == INPUTSTATE.KEEPPRESSING) { float zoomEndTouchDistance = (touchPositions[1] - touchPositions[0]).magnitude; // distance between the 2 touch points float scale = (float)zoomStartTouchDistance / (float)zoomEndTouchDistance; // scale factor orbitCamDistance = zoomStartorbitCamDistance * scale; panEndTouch = GetCenterOfTouches(); panDiffTouch = panEndTouch - panStartTouch; panStartTouch = panEndTouch; panVector += orbitCamera.transform.right * (-panDiffTouch.x * panningSensitivity); panVector += orbitCamera.transform.up * (panDiffTouch.y * panningSensitivity); } orbitCamDistance = Mathf.Clamp(orbitCamDistance, orbitCamDistanceMin, orbitCamDistanceMax); } // --- // --- DOUBLE CLICK --- // --- // register double clicks if (lastTouchCount == 1 && inputState == INPUTSTATE.JUSTRELEASED) { if (systemTime <= lastClickTime + doubleClickDiffTime && // double click within defined time? (mousePosition - lastDoublClickClickPos).magnitude < (Screen.height / 20.0f)) // and the click distance is within a reasonable distance { Reset(); return; // no further processing } lastClickTime = systemTime; lastDoublClickClickPos = mousePosition; } // remember this as lastState lastNavState = navState; lastTouchCount = touchCount; }
// Update is called once per frame void FixedUpdate() { if (_myRb.velocity != Vector2.zero) { Vector2 frictionDir = _myRb.velocity.normalized; float frictionToApply = friction * Time.fixedDeltaTime; if (_myRb.velocity.sqrMagnitude <= frictionToApply * frictionToApply) { _myRb.velocity = Vector2.zero; } else { _myRb.velocity -= frictionToApply * frictionDir; } } //Dicte quand on passe d'un enum à l'autre #region Change Enum if (!_isInputDisabled) { if (_input != Vector2.zero && !_isTooMuchPowerGathered) { _playerInput = INPUTSTATE.GivingInput; _timerDeadPoint = 0; inputXSign = _inputVariableToStoreDirection.x; inputYSign = _inputVariableToStoreDirection.y; } else if (_playerInput == INPUTSTATE.GivingInput && (_input.x == 0 || _input.y == 0) && _timerDeadPoint < 0.1) { _playerInput = INPUTSTATE.Released; } else if ((_playerInput == INPUTSTATE.EasingInput && _timerDeadPoint >= 0.1)) { _playerInput = INPUTSTATE.Released; } if (_input.x == 0 || _input.y == 0) { //permet de pouvoir reslinger après avoir fait un sling max if (_isTooMuchPowerGathered) { _isTooMuchPowerGathered = false; } } } #endregion #region Actions depending on INPUTSTATE if (_playerInput == INPUTSTATE.GivingInput) { if (tooMuchPowerTimer < tooMuchPowerTimerMax) { _animator.SetBool("IsSlingshoting", true); } _animator.SetBool("isHit", false); _animator.SetBool("isHitting", false); _playerScoreImage.sprite = _playerScoreImageSprites[2]; _angle = Mathf.Atan2(_input.x, _input.y) * Mathf.Rad2Deg; transform.rotation = Quaternion.Euler(0, 0, _angle); powerJaugeParent.gameObject.SetActive(true); powerJauge.fillAmount = _timerPower / powerMax; _inputVariableToStoreDirection = _input; Vector2 frictionDir = _myRb.velocity.normalized; if (_myRb.velocity.sqrMagnitude >= (frictionWhenCharging * Time.fixedDeltaTime) * (frictionWhenCharging * Time.fixedDeltaTime)) { _myRb.velocity -= (frictionWhenCharging * Time.fixedDeltaTime) * frictionDir; } else { _myRb.velocity = Vector2.zero; } _timerPower += Time.fixedDeltaTime; if (_timerPower >= (powerMax + tooMuchPowerTimerMax) * 0.33f) { sweatParticles.SetActive(true); } //check si ça fait pas de probs plus tard if (_timerPower >= powerMax) { _timerPower = powerMax; tooMuchPowerTimer += Time.fixedDeltaTime; if (tooMuchPowerTimer > tooMuchPowerTimerMax) { _animator.SetBool("IsSlingshoting", false); _isTooMuchPowerGathered = true; powerJaugeParent.gameObject.SetActive(false); sweatParticles.SetActive(false); _myRb.velocity = new Vector2(_inputVariableToStoreDirection.x, -_inputVariableToStoreDirection.y).normalized *(-_timerPower * speed); _inputVariableToStoreDirection = Vector2.zero; _lastFramePower = _timerPower; _timerPower = 0; _timerDeadPoint = 0; vibrationTreshold = 0.2f; powerJauge.fillAmount = 0; if (gameObject.tag == "Player1") { _playerManagerScript.player[0].StopVibration(); } else if (gameObject.tag == "Player2") { _playerManagerScript.player[1].StopVibration(); } else if (gameObject.tag == "Player3") { _playerManagerScript.player[2].StopVibration(); } else if (gameObject.tag == "Player4") { _playerManagerScript.player[3].StopVibration(); } _playerScoreImage.sprite = _playerScoreImageSprites[2]; _playerInput = INPUTSTATE.None; tooMuchPowerTimer = 0; } } } else if (_playerInput == INPUTSTATE.EasingInput) { _timerDeadPoint += Time.fixedDeltaTime; } else if (_playerInput == INPUTSTATE.Released) { if (_touchedByPlayer) { _touchedByPlayer = false; } sweatParticles.SetActive(false); _animator.SetBool("IsSlingshoting", false); _playerScoreImage.sprite = _playerScoreImageSprites[2]; powerJaugeParent.gameObject.SetActive(false); _myRb.velocity = new Vector2(inputXSign, -inputYSign).normalized *(-_timerPower * speed); _inputVariableToStoreDirection = Vector2.zero; _lastFramePower = _timerPower; _timerPower = 0; _timerDeadPoint = 0; _playerInput = INPUTSTATE.None; } else if (_playerInput == INPUTSTATE.None) { vibrationTreshold = 0.2f; powerJauge.fillAmount = 0; if (gameObject.tag == "Player1") { _playerManagerScript.player[0].StopVibration(); } else if (gameObject.tag == "Player2") { _playerManagerScript.player[1].StopVibration(); } else if (gameObject.tag == "Player3") { _playerManagerScript.player[2].StopVibration(); } else if (gameObject.tag == "Player4") { _playerManagerScript.player[3].StopVibration(); } } #endregion //Fait apparaitre une trail si la vitesse atteind le seuil des murs _myVelocityFloat = _myRb.velocity.sqrMagnitude; _velocityConvertedToRatio = (_myVelocityFloat / _velocityMax); if (_velocityConvertedToRatio > trailApparitionTreshold) { _playerTrail.enabled = true; _needTrail = true; } if (_needTrail) { _trailTimer += Time.deltaTime; } if (_trailTimer >= trailDuration) { _needTrail = false; } if (!_needTrail) { _playerTrail.enabled = false; _trailTimer = 0; } _lastFrameVelocity = _myRb.velocity; //if(_myRb.velocity.sqrMagnitude > velocityClamp) //{ // float factor = _myRb.velocity.sqrMagnitude / velocityClamp; // if (_myRb.velocity.sqrMagnitude > _velocityMax) // { // _myRb.velocity -= _myRb.velocity.normalized * factor; // } //} //if(playerScaleHitWall) //{ // float yScale = ((squash / -2) + originalScale); // float xScale = (squash + originalScale); // //directionTraveling = ((Mathf.Atan2(rb.velocity.y, rb.velocity.x)) - (90 * (Mathf.PI / 180))); // //Quaternion directionTraveling = Quaternion.LookRotation(_myRb.velocity); // //playerSprite.transform.rotation = directionTraveling; // playerSprite.transform.localScale = new Vector3(xScale, yScale, 1); // playerScaleHitWall = false; //} //else //{ // playerSprite.transform.localScale = new Vector3(originalScale, originalScale, originalScale); //} }
// Update is called once per frame void FixedUpdate() { if (_myRb.velocity != Vector2.zero) { Vector2 frictionDir = _myRb.velocity.normalized; float frictionToApply = friction * Time.fixedDeltaTime; if (_myRb.velocity.sqrMagnitude <= frictionToApply * frictionToApply) { _myRb.velocity = Vector2.zero; } else { _myRb.velocity -= frictionToApply * frictionDir; } } //Dicte quand on passe d'un enum à l'autre #region Change Enum if (_input != Vector2.zero) { _playerInput = INPUTSTATE.GivingInput; _timerDeadPoint = 0; } else if (_playerInput == INPUTSTATE.GivingInput && (_input.x == 0 || _input.y == 0) && _timerDeadPoint < 0.1) { _playerInput = INPUTSTATE.EasingInput; } else if ((_playerInput == INPUTSTATE.EasingInput && _timerDeadPoint >= 0.1)) { _playerInput = INPUTSTATE.Released; } #endregion #region Actions depending on INPUTSTATE if (_playerInput == INPUTSTATE.GivingInput) { _animator.SetBool("IsSlingshoting", true); _angle = Mathf.Atan2(_input.x, _input.y) * Mathf.Rad2Deg; transform.rotation = Quaternion.Euler(0, 0, _angle); powerJaugeParent.gameObject.SetActive(true); powerJauge.fillAmount = _timerPower / powerMax; _inputVariableToStoreDirection = _input; _timerPower += Time.fixedDeltaTime; //check si ça fait pas de probs plus tard if (_timerPower >= powerMax) { _timerPower = powerMax; tooMuchPowerTimer += Time.fixedDeltaTime; if (tooMuchPowerTimer > tooMuchPowerTimerMax) { tooMuchPowerTimer = 0; _isTooMuchPowerGathered = true; powerJaugeParent.gameObject.SetActive(false); _myRb.velocity = new Vector2(_inputVariableToStoreDirection.x, -_inputVariableToStoreDirection.y).normalized *(-_timerPower * speed); _inputVariableToStoreDirection = Vector2.zero; _lastFramePower = _timerPower; _timerPower = 0; _timerDeadPoint = 0; vibrationTreshold = 0.2f; powerJauge.fillAmount = 0; if (gameObject.tag == "Player1") { _playerManagerScript._player.StopVibration(); } _playerInput = INPUTSTATE.None; } } } else if (_playerInput == INPUTSTATE.EasingInput) { _timerDeadPoint += Time.fixedDeltaTime; } else if (_playerInput == INPUTSTATE.Released) { _animator.SetBool("IsSlingshoting", false); powerJaugeParent.gameObject.SetActive(false); _myRb.velocity = new Vector2(_inputVariableToStoreDirection.x, -_inputVariableToStoreDirection.y).normalized *(-_timerPower * speed); _inputVariableToStoreDirection = Vector2.zero; _lastFramePower = _timerPower; _timerPower = 0; _timerDeadPoint = 0; _playerInput = INPUTSTATE.None; } else if (_playerInput == INPUTSTATE.None) { vibrationTreshold = 0.2f; powerJauge.fillAmount = 0; if (gameObject.tag == "Player1") { _playerManagerScript._player.StopVibration(); } } #endregion //Fait apparaitre une trail si la vitesse atteind le seuil des murs (on changera après le 0.8 par une variable) _myVelocity = _myRb.velocity.sqrMagnitude; _velocityConvertedToRatio = (_myVelocity / _velocityMax); if (_velocityConvertedToRatio > 0.8) { _trailRenderer.enabled = true; } else { _trailRenderer.enabled = false; } _lastFrameVelocity = _myRb.velocity; }