コード例 #1
0
    public void onStateChange(PlayerStateController.playerStates newState)
    {
        if (newState == currentState)
        {
            return;
        }
        // Comprovar que no hi hagi condicions per abortar l'estat
        if (checkIfAbortOnStateCondition(newState))
        {
            return;
        }

        if (!checkForValidStatePair(newState))
        {
            return;
        }
        Debug.Log(newState + ", current: " + currentState);
        rb2D = GetComponent <Rigidbody2D>();

        switch (newState)
        {
        case PlayerStateController.playerStates.idle:
            playerAnimator.SetBool("Walking", false);
            break;

        case PlayerStateController.playerStates.left:
            playerAnimator.SetBool("Walking", true);
            break;

        case PlayerStateController.playerStates.right:
            playerAnimator.SetBool("Walking", true);
            break;

        case PlayerStateController.playerStates.jump:

            if (playerHasLanded)
            {
                if (right)
                {
                    if (playerJumpStrongY < 0)
                    {
                        playerJumpStrongY = playerJumpStrongY * -1f;
                    }
                    rb2D.AddForce(new Vector2(playerJumpStrongY, playerJumpStrongX));
                }
                else
                {
                    if (playerJumpStrongY < 0)
                    {
                        rb2D.AddForce(new Vector2(playerJumpStrongY, playerJumpStrongX));
                    }
                    else
                    {
                        playerJumpStrongY = playerJumpStrongY * -1f;
                        rb2D.AddForce(new Vector2(playerJumpStrongY, playerJumpStrongX));
                    }
                }
            }

            playerHasLanded = false;
            Debug.Log("landing = " + playerHasLanded);
            break;

        case PlayerStateController.playerStates.landing:
            // Debug.Log("Landed " + playerHasLanded);
            playerHasLanded = true;
            //  Debug.Log("Landed " + playerHasLanded);
            break;

        case PlayerStateController.playerStates.falling:
            break;

        case PlayerStateController.playerStates.kill:
            break;

        case PlayerStateController.playerStates.resurrect:

            transform.position = playerRespawnPoint.transform.position;
            transform.rotation = Quaternion.identity;
            rb2D.velocity      = Vector2.zero;

            break;

        case PlayerStateController.playerStates.firingWeapon:
            PlayerStateController.stateDelayTimer[(int)PlayerStateController.playerStates.firingWeapon] = Time.time + 0.25f;
            GameObject newBullet = (GameObject)Instantiate(bulletPrefab);
            newBullet.transform.position = bulletSpawnTransform.position;
            PlayerBulletController bullCon = newBullet.GetComponent <PlayerBulletController>();
            bullCon.playerObject = gameObject;
            bullCon.launchBullet();
            onStateChange(currentState);
            break;

        case PlayerStateController.playerStates.firingWeapon2:
            PlayerStateController.stateDelayTimer[(int)PlayerStateController.playerStates.firingWeapon2] = Time.time + 0.25f;
            GameObject newBullet2 = (GameObject)Instantiate(bulletPrefab2);
            newBullet2.transform.position = bulletSpawnTransform.position;
            PlayerBulletController2 bullCon2 = newBullet2.GetComponent <PlayerBulletController2>();
            bullCon2.playerObject = gameObject;
            bullCon2.launchBullet();
            onStateChange(currentState);
            break;
        }
        previousState = currentState;
        currentState  = newState;
    }
コード例 #2
0
    // onStateChange es crida sempre que canvia l'estat del player

    public void onStateChange(PlayerStateController.playerStates newState)
    {
        // Si l'estat actual i el nou són el mateix, no cal fer res
        if (newState == currentState)
        {
            return;
        }
        // Comprovar que no hi hagi condicions per abortar l'estat
        if (checkIfAbortOnStateCondition(newState))
        {
            return;
        }

// Comprovar que el pas de l'estat actual al nou estat està permès. Si no està, no es continua.

        if (!checkForValidStatePair(newState))
        {
            return;
        }

        // Realitzar les accions necessàries en cada cas per canviar l'estat.
        // De moment només es gestionen els estats idle, right i left
        switch (newState)
        {
        case PlayerStateController.playerStates.idle:
            playerAnimator.SetBool("Walking", false);
            break;

        case PlayerStateController.playerStates.left:
            playerAnimator.SetBool("Walking", true);
            break;

        case PlayerStateController.playerStates.right:
            playerAnimator.SetBool("Walking", true);
            break;

        case PlayerStateController.playerStates.jump:
            Debug.Log(newState);
            if (playerHasLanded)
            {
                // jumpDirection determina si el salt es a la dreta, esquerra o vertical
                float jumpDirection = 0.0f;
                if (currentState == PlayerStateController.playerStates.left)
                {
                    jumpDirection = -1.0f;
                }
                else if (currentState == PlayerStateController.playerStates.right)
                {
                    jumpDirection = 1.0f;
                }
                else
                {
                    jumpDirection = 0.0f;
                }
                // aplicar la força per fer el salt
                GetComponent <Rigidbody2D>().AddForce(new Vector2(jumpDirection * playerJumpForceHorizontal, playerJumpForceVertical));
                //indicar que el Player esta saltant en l'aire
                playerHasLanded = false;
            }

            break;

        case PlayerStateController.playerStates.landing:
            playerHasLanded = true;
            break;

        case PlayerStateController.playerStates.falling:
            break;

        case PlayerStateController.playerStates.kill:
            break;

        case PlayerStateController.playerStates.resurrect:
            //posicio: la de PlayerRespawnPoint
            transform.position = playerRespawnPoint.transform.position;
            transform.rotation = Quaternion.identity;             //rotacio: cap
            GetComponent <Rigidbody2D>().velocity = Vector2.zero; //velocitat lineal: zero
            break;

        case PlayerStateController.playerStates.firingWeapon:
            // Construir l'objecte bala a partir del Prefab
            GameObject newBullet = (GameObject)Instantiate(bulletPrefab);
            // Establir la posicio inicial de la bala creada
            //(la posicio de BulletSpawnTransform)
            newBullet.transform.position = bulletSpawnTransform.position;

            // Agafar el component PlayerBulletController de la bala
            //que s'ha creat
            // Establir temps a partir del qual es pot tornar a disparar
            PlayerStateController.stateDelayTimer[(int)PlayerStateController.playerStates.firingWeapon] = Time.time + 0.25f;
            PlayerBulletController bullCon = newBullet.GetComponent <PlayerBulletController>();
            // Assignar a l'atribut playerObject de l'script
            //PlayerBulletController el Player
            bullCon.playerObject = gameObject;
            // Invocar metode que dispara la bala
            bullCon.launchBullet();
            // Despres de disparar, tornar a l'estat previ.
            onStateChange(currentState);
            break;

        case PlayerStateController.playerStates.firingMortar:
            // Construir l'objecte bala a partir del Prefab
            GameObject newBullet2 = (GameObject)Instantiate(bulletPrefab2);
            // Establir la posicio inicial de la bala creada
            //(la posicio de BulletSpawnTransform)
            newBullet2.transform.position = bulletSpawnTransform.position;

            // Agafar el component PlayerBulletController de la bala
            //que s'ha creat
            // Establir temps a partir del qual es pot tornar a disparar
            PlayerStateController.stateDelayTimer[(int)PlayerStateController.playerStates.firingMortar] = Time.time + 0.25f;
            PlayerBulletController2 bullCon2 = newBullet2.GetComponent <PlayerBulletController2>();
            // Assignar a l'atribut playerObject de l'script
            //PlayerBulletController el Player
            bullCon2.playerObject = gameObject;
            // Invocar metode que dispara la bala
            bullCon2.launchBullet();
            // Despres de disparar, tornar a l'estat previ.
            onStateChange(currentState);
            break;
        }
        // Guardar estat actual com a estat previ
        previousState = currentState;
        // Assignar el nou estat com a estat actual del player
        currentState = newState;
    }