コード例 #1
0
    IEnumerator InitLevel()
    {
        yield return(new WaitForEndOfFrame());

        hasReachedExit       = false;
        board                = GameObject.FindWithTag("Board").GetComponent <Board>();
        characterSelectPanel = GameObject.FindWithTag("CharacterSelect").GetComponent <CharacterSelect>();
        Level level = Resources.Load("Levels/" + levelToLoad) as Level;

        levelName = level.title;
        timeLimit = level.timeLimit;

        victoryPanel = GameObject.FindGameObjectWithTag("VictoryPanel").transform.GetChild(0).gameObject;

        playerSpawnPoints = new List <PlayerSpawnPoint>();
        actors            = new List <Actor>();

        playerAvailableCharactersPrefabs = new List <GameObject>();
        playerPreviews = new List <GameObject>();
        playerIcons    = new List <Sprite>();

        foreach (Level.PlayerClass cls in level.classes)
        {
            GameObject prefab  = null;
            GameObject preview = null;
            Sprite     icon    = null;
            switch (cls)
            {
            case Level.PlayerClass.ARCHER:
                prefab  = prefabArcher;
                preview = previewArcher;
                icon    = iconArcher;
                break;

            case Level.PlayerClass.THIEF:
                prefab  = prefabThief;
                preview = previewThief;
                icon    = iconThief;
                break;

            case Level.PlayerClass.WARRIOR:
                prefab  = prefabWarrior;
                preview = previewWarrior;
                icon    = iconWarrior;
                break;
            }
            playerAvailableCharactersPrefabs.Add(prefab);
            playerPreviews.Add(preview);
            playerIcons.Add(icon);
        }

        System.Random rng = new System.Random();
        characterSelectPanel.Init();
        foreach (Position pos in level.walls)
        {
            CreateWall(rng, pos.row, pos.col);
        }
        foreach (Position pos in level.spawnPoints)
        {
            Vector3 spawnPos = board.GetCoordinates(pos.row, pos.col);
            spawnPos += 0.1f * Vector3.up;
            GameObject objSP = Instantiate(
                prefabSpawnPoint,
                spawnPos,
                prefabSpawnPoint.transform.rotation);
            PlayerSpawnPoint spawn = objSP.GetComponent <PlayerSpawnPoint>();
            spawn.r = pos.row;
            spawn.c = pos.col;
            playerSpawnPoints.Add(spawn);
        }
        foreach (Position pos in level.coins)
        {
            Vector3 coinPos = board.GetCoordinates(pos.row, pos.col);
            coinPos += prefabCoin.transform.localScale.x * Vector3.up;
            Instantiate(
                prefabCoin, coinPos, prefabCoin.transform.rotation);
        }
        foreach (Level.EnemyInstance inst in level.enemies)
        {
            GameObject prefab = null;
            switch (inst.enemyType)
            {
            case Level.EnemyType.TEST_ENEMY:
                prefab = prefabTestEnemy;
                break;

            case Level.EnemyType.SKELETON:
                prefab = prefabSkeleton;
                break;
            }
            Actor actor = Instantiate(prefab).GetComponent <Actor>();
            actor.Spawn(board, inst.position.row, inst.position.col);
            inst.plan.ForEach(
                action => actor.AddAction(action));
            actors.Add(actor);
        }
        if (level.button.row != -1 && level.button.col != -1 && level.door.row != -1 && level.door.col != -1)
        {
            Vector3    doorPos = board.GetCoordinates(level.door.row, level.door.col);
            GameObject door    = Instantiate(
                prefabDoor, doorPos, Quaternion.identity);
            board.Set(level.door.row, level.door.col, door);

            Vector3    buttonPos = board.GetCoordinates(level.button.row, level.button.col);
            GameObject button    = Instantiate(
                prefabButton, buttonPos, Quaternion.identity);
            button.GetComponent <ButtonBehaviour>().door      = door;
            button.GetComponent <ButtonBehaviour>().targetCol = level.door.col;
            button.GetComponent <ButtonBehaviour>().targetRow = level.door.row;
        }

        for (int i = 0; i < Board.GRID_ROWS; i++)
        {
            for (int j = 0; j < Board.GRID_COLS; j++)
            {
                if (i != level.exit.row || j != level.exit.col)
                {
                    CreateFloorTile(rng, i, j);
                }
            }
        }
        Vector3 exitPos = board.GetCoordinates(level.exit.row, level.exit.col);

        Instantiate(prefabExit, exitPos, Quaternion.identity);

        Init();
    }