Esempio n. 1
0
    private void Update()
    {
        if (rb2d.velocity.magnitude > speed / 2)
        {
            if (walkSoundTimer < 0)
            {
                if (!walkSound.isPlaying)
                {
                    walkSound.Play();
                }
            }
            else
            {
                walkSoundTimer -= Time.deltaTime;
            }
        }
        else
        {
            walkSound.Stop();
            walkSoundTimer = walkSoundDelay;
        }
        InputCommand inp = TakeInput();
        // Debug.LogFormat("State: {0}, Direction: {1}; Input: Moving: {2}, Direction: {3}", state, direction, inp.move, inp.direction);
        PlayerState newState     = state;
        Direction   oldDirection = direction;

        // Note: this if statement is a hack necessary due to the messiness of PlayerItemManager
        // If I find some extra time I'll make this better, but for now this script needs to
        // monitor if we pick up an item and use a frame to switch to the carrying state if so.
        if (pItem.currentItem != null && state != PlayerState.standCarrying &&
            state != PlayerState.moveCarrying)
        {
            pickupSound.Play();
            newState = PlayerState.standCarrying;
        }
        else if (state != PlayerState.dead && !alive)
        {
            newState = PlayerState.dead;
        }
        else
        {
            switch (state)
            {
            case PlayerState.standing:
                if (inp.attack)
                {
                    rb2d.velocity = Vector2.zero;
                    newState      = PlayerState.attacking;
                }
                else if (inp.interact)
                {
                    if (Grab())
                    {
                        newState = PlayerState.grabbing;
                    }
                    else
                    {
                        pItem.Pickup();
                    }
                }
                else if (inp.move)
                {
                    rb2d.velocity = GetVectorDirection(inp.direction) * speed;
                    direction     = inp.direction;
                    newState      = PlayerState.moving;
                }
                break;

            case PlayerState.moving:
                if (inp.attack)
                {
                    rb2d.velocity = Vector2.zero;
                    newState      = PlayerState.attacking;
                }
                else if (inp.interact)
                {
                    if (Grab())
                    {
                        rb2d.velocity = Vector2.zero;
                        newState      = PlayerState.grabbing;
                    }
                    else
                    {
                        pItem.Pickup();
                    }
                }
                else if (!inp.move)
                {
                    rb2d.velocity = Vector2.zero;
                    newState      = PlayerState.standing;
                }
                else
                {
                    rb2d.velocity = GetVectorDirection(inp.direction) * speed;
                    direction     = inp.direction;
                }
                break;

            case PlayerState.standCarrying:
                if (inp.interact)
                {
                    dropSound.Play();
                    pItem.Drop();
                    newState = PlayerState.standing;
                }
                else if (inp.move)
                {
                    rb2d.velocity = GetVectorDirection(inp.direction) * speed;
                    direction     = inp.direction;
                    newState      = PlayerState.moveCarrying;
                }
                break;

            case PlayerState.moveCarrying:
                if (inp.interact)
                {
                    dropSound.Play();
                    pItem.Drop();
                    rb2d.velocity = Vector2.zero;
                    newState      = PlayerState.standing;
                }
                else if (!inp.move)
                {
                    rb2d.velocity = Vector2.zero;
                    newState      = PlayerState.standCarrying;
                }
                else
                {
                    rb2d.velocity = GetVectorDirection(inp.direction) * speed;
                    direction     = inp.direction;
                }
                break;

            case PlayerState.attacking:
                // todo: improve feel of combat
                if (!timerSet)
                {
                    swingSound.Play();
                    nextStateTime = attackTime;
                    timerSet      = true;
                    pCombat.StartAttack(direction);
                }
                else
                {
                    nextStateTime -= Time.deltaTime;
                }
                if (nextStateTime < 0)
                {
                    timerSet = false;
                    pCombat.EndAttack();
                    newState = PlayerState.standing;
                }
                // should you be able to turn while attacking?
                // direction = inp.direction;
                break;

            case PlayerState.grabbing:
                if (inp.move &&
                    (GetVectorDirection(inp.direction).Equals(GetVectorDirection(direction)) ||
                     GetVectorDirection(inp.direction).Equals(-1 * GetVectorDirection(direction))))
                {
                    pullVector    = GetVectorDirection(inp.direction) * pushSpeed;
                    rb2d.velocity = pullVector;
                    grabbedTile.GetComponent <Rigidbody2D>().constraints = RigidbodyConstraints2D.FreezeRotation;
                    grabbedTile.GetComponent <Rigidbody2D>().velocity    = pullVector;
                    newState = PlayerState.pushing;
                }
                else if (inp.interact)
                {
                    grabbedTile = null;
                    newState    = PlayerState.standing;
                }
                break;

            case PlayerState.pushing:
                if (!timerSet)
                {
                    nextStateTime = pushTime; // todo: calculate based on grid distance
                    timerSet      = true;
                }
                else
                {
                    nextStateTime -= Time.deltaTime;
                }
                if (nextStateTime < 0)
                {
                    timerSet      = false;
                    rb2d.velocity = Vector2.zero;
                    grabbedTile.GetComponent <Rigidbody2D>().velocity    = Vector2.zero;
                    grabbedTile.GetComponent <Rigidbody2D>().constraints = RigidbodyConstraints2D.FreezeAll;
                    newState = PlayerState.grabbing;
                }
                break;

            case PlayerState.dead:
                rb2d.velocity = Vector2.zero;
                if (!timerSet)
                {
                    nextStateTime = deathTime;
                    timerSet      = true;
                }
                else
                {
                    nextStateTime -= Time.deltaTime;
                }
                rb2d.isKinematic = true;
                if (nextStateTime < 0)
                {
                    timerSet = false;
                    level.EndGame();
                }
                break;
            }
        }
        // todo fix animations
        if (direction != oldDirection || state != newState)
        {
            animator.Play(animations.GetName(newState, direction));
        }
        state = newState;
    }