コード例 #1
0
ファイル: RoomSpawner.cs プロジェクト: veerdonk/Dungeon
    public void UpdatePlayerLocationAndSpawnRoom(GameObject corridor)
    {
        //Retrieve information about corridor
        Corridor corridorData = null;

        foreach (Corridor corr in corridors)
        {
            if (corridor.name == corr.name)
            {
                corridorData = corr;
            }
        }

        //Disable old room
        roomGrid[playerGridLocX][playerGridLocY].SetActive(false);
        //Disable old walls
        foreach (Transform wall in rooms[new Vector3(playerGridLocX, playerGridLocY)].walls)
        {
            wall.GetComponent <ShadowTrigger>().DisableWall();
        }
        Vector3 newPlayerPos = new Vector3();

        //Create new room in right direction
        int playerXOfsset = 0;
        int playerYOfsset = 0;

        GameObject roomToAdd;


        //TODO refactor to use rooms from right biome
        switch (direction)
        {
        case Constants.EXIT_TOP:
            playerGridLocY++;
            playerYOfsset = -2;
            roomToAdd     = templates.roomsPerBiome[currentBiome].Brooms[Random.Range(0, templates.roomsPerBiome[currentBiome].Brooms.Count)];
            //roomToAdd = templates.dungeonBrooms[Random.Range(0, templates.dungeonBrooms.Count)];
            break;

        case Constants.EXIT_BOT:
            playerGridLocY--;
            playerYOfsset = 2;
            roomToAdd     = templates.roomsPerBiome[currentBiome].Trooms[Random.Range(0, templates.roomsPerBiome[currentBiome].Trooms.Count)];
            //roomToAdd = templates.dungeonTrooms[Random.Range(0, templates.dungeonTrooms.Count)];
            break;

        case Constants.EXIT_RIGHT:
            playerGridLocX++;
            playerXOfsset = -2;
            roomToAdd     = templates.roomsPerBiome[currentBiome].Lrooms[Random.Range(0, templates.roomsPerBiome[currentBiome].Lrooms.Count)];
            //roomToAdd = templates.dungeonLrooms[Random.Range(0, templates.dungeonLrooms.Count)];
            break;

        case Constants.EXIT_LEFT:
            playerGridLocX--;
            playerXOfsset = 2;
            roomToAdd     = templates.roomsPerBiome[currentBiome].Rrooms[Random.Range(0, templates.roomsPerBiome[currentBiome].Rrooms.Count)];
            //roomToAdd = templates.dungeonRrooms[Random.Range(0, templates.dungeonRrooms.Count)];
            break;

        default:
            roomToAdd = null;
            Debug.LogError($"Entrance code unknown: {direction}");
            break;
        }

        GameObject addedRoom = null;

        if (roomGrid.ContainsKey(playerGridLocX))
        {
            if (!roomGrid[playerGridLocX].ContainsKey(playerGridLocY))
            {
                //X exists, Y does not
                addedRoom = AddRoom(roomToAdd);
                AddExits(addedRoom.transform);
            }
            else
            {
                //X/Y both exist -> enable existing room
                addedRoom         = roomGrid[playerGridLocX][playerGridLocY];
                possibleEntrances = rooms[new Vector3(playerGridLocX, playerGridLocY)].entrancePositions;
                roomGrid[playerGridLocX][playerGridLocY].SetActive(true);
            }
        }
        else
        {
            //X/Y do not exist
            addedRoom = AddRoom(templates.dungeonRooms[Random.Range(0, templates.dungeonRooms.Length)]);
            AddExits(addedRoom.transform);
        }

        foreach (Transform entrance in possibleEntrances)
        {
            if (entrance.name == Constants.opositeEntrance[direction])
            {
                Debug.Log($"Opposite entrance = {entrance.name}, position = {entrance.position}");
                newPlayerPos = entrance.position;
                break;
            }
        }

        newPlayerPos.x += playerXOfsset;
        newPlayerPos.y += playerYOfsset;

        //Set player to right spot
        playerController.setPlayerPosition(newPlayerPos);

        //Set direction to origin dir
        direction = Constants.opositeEntrance[direction];
        //Once all exits have been added rescan the A* pathfinding
        //AstarPath.active.Scan();
    }