コード例 #1
0
    // Handle on trigger event
    private void OnTriggerEnter2D(Collider2D collision)
    {
        // check if Pacman touches a portal
        if (collision.gameObject.tag == "Waypoint")
        {
            Waypoint waypoint = collision.gameObject.GetComponent <Waypoint>();
            // teleport to target portal
            if (waypoint.isPortal) // if pacman touches a portal, change position to destination portal
            {
                Vector2 targetPortalPosition = waypoint.targetPortal.GetComponent <Transform>().position;
                transform.position = targetPortalPosition;
            }
        }

        // consume balls when touch and play sound
        if (collision.gameObject.tag == "SmallBall")
        {
            if (!chompSound.isPlaying)
            {
                chompSound.Play();
            }
            gameData.ConsumeSmallBall(isPlayer1);
            Destroy(collision.gameObject);
        }

        // if pacman consume a big ball, all ghost will change into frightened mode
        if (collision.gameObject.tag == "BigBall")
        {
            GameObject[] ghosts = GameObject.FindGameObjectsWithTag("Ghost");
            foreach (GameObject ghost in ghosts)
            {
                ghost.GetComponent <Ghost>().BecomeScared();
            }
            gameData.ConsumeBigBall(isPlayer1);
            Destroy(collision.gameObject);
        }

        // add points when consuming Fruits
        if (collision.gameObject.tag == "Fruit")
        {
            if (!eatFruitSound.isPlaying)
            {
                eatFruitSound.Play();
            }
            Fruit fruit = (Fruit)collision.gameObject.GetComponent <Fruit>();
            gameData.ConsumeFruit(fruit, isPlayer1);
        }

        // touch ghost
        if (collision.gameObject.tag == "Ghost")
        {
            Ghost ghost = collision.gameObject.GetComponent <Ghost>();
            // if the ghost is frightened, consume it to gain points and play sound
            if (ghost.mode == Ghost.GhostMode.Frightened)
            {
                if (!eatGhostSound.isPlaying)
                {
                    eatGhostSound.Play();
                }
                gameData.ConsumeGhost(isPlayer1);
                ghost.BecomeScatter();
            }
            else if (ghost.mode != Ghost.GhostMode.Scatter)  // if the ghost is scattered, nothing happens, else pacman dies
            {
                if (!deathSound.isPlaying)
                {
                    deathSound.Play();
                }
                if (collisionParticle.gameObject.activeSelf == false)
                {
                    collisionParticle.gameObject.SetActive(true);
                }
                // play particle effect when pacman die
                collisionParticle.transform.position = gameObject.transform.position;
                collisionParticle.Play();
                gameObject.SetActive(false);
                gameData.isOver = true;
                // define winner in Battle mode if 1 player dies
                if (gameData.currentMode == GameData.Mode.BattleMode)
                {
                    gameData.gameResult = (isPlayer1) ? GameData.GameResult.Player2Win : GameData.GameResult.Player1Win;
                }
                else // in classic and innovative mode, play will lose
                {
                    gameData.gameResult = GameData.GameResult.Lose;
                }
            }
        }
    }