示例#1
0
    // Restarts the level
    public void ReloadLevel(bool resetPoints = false)
    {
        countdownText.enabled = true;
        playLevel             = false; // so that the countdown starts
        scoreDisplayParent.SetActive(false);
        Time.timeScale = 1;
        StartCoroutine(StartCountdown());
        ghostList.Clear();

        if (resetPoints)
        {
            numDotsEaten = 0;
            // this destroys all the gameobjects created and resets the level
            for (int i = 0; i < levelGameObjects.Count; i++)
            {
                Destroy(levelGameObjects[i]);
            }
            levelGameObjects.Clear();

            Points           = 0;
            ghostKills       = 0;
            numLives         = 3;
            hasSpawnedCherry = false;
        }

        // reset the ghosts and pacman
        for (int i = 0; i < resetableGameObjects.Count; i++)
        {
            Destroy(resetableGameObjects[i]);
        }
        resetableGameObjects.Clear();
        system.Ghosts.Clear();

        currentGhostKills = 0;
        // now spawn new ones and add them to the levelgameobjects list:
        GameObject pacman = Instantiate(pacmanPrefab);

        resetableGameObjects.Add(pacman);
        system.Pacman = pacman;

        // set the pacman transform and orientation etc:
        pacman.GetComponent <PacmanMovement>().SetLevelManager(this, pacmanSpawnLocation, pacmanSpawnOrientation);

        for (int i = 0; i < 4; i++)
        {
            // only spawn it if the x >= 0, otherwise it's off the map because it doesn't have a spawning position
            if (ghostSpawnLocations[i].x >= 0)
            {
                GameObject ghost = Instantiate(ghostPrefabs[i]);
                ghost.SetActive(true);
                resetableGameObjects.Add(ghost);
                ghost.GetComponent <GhostMovement>().SetLevelManager(this, ghostSpawnLocations[i], ghostSpawnOrientations[i]);
                system.Ghosts.Add(ghost);
                IndividualFSMSystem s = ghost.GetComponent <IndividualFSMSystem>();
                s.enableFSMControl = !system.enableFSMControl; // set the AI control
                s.Pacman           = pacman;
                ghostList.Add(ghost);
            }
        }
        chasestate.OnReload(isWalkableArray, levelWidth, levelHeight);
        flankstate.OnReload(isWalkableArray, levelWidth, levelHeight);
        deathstate.OnReload(isWalkableArray, levelWidth, levelHeight, ghostSpawnLocations);
        system.OnReload();
        int ghostIndex = 0;

        foreach (GameObject ghost in ghostList)
        {
            IndividualFSMSystem s = ghost.GetComponent <IndividualFSMSystem>();
            s.OnReload(isWalkableArray, levelWidth, levelHeight, ghostSpawnLocations, ghostIndex);
            ghostIndex++;
        }

        if (resetPoints)
        {
            for (int i = 0; i < dotLocations.Count; i++)
            {
                GameObject dot = Instantiate(dotPrefab);
                levelGameObjects.Add(dot);
                dot.transform.position = dotLocations[i];
            }
            for (int i = 0; i < bigDotLocations.Count; i++)
            {
                GameObject bigDot = Instantiate(bigDotPrefab);
                levelGameObjects.Add(bigDot);
                bigDot.transform.position = bigDotLocations[i];
            }
            if (levelHasCherry)
            {
                GameObject cherry = Instantiate(cherryPrefab);
                levelGameObjects.Add(cherry);
                cherry.SetActive(false);
                cherryGameObject          = cherry;
                cherry.transform.position = cherryLocation;
            }
        }
    }