public void CreateDungeon() { if (floor == 4 && !generateBossLevel) { return; } loadingScreen.SetActive(true); DestroyDungeon(); if (generateBossLevel) { audioPlayer.GetComponent <AudioPlayer>().StopPlayingMusic(); bossLevel.SetActive(true); if (!playerObject) { playerObject = Instantiate(playerPrefab, bossLevel.transform.FindChild("PlayerSpawn").position, Quaternion.identity) as GameObject; } playerObject.transform.position = bossLevel.transform.FindChild("PlayerSpawn").position; playerObject.GetComponent <PlayerAI>().dest = playerObject.transform.position; GameObject boss = Instantiate(bossPrefab, bossLevel.transform.FindChild("BossSpawn").position, Quaternion.identity) as GameObject; if (InitializeGrid) { // Disable in case we have already made a grid. gameWorld.GetComponent <GridComponent>().Disable(100); // Initialize grid. gameWorld.GetComponent <GridComponent>().Initialize(100, (ignored) => OnGridWasInitialized()); } generateBossLevel = false; return; } if (floor >= 1) { foreach (GameObject monster in hardMonsterObjects) { if (!monsterObjects.Contains(monster)) { monsterObjects.Add(monster); } } } csDungeonRoomGenerator rG = new csDungeonRoomGenerator(); int roomSizeMin = 2; int roomSizeMax = 2; switch (m_dungeonRoomSize) { case enDungeonRoomSize.Small: roomSizeMin = 2; roomSizeMax = 2; break; case enDungeonRoomSize.Medium: roomSizeMin = 2; roomSizeMax = 3; break; case enDungeonRoomSize.Large: roomSizeMin = 2; roomSizeMax = 4; break; } rG.Constructor (m_maxRoomCount, roomSizeMin, roomSizeMax, roomSizeMin, roomSizeMax); csDungeonGenerator dG = new csDungeonGenerator(); dG.Constructor ((int)m_dungeonSize, (int)m_dungeonSize, (int)m_twists, (int)m_corridors, (int)m_deadEnds, rG); csDungeon dungeon = dG.Generate(); int[,] tiles = csDungeonGenerator.ExpandToTiles (dungeon); if (this.floorPrefab && this.wallsPrefab && this.doorsPrefab) { for (int k = 0; k < tiles.GetUpperBound(0); k++) { for (int l = 0; l < tiles.GetUpperBound(1); l++) { float x = k * cellSpacingFactor; float y = l * cellSpacingFactor; if (tiles[k, l] != (int)csDungeonCell.TileType.Rock && tiles[k, l] != (int)csDungeonCell.TileType.Void && tiles[k, l] != (int)csDungeonCell.TileType.OuterRock) { GameObject tile = (GameObject)Instantiate (this.floorPrefab, new Vector3(x, this.floorHeightOffset, y), Quaternion.identity); tile.transform.parent = this.transform; if (this.GenerateCeiling) { tile = (GameObject)Instantiate (this.floorPrefab, new Vector3(x, this.roofsHeightOffset, y), Quaternion.identity); tile.transform.parent = this.transform; } } if (tiles[k, l] == (int)csDungeonCell.TileType.Rock) { GameObject tile = (GameObject)Instantiate (this.wallsPrefab, new Vector3(x, this.wallsHeightOffset, y), Quaternion.identity); tile.transform.parent = this.transform; } if (tiles[k, l] == (int)csDungeonCell.TileType.OuterRock) { GameObject tile = (GameObject)Instantiate (this.wallsPrefab, new Vector3(x, this.wallsHeightOffset, y), Quaternion.identity); tile.transform.parent = this.transform; Destroy(tile.GetComponent <BoxCollider>()); } if (tiles[k, l] == (int)csDungeonCell.TileType.DoorNS) { GameObject tile = (GameObject)Instantiate (this.doorsPrefab, new Vector3(x, this.doorsHeightOffset, y), Quaternion.identity); tile.transform.parent = this.transform; } if (tiles[k, l] == (int)csDungeonCell.TileType.DoorEW) { GameObject tile = (GameObject)Instantiate (this.doorsPrefab, new Vector3(x, this.doorsHeightOffset, y), Quaternion.AngleAxis(90.0f, Vector3.up)); tile.transform.parent = this.transform; } } } // Spawn rare chest loot. SpawnLoot(tiles); if (InitializeGrid) { // Disable in case we have already made a grid. gameWorld.GetComponent <GridComponent>().Disable(100); // Initialize grid. gameWorld.GetComponent <GridComponent>().Initialize(100, (ignored) => OnGridWasInitialized(tiles)); } } }
public void CreateDungeon(int seed) { csRandom.Instance.Seed(seed); csDungeonRoomGenerator rG = new csDungeonRoomGenerator(); int roomSizeMin = 2; int roomSizeMax = 2; switch (m_dungeonRoomSize) { case enDungeonRoomSize.Small: roomSizeMin = 2; roomSizeMax = 2; break; case enDungeonRoomSize.Medium: roomSizeMin = 2; roomSizeMax = 3; break; case enDungeonRoomSize.Large: roomSizeMin = 2; roomSizeMax = 4; break; } rG.Constructor (m_maxRoomCount, roomSizeMin, roomSizeMax, roomSizeMin, roomSizeMax); csDungeonGenerator dG = new csDungeonGenerator(); dG.Constructor ((int)m_dungeonSize, (int)m_dungeonSize, (int)m_twists, (int)m_corridors, (int)m_deadEnds, rG); csDungeon dungeon = dG.Generate(); int[,] tiles = csDungeonGenerator.ExpandToTiles (dungeon); if (this.floorPrefab && this.wallsPrefab && this.doorsPrefab) { for (int k = 0; k < tiles.GetUpperBound(0); k++) { for (int l = 0; l < tiles.GetUpperBound(1); l++) { float x = k * cellSpacingFactor; float y = l * cellSpacingFactor; if (tiles[k, l] != (int)csDungeonCell.TileType.Rock && tiles[k, l] != (int)csDungeonCell.TileType.Void) { GameObject tile = (GameObject)Instantiate (this.floorPrefab, new Vector3(x, this.floorHeightOffset, y), Quaternion.identity); tile.transform.parent = this.transform; if (this.GenerateCeiling) { tile = (GameObject)Instantiate (this.floorPrefab, new Vector3(x, this.roofsHeightOffset, y), Quaternion.identity); tile.transform.parent = this.transform; } } if (tiles[k, l] == (int)csDungeonCell.TileType.Rock) { GameObject tile = (GameObject)Instantiate (this.wallsPrefab, new Vector3(x, this.wallsHeightOffset, y), Quaternion.identity); tile.transform.parent = this.transform; } if (tiles[k, l] == (int)csDungeonCell.TileType.DoorNS) { GameObject tile = (GameObject)Instantiate (this.doorsPrefab, new Vector3(x, this.doorsHeightOffset, y), Quaternion.identity); tile.transform.parent = this.transform; } if (tiles[k, l] == (int)csDungeonCell.TileType.DoorEW) { GameObject tile = (GameObject)Instantiate (this.doorsPrefab, new Vector3(x, this.doorsHeightOffset, y), Quaternion.AngleAxis(90.0f, Vector3.up)); tile.transform.parent = this.transform; } } } //Find a dead end to place the player in if (this.playerObject) { for (int k = 1; k < tiles.GetUpperBound(0) - 1; k++) { for (int l = 1; l < tiles.GetUpperBound(1) - 1; l++) { if (tiles[k, l] == (int)csDungeonCell.TileType.Corridor) { int deadEndWeight = 0; if (tiles[k - 1, l - 1] == (int)csDungeonCell.TileType.Rock) { deadEndWeight++; } if (tiles[k, l - 1] == (int)csDungeonCell.TileType.Rock) { deadEndWeight++; } if (tiles[k + 1, l - 1] == (int)csDungeonCell.TileType.Rock) { deadEndWeight++; } if (tiles[k - 1, l] == (int)csDungeonCell.TileType.Rock) { deadEndWeight++; } if (tiles[k + 1, l] == (int)csDungeonCell.TileType.Rock) { deadEndWeight++; } if (tiles[k - 1, l + 1] == (int)csDungeonCell.TileType.Rock) { deadEndWeight++; } if (tiles[k, l + 1] == (int)csDungeonCell.TileType.Rock) { deadEndWeight++; } if (tiles[k + 1, l + 1] == (int)csDungeonCell.TileType.Rock) { deadEndWeight++; } if (deadEndWeight == 7) { playerObject.transform.position = new Vector3(k * cellSpacingFactor, 1.0f, l * cellSpacingFactor); } } } } } if (this.monsterObject) { bool itemGiven = false; for (int k = 1; k < tiles.GetUpperBound(0) - 1; k++) { for (int l = 1; l < tiles.GetUpperBound(1) - 1; l++) { if (tiles[k, l] == (int)csDungeonCell.TileType.Room && monstersInRooms == true) { if (Random.Range(0, 100) < monsterAppearChance) { GameObject monster = (GameObject) Instantiate(monsterObject, new Vector3(k * cellSpacingFactor, 1.05f, l * cellSpacingFactor), Quaternion.identity); monster.transform.parent = this.transform; if (!itemGiven) { monster.GetComponentInChildren <EnemyProps>().hasItem = true; // Give this monster the item!!! itemGiven = true; } } } if (tiles[k, l] == (int)csDungeonCell.TileType.Corridor && monstersInCorridors == true) { if (Random.Range(0, 100) < monsterAppearChance) { GameObject monster = (GameObject) Instantiate(monsterObject, new Vector3(k * cellSpacingFactor, 1.05f, l * cellSpacingFactor), Quaternion.identity); monster.transform.parent = this.transform; if (!itemGiven) { monster.GetComponentInChildren <EnemyProps>().hasItem = true; // Give this monster the item!!! itemGiven = true; } } } } } } } }