void SetGameStateBasedOnCharState() { if (!GameStateUtils.IsPlaying(gameState)) { return; } if (state.Value == CharacterState.Idle) { gameState.Value = GameStateConstants.PLAYING_RUNNING; } else if (CharacterStateUtils.IsJumping(state.Value)) { gameState.Value = GameStateConstants.PLAYING_JUMPING; } else if (CharacterStateUtils.IsFalling(state.Value)) { gameState.Value = GameStateConstants.PLAYING_FALLING; } else if (CharacterStateUtils.HasLanded(state.Value)) { gameState.Value = GameStateConstants.PLAYING_LANDED; } else { gameState.Value = GameStateConstants.PLAYING; } }
void OnCollisionEnter2D(Collision2D collision) { if (!GameStateUtils.IsPlaying(gameState)) { return; } if ((collision.gameObject.tag == TagConstants.FLOOR || collision.gameObject.tag == TagConstants.MATTRESS) && state.Value == CharacterState.Falling) { smoke.Play(); Camera.main.GetComponent <CameraShake>().Shake(0.3f); // Prevent character from moving sideways when landing actionsNextFixedUpdate.Add(StopXVelocity); if (collision.gameObject.tag == TagConstants.MATTRESS) { SoundManager.instance.PlayFX(LANDING); scoreManager.DidLandOnMattress(); state.Value = CharacterState.Landed; StartCoroutine(LandedOnMattress()); } else if (collision.gameObject.tag == TagConstants.FLOOR) { Die(); } } }
void Update() { if (!GameStateUtils.IsPlaying(gameState)) { return; } animator.SetInteger(STATE, (int)state.Value); Shader.SetGlobalFloat(_Y_POSITION, this.transform.position.y * 0.03f); rb.gravityScale = rb.velocity.y < -1f ? 3.5f : 1f; // For animation if (state.Value == CharacterState.Idle && (Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.RightArrow))) { state.Value = CharacterState.Running; } if (state.Value == CharacterState.CanJump && Input.GetKeyDown(KeyCode.Space) && jumpingTriggerTransform != null) { SoundManager.instance.PlayFX(JUMP); state.Value = CharacterState.PreJumping; actionsNextFixedUpdate.Add(DoPreJump); StartCoroutine(DelayAndJump()); percentJumpAreaReached = (transform.position.x - (jumpingTriggerTransform.position.x - jumpingTriggerTransform.localScale.x / 2)) / jumpingTriggerTransform.localScale.x; } else if (state.Value == CharacterState.Running || state.Value == CharacterState.CanJump) { if ((lastKey == KeyCode.None || lastKey == KeyCode.RightArrow) && Input.GetKeyDown(KeyCode.LeftArrow)) { SoundManager.instance.PlayFX(RUNNING); actionsNextFixedUpdate.Add(AddRunSpeed); lastKey = KeyCode.LeftArrow; smoke.Play(); } else if ((lastKey == KeyCode.None || lastKey == KeyCode.LeftArrow) && Input.GetKeyDown(KeyCode.RightArrow)) { SoundManager.instance.PlayFX(RUNNING); actionsNextFixedUpdate.Add(AddRunSpeed); lastKey = KeyCode.RightArrow; smoke.Play(); } } else if (state.Value == CharacterState.Falling) { if (Input.GetKey(KeyCode.LeftArrow)) { actionsNextFixedUpdate.Add(AddTorqueNForceFallingLeft); } else if (Input.GetKey(KeyCode.RightArrow)) { actionsNextFixedUpdate.Add(AddTorqueNForceFallingRight); } CountRotation(); scoreManager.AddWhenFalling(Time.deltaTime); } }
void OnTriggerExit2D(Collider2D collider) { if (!GameStateUtils.IsPlaying(gameState)) { return; } if (collider.tag == TagConstants.JUMPING_TRIGGER) { jumpingTriggerTransform = null; if (state.Value == CharacterState.CanJump) { Die(); } } }
void FixedUpdate() { if (!GameStateUtils.IsPlaying(gameState)) { return; } rb.gravityScale = rb.velocity.y < -1f ? 3.5f : 1f; for (int i = 0; i < actionsNextFixedUpdate.Count; ++i) { actionsNextFixedUpdate[i](); } actionsNextFixedUpdate.Clear(); ClampAngularVelocity(); }
void OnTriggerEnter2D(Collider2D collider) { if (!GameStateUtils.IsPlaying(gameState)) { return; } if (collider.tag == TagConstants.JUMPING_TRIGGER) { jumpingTriggerTransform = collider.transform; state.Value = CharacterState.CanJump; } else if ((state.Value != CharacterState.Running && (collider.tag == TagConstants.POLE && collider.transform.parent == null) || collider.tag == TagConstants.HINDER)) { Die(); } }