private void Update() { if (m_PlayerState == State.Intro || m_PlayerState == State.ControllingEnemy) { return; } State nextState = State.None; Vector2 moveVecThisFrame = Vector2.zero; bool bgInputDown = m_BackgroundTap.GetHasInput(); bool bgInputUp = m_BackgroundTap.GetHasInputUp(); if (nextState == State.Flight) { if (Input.GetKey(KeyCode.UpArrow)) { moveVecThisFrame = new Vector2(0f, 6f * Time.deltaTime); } if (Input.GetKey(KeyCode.DownArrow)) { moveVecThisFrame = new Vector2(0f, -6f * Time.deltaTime); } } //Detect to go into jump boost state if (bgInputDown && m_PlayerState != State.Damaged && m_PlayerState != State.OnHatch && m_PlayerState != State.AttachedToObject) { if (m_StartedBoostPath && m_PlayerSpear.CanClamp) { HandleClampedToObjectByRope(); } else { float currentTime = Time.realtimeSinceStartup; if (!m_PressedForBoost) { m_PressedForBoost = true; m_PressedForBoostStartTime = currentTime; } if (currentTime - m_PressedForBoostStartTime > .3f) { nextState = State.JumpBoost; ResetStateVars(nextState); if (m_PlayerState != nextState) { InitJumpBoostState(); } Vector2 playerScreenPos = Camera.main.WorldToScreenPoint(transform.position); Vector2 arrowDirPos = Input.mousePosition; Vector2 lookAt = (arrowDirPos - playerScreenPos).normalized; float jumpBoostAngle = Mathf.Atan2(lookAt.y, lookAt.x) * Mathf.Rad2Deg; m_JumpBoostRightArrow.rotation = Quaternion.Euler(new Vector3(0f, 0f, jumpBoostAngle)); m_JumpBoostRightArrow.localPosition = m_JumpBoostRightArrow.transform.right * .3f; float timeToMaxBoost = 1f; float timeInJumpBoost = Mathf.Min(Time.realtimeSinceStartup - m_JumpBoostStartTime, timeToMaxBoost); float maxBoostAdditiveArrowScale = 7f; m_JumpBoostRightArrow.localScale = new Vector3(maxBoostAdditiveArrowScale, 4f, 1f); m_DisplayRoot.localScale = new Vector3(Mathf.Abs(m_DisplayRoot.localScale.x) * (lookAt.x < 0f ? -1f : 1f), m_DisplayRoot.localScale.y, m_DisplayRoot.localScale.z); } } } //On mouse up, see if we've been jump boost state, if not, see if there was no other state set, that //would mean we can do our go-to. if (bgInputUp && m_PlayerState != State.Damaged && m_PlayerState != State.OnHatch && m_PlayerState != State.AttachedToObject) { m_PressedForBoost = false; if (m_PlayerState == State.JumpBoost) { if (!m_StartedBoostPath) { m_StartedBoostPath = true; m_JumpBoostRightArrow.gameObject.SetActive(false); m_PlayerSpear.Clear(); m_PlayerSpear.FireIntoDirection(transform.position, m_JumpBoostRightArrow.right, () => { ResetStateVars(State.Idle); InitIdleState(); }); } } else if (nextState == State.None) { nextState = State.GoTo; ResetStateVars(nextState); InitGoToState(); } } if (m_PlayerState == State.OnHatch || m_PlayerState == State.AttachedToObject) { if (m_PlayerState != State.AttachedToObject) { bool hijackBtnPressed = m_HijackButton.IsHeldDown; if (hijackBtnPressed) { if (!m_IsOpening) { m_IsOpening = true; m_Animations.Play("HijackOpen"); OnStartedOpeningInHijack?.Invoke(); } } else if (m_IsOpening) { m_IsOpening = false; m_Animations.Play("Idle"); OnStoppedOpeningInHijack?.Invoke(); } } if (m_PlayerSpear.CurrentState == PlayerSpear.State.ClosedToClamp) { if (!m_IsSwingingOnClamp) { //Spear being in this state means we can swipe on the screen to clamp swing if (bgInputDown && !m_InputDownForClampSwing) { m_InputDownForClampSwing = true; m_ClampSwingScreenDownPos = Input.mousePosition; m_ClampSwingDownStartTime = Time.realtimeSinceStartup; } else if (bgInputUp && m_InputDownForClampSwing) { m_InputDownForClampSwing = false; float clampSwipeTime = Time.realtimeSinceStartup - m_ClampSwingDownStartTime; if (clampSwipeTime <= kMaxClampSwingTimeWindow) { //Reset spear to its idle state before doing any swing out behavior m_PlayerSpear.ResetToIdle(); m_PlayerSpear.SetClosedToClampState(); //We'll say that this was quick enough to be considered a swipe Vector3 worldSwipeStartPos = Camera.main.ScreenToWorldPoint(m_ClampSwingScreenDownPos); Vector3 worldSwipeEndPos = Camera.main.ScreenToWorldPoint(Input.mousePosition); //Get swipe direction and distance Vector2 swipeDir = (worldSwipeEndPos - worldSwipeStartPos).normalized; float swipeDist = swipeDir.magnitude; m_DisplayRoot.localScale = new Vector3(Mathf.Abs(m_DisplayRoot.localScale.x) * (swipeDir.x > 0 ? -1 : 1), m_DisplayRoot.localScale.y, m_DisplayRoot.localScale.z); m_ClampSwingDir = swipeDir; m_ClampSwingVelocity = swipeDir * 30f; m_ClampSwingAcceleration = m_ClampSwingVelocity * 4f; //We now use these data points to get the player spear to do its clamp swing behavior m_IsSwingingOnClamp = true; m_RopeSwingOutEndWorldLocalPos = transform.localPosition; m_RopeSwingOutParent = transform.parent; Vector3 swingInDestPos = transform.localPosition; m_SwingOutDoneCallback = () => { Vector3 localMoveDest = swingInDestPos; m_TopOfSwingDelay = DOTween.Sequence(); m_TopOfSwingDelay.AppendInterval(.3f); m_TopOfSwingDelay.Append(DOTween.To(GetCurrentPosFromRopeSwingIn, HandleClampedRopeSwingInVelocity, localMoveDest, 1f)); m_TopOfSwingDelay.AppendCallback(() => { m_TopOfSwingDelay = null; m_IsSwingingOnClamp = false; }); }; } } } else if (bgInputUp && m_TopOfSwingDelay != null) { TugOffRopeClamp(); } } } //Detect to go into idle state if (nextState == State.None && m_PlayerState != State.GoTo && m_PlayerState != State.JumpBoost && m_PlayerState != State.Damaged && m_PlayerState != State.OnHatch && m_PlayerState != State.AttachedToObject) { nextState = State.Idle; ResetStateVars(nextState); if (m_PlayerState != nextState) { InitIdleState(); } } //if(m_StartedBoostPath && m_PlayerState == State.JumpBoost) //HandleBoostVelocity(ref moveVecThisFrame); if (m_PlayerState == State.Damaged) { HandleDamagedVelocity(ref moveVecThisFrame); } //if(m_PlayerState != State.OnHatch && m_PlayerState != State.AttachedToObject) HandleMoveStep(moveVecThisFrame); if ((m_PlayerState == State.OnHatch || m_PlayerState == State.AttachedToObject) && m_IsSwingingOnClamp) { HandleClampedRopeSwingOutVelocity(); } //Check to see if player is close to any enemies that are controlled. If yes, //we show the enter/leave button if (m_PlayerState != State.ControllingEnemy && m_PlayerState != State.OnHatch) { bool inRangeOfAny = false; if (m_CurrentControlledEnemy == null) { foreach (Enemy e in m_OwnedEnemies) { float dist = (transform.position - e.transform.position).magnitude; inRangeOfAny = (dist <= 5f); if (inRangeOfAny) { break; } } } m_EnterOrLeaveShipBtn.gameObject.SetActive(inRangeOfAny); } else if (m_PlayerState == State.ControllingEnemy && !m_EnterOrLeaveShipBtn.gameObject.activeSelf) { m_EnterOrLeaveShipBtn.gameObject.SetActive(true); } float bgWorldStep = m_BackgroundScroller.GetMoveStep() * .05f; //m_DistTraveledFromStart += new Vector2(bgWorldStep, 0f); m_WorldRoot.transform.position += new Vector3(-bgWorldStep, 0f, 0f); }
public void Update() { HandleFallMovement(); if (m_BGScroller != null) { m_BGScroller.UpdateFromMoveStep(); } if (m_FallingCamera != null) { m_FallingCamera.TryToFollow(); } if (m_ActiveSpearThrow) { if (m_BackgroundTap.GetHasInput() && m_Player.Spear.CanClamp) { m_Player.ChangeToState(m_Player.AttachedToObjectState); } return; } if (!m_ActiveSpearThrow && m_BackgroundTap.GetHasInputUp()) { float timeInState = Time.realtimeSinceStartup - m_StateStartTime; if (timeInState > .25f) { //This means we held long enough in a direction to say: "yeah, player was throwing spear in direction" m_ActiveSpearThrow = true; m_Player.Spear.Clear(); m_Player.Spear.FireIntoDirection(m_Player.transform.position, m_JumpBoostRightArrow.right, () => m_Player.ChangeToState(m_Player.IdleState)); m_JumpBoostRightArrow.gameObject.SetActive(false); } else { m_Player.ChangeToState(m_Player.IdleState); } return; } //This means player is still holding down, so we update throw direction here: if (!m_JumpBoostRightArrow.gameObject.activeSelf) { m_JumpBoostRightArrow.gameObject.SetActive(true); } Vector2 playerScreenPos = Camera.main.WorldToScreenPoint(m_Player.transform.position); Vector2 arrowDirPos = Input.mousePosition; Vector2 lookAt = (arrowDirPos - playerScreenPos).normalized; float jumpBoostAngle = Mathf.Atan2(lookAt.y, lookAt.x) * Mathf.Rad2Deg; m_JumpBoostRightArrow.rotation = Quaternion.Euler(new Vector3(0f, 0f, jumpBoostAngle)); m_JumpBoostRightArrow.localPosition = m_JumpBoostRightArrow.transform.right * .3f; m_DisplayRoot.localScale = new Vector3(Mathf.Abs(m_DisplayRoot.localScale.x) * (lookAt.x < 0f ? -1f : 1f), m_DisplayRoot.localScale.y, m_DisplayRoot.localScale.z); }