Esempio n. 1
0
    IEnumerator UpdateGameLogic()
    {
        while (isGame)
        {
            //Gameloop and update logic
            // Debug.Log("TICK : " + Time.time);
            if (power.IsPacmanSuper())
            {
                AudioManager.PlaySound("fuite");
                CurrentMode = ChaseMode.Frighten;
            }
            //Check if pacman collide an ennemy
            foreach (GhostBehavior ghost in ghostPrefabs)
            {
                ghost.ComputeNextMove(CurrentMode);
                if (ghost.position == pacman.position)
                {
                    pacman.colliding = true;
                }
            }

            if (pacman.colliding && pacmanLife > 0 && !power.IsPacmanSuper())
            {
                AudioManager.PlaySound("death");
                pacman.colliding = false;
                pacman.lastDir   = MoveDir.Up;
                pacmanLife      -= 1;
                UIManager.Touch();
                pacman.Spawn();

                GhostRespawn();
            }
            else if (pacman.colliding && power.IsPacmanSuper())
            {
                pacman.colliding = false;
                GhostBehavior closest  = ghostPrefabs[0];
                float         distance = 100f;
                foreach (GhostBehavior ghost in ghostPrefabs)
                {
                    if (Vector2.Distance(ghost.position, pacman.position) < distance)
                    {
                        closest  = ghost;
                        distance = Vector2.Distance(ghost.position, pacman.position);
                        Debug.Log("Closest is : " + closest.name);
                    }
                }
                closest.Spawn();
                AudioManager.PlaySound("chomp");
            }
            else if (pacman.colliding && pacmanLife == 0)
            {
                pacman.colliding = false;
                AudioManager.PlaySound("death");
                score.SaveScore();
                UIManager.GameOver();
                isGame = false;
            }

            //Update Pacman
            pacman.Move(currDirection);

            pacman.colliding = false;

            //Wait timeTillUpdate seconds till next update cycle
            while (triggerUpdate == false)
            {
                yield return(null);
            }

            triggerUpdate = false;
        }
    }