Exemplo n.º 1
0
    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.I))
        {
            brain.SetNextAvailableIncense();
            _timer.ChangeTimerBarSprite(brain.IncenseSprites[(int)brain.currIncense]);
        }

        if (Input.GetKeyDown(KeyCode.B) && debugMode)
        {
            infiniteBombs = !infiniteBombs;
        }

        if (_isInvincible)
        {
            _invincibleTimer += Time.deltaTime;
            if (_invincibleTimer > _invincibleTime)
            {
                _invincibleTimer = 0;
                _isInvincible    = false;
                _invincibilitySprite.SetActive(false);
            }
        }

        // Check to see if player has eliminated all key enemies.
        if (!shopping && _gameManager.GetNumOfKeyEnemiesAlive() <= 0)
        {
            if (_currentState != playerState.WINNING)
            {
                _animator.setAnimation(_victoryAnimation);
            }
            _currentState = playerState.WINNING;
            // Wait until the player is on the ground, all controls are blocked
            if (_controller.isGrounded)
            {
                if (_winTimer < _winTime)
                {
                    _winTimer += Time.deltaTime;
                }
                if (_winTimer >= _winTime)
                {
                    _gameManager.LevelFinished();
                    //_animator.setAnimation("ShibaFreezeFrame");
                }
                //_gameManager.LevelFinished();
            }
        }
        if (_currentState == playerState.DEAD)
        {
            _animator.setAnimation(_deadAnimation);
            if (_deathTimer < _deathTime)
            {
                _deathTimer += Time.deltaTime;
            }
            if (_deathTimer >= _deathTime)
            {
                _gameManager.GameOver();
            }
        }
        else
        {
            // The order of importance of player actions is as follows:
            //  1. Dashing (will interupt all other actions and override them)
            //  2. Attacking (can attack anytime other than when dashing)
            //  3. Jumping/Falling
            //  4. Walking
            //  Falling is checked first, then dashing, then attacking, then other
            Vector3 velocity = _controller.velocity;
            velocity.x = 0;
            // Keeps track of the amount of items the player has at this frame
            int BombTotal  = brain.playerItemCounts[TheBrain.ItemTypes.Bomb];
            int invicTotal = brain.playerItemCounts[TheBrain.ItemTypes.Invincible];

            if (!_canPlaceBomb)
            {
                _bombCooldownTimer += Time.deltaTime;
                if (_bombCooldownTimer >= _bombCooldownWaitTime)
                {
                    _canPlaceBomb = true;
                }
            }

            //private float _damageTimer = 0.2f;
            //private float _damageTime = 0f;
            // If the player is currently taking damage...
            if (_currentState == playerState.TAKINGDAMAGE)
            {
                if (!_isInvincible)
                {
                    _animator.setAnimation(_damageAnimation);
                }
                _damageTime            += Time.deltaTime;
                _playerCollider.enabled = false;
                if (_damageTime >= _damageTimer)
                {
                    _currentState           = playerState.FREE;
                    _damageTime             = 0;
                    _playerCollider.enabled = true;
                    _animator.setAnimation("ShibaIdle");
                }
                else
                {
                    velocity.y = 0;
                    velocity  -= _damageFallbackDirection * 500 * Time.deltaTime;
                    // Fall!
                    //velocity.y += gravity * Time.deltaTime;
                }
            }
            // If the player has left the ground...
            else if (!_controller.isGrounded)
            {
                _wasLanded = false;
                // ...or is falling.
                // Checking the y is important- if a player is on the rising
                //  arc of their jump they shouldn't be falling. Also if the player
                //  is aerial attacking do not switch the animation
                if (_prevY > gameObject.transform.position.y && !(_currentState == playerState.AIRATTACKING))
                {
                    _animator.setAnimation(_fallAnimation);
                }
            }
            // ...and just landed.
            if (!_wasLanded && _controller.isGrounded && _currentState != playerState.TAKINGDAMAGE)
            {
                _wasLanded    = true;
                _currentState = playerState.LANDING;
            }
            // If the player can't dash, incrememnt their cooldown timer.
            if (!_canDash)
            {
                // If they've done their time, allow the ability again
                if (dashCooldownTime + dashTime < _dashTimer)
                {
                    _canDash   = true;
                    _dashTimer = 0;
                }
                else
                {
                    _dashTimer += Time.deltaTime;
                }
            }
            // Dashing overrides all other movements (except for the player taking damage)
            if (((Input.GetKeyDown("k") && _canDash) || (_currentState == playerState.DASHING)) && _currentState != playerState.TAKINGDAMAGE && _currentState != playerState.WINNING)
            {
                // Player doesn't fall
                velocity.y = 0;
                _animator.setAnimation(_dashAnimation);
                // If the player has just begun dashing,
                //  allow them to attack.
                if (_currentState != playerState.DASHING)
                {
                    _currentState = playerState.DASHING;
                    _attackColliderController.setEnabled(true);
                    _source.PlayOneShot(_playerDash, _playerDashVolume);
                }
                // Move in the direction they are facing
                if (_isFacingRight)
                {
                    velocity.x = dashSpeed;
                }
                else
                {
                    velocity.x = dashSpeed * -1;
                }
                // Increment the timer for how long they can dash for.
                _dashTimer += Time.deltaTime;
                // Check to see if they are finished dashing.
                if (_dashTimer > dashTime)
                {
                    _currentState = playerState.FREE;
                    _canDash      = false;
                    _animator.setAnimation(_idleAnimation);
                }
            }

            // Only perform other checks if not dashing or attacking
            else if (_currentState != playerState.TAKINGDAMAGE)
            {
                if (_currentState == playerState.ATTACKING || _currentState == playerState.AIRATTACKING)
                {
                    // attackAnimationTimer is how long they are in an attack state
                    _attackTimer += Time.deltaTime;
                    // Check to see if they are finished
                    if (_attackTimer > attackAnimationTimer)
                    {
                        _currentState = playerState.FREE;
                        _attackTimer  = 0;
                    }
                    if (_currentState == playerState.ATTACKING)
                    {
                        return;
                    }
                }
                // If walking left
                if (Input.GetAxis("Horizontal") < 0 && _currentState != playerState.WINNING)
                {
                    velocity.x = walkSpeed * -1;
                    _animator.setFacing("Left");
                    if (_controller.isGrounded)
                    {
                        _animator.setAnimation(_walkAnimation);
                    }
                    _isFacingRight = false;
                }
                // If walking right
                else if (Input.GetAxis("Horizontal") > 0 && _currentState != playerState.WINNING)
                {
                    velocity.x = walkSpeed;
                    _animator.setFacing("Right");
                    if (_controller.isGrounded)
                    {
                        _animator.setAnimation(_walkAnimation);
                    }
                    _isFacingRight = true;
                }
                else
                {
                    // If the player is on the ground, they either just landed
                    //  or are in an idle state
                    if (_controller.isGrounded)
                    {
                        // Idling
                        if (_currentState != playerState.LANDING)
                        {
                            _animator.setAnimation(_idleAnimation);
                        }
                        // Landing
                        else
                        {
                            _animator.setAnimation(_landAnimation);
                            _landingTimer += Time.deltaTime;
                            if (_landingTimer > _landTime)
                            {
                                _landingTimer = 0;
                                _currentState = playerState.FREE;
                            }
                        }
                    }
                }
                // If the player tries to jump, only allow it if they are grounded.
                if (Input.GetAxis("Jump") > 0 && _controller.isGrounded && _currentState != playerState.WINNING)
                {
                    if (_controller.isGrounded)
                    {
                        _source.PlayOneShot(_playerJump, _playerJumpVolume);
                    }
                    velocity.y = Mathf.Sqrt(2f * jumpHeight * -gravity);
                    _animator.setAnimation(_jumpAnimation);
                }
                // The player has initiated an attack...
                if (Input.GetKeyDown("j") && _currentState != playerState.WINNING)
                {
                    Vector2 forceVect = _isFacingRight ? new Vector2(projectileSpeed, 0) : new Vector2(-1 * projectileSpeed, 0);
                    _source.PlayOneShot(_playerAttack, _playerAttackVolume);
                    // ...perform a ground attack.
                    if (_controller.isGrounded)
                    {
                        _animator.setAnimation(_attackAnimation);
                        _projectile.ShootProjectile(forceVect, _isFacingRight);
                        _currentState = playerState.ATTACKING;
                    }
                    // ...perform an aerial attack
                    else
                    {
                        _animator.setAnimation(_attackAirAnimation);
                        _projectile.ShootProjectile(forceVect, _isFacingRight);
                        _currentState = playerState.AIRATTACKING;
                    }
                    _attackColliderController.setEnabled(true);
                }
                if (Input.GetKeyDown("l") && _bombsPlaced < 2 && (BombTotal > 0 || infiniteBombs) && _currentState != playerState.WINNING)
                {
                    if (_canPlaceBomb)
                    {
                        _canPlaceBomb = false;
                        GameObject bomb = Instantiate(_Bomb, transform.position, Quaternion.identity) as GameObject;
                        _bombsPlaced++;
                        bomb.GetComponent <Rigidbody2D>().velocity = transform.TransformDirection(transform.forward);//.AddForce(transform.forward * _bombThrust);
                        if (!infiniteBombs)
                        {
                            brain.playerItemCounts[TheBrain.ItemTypes.Bomb] = CalcNewItemCount(BombTotal);
                        }

                        //Debug.Log("Removed Bomb Ability : " + brain.playerItemCounts[TheBrain.ItemTypes.Bomb]);
                    }
                }
                if (Input.GetKeyDown(";") && _currentState != playerState.WINNING && invicTotal > 0)
                {
                    if (!_isInvincible)
                    {
                        _isInvincible = true;
                        _invincibilitySprite.SetActive(true);

                        brain.playerItemCounts[TheBrain.ItemTypes.Invincible] = CalcNewItemCount(invicTotal);
                    }
                }
                // Fall!
                velocity.y += gravity * Time.deltaTime;
            }
            // Save the y, so on the next frame we can check if the player is falling.
            _prevY = gameObject.transform.position.y;
            // Perform the move.
            _controller.move(velocity * Time.deltaTime);
        }
    }