Exemple #1
0
    /*
     * Check and handle the collisions encounted by the collider
     */
    private void CheckCollision(Collider2D collision)
    {
        if (collision.gameObject.CompareTag("Dots"))
        {
            PlayEatSound();
            Destroy(collision.gameObject);
            AddScore(dotScore);
        }
        else if (collision.gameObject.CompareTag("Large Dots"))
        {
            if (gameManager != null)
            {
                gameManager.SendMessage("ScatterAllGhosts");
            }

            if (dotParticleObject != null)
            {
                Instantiate(dotParticleObject, transform.position, transform.rotation);
            }

            if (eatGhostSound != null)
            {
                audioSource.PlayOneShot(eatGhostSound);
            }

            Destroy(collision.gameObject);
            AddScore(largeDotScore);
            PowerUp();
            Invoke("PowerDown", poweredUpTimeCurrent);
        }
        else if (collision.gameObject.CompareTag("Ghosts"))
        {
            if (powerState.Equals(PoweredStates.PoweredUp))
            {
                GhostController ghostController = collision.gameObject.GetComponent <GhostController>();
                ghostController.SendMessage("Die");

                AddScore(ghostScore * ghostsEatenCounter);
                ghostsEatenCounter += 1;
                StartCoroutine(PauseGame(1.0f));

                if (eatGhostSound != null)
                {
                    audioSource.PlayOneShot(eatGhostSound);
                }
            }
            else
            {
                if (ghostEatTimer + 2f < Time.time)
                {
                    if (deathSound != null)
                    {
                        audioSource.PlayOneShot(deathSound);
                    }

                    LoseLife();
                    StartCoroutine(PauseGame(3.0f));
                    ghostEatTimer = Time.time;

                    if (currentLives > 0)
                    {
                        transform.position = startingPosition;
                        movementDirection  = MovementDirections.Right;
                    }
                    else
                    {
                        transform.position = new Vector3(999f, 999f);
                        StartCoroutine(EndGame());
                    }
                }
            }
        }
        else if (collision.gameObject.CompareTag("Powerup"))
        {
            if (collision.name.Contains("Double Points"))
            {
                SetScoreModifier(2);
            }
            else if (collision.name.Contains("Extra Life"))
            {
                AddLife();
            }
            else if (collision.name.Contains("Freeze"))
            {
                Freeze();
            }
            else if (collision.name.Contains("Invisible Walls"))
            {
                gameManager.SendMessage("HideWalls");
            }
            else if (collision.name.Contains("No Points"))
            {
                SetScoreModifier(0);
            }
            else if (collision.name.Contains("Speed Boost"))
            {
                SpeedUp(1.4f);
            }

            Destroy(collision.gameObject);
        }
    }