// Generate a new Room public void Generate() { GridGenerator.Rooms += 0.02f; if (GridGenerator.Corridors < 5) { GridGenerator.Corridors += 0.04f; } _height = Random.Range(6, 12); _width = Random.Range(6, 12); _corridor = GetComponent <CorridorGenerator>(); for (int i = 0; i < _width; i++) { for (int j = 0; j < _height; j++) { if (GridGenerator.objs[Mathf.FloorToInt(i + pos.x - _width / 2)][Mathf.FloorToInt(j + pos.y - _height / 2)] != null) { GridGenerator.objs[Mathf.FloorToInt(i + pos.x - _width / 2)][Mathf.FloorToInt(j + pos.y - _height / 2)].GetComponent <RoomGenerator>().ToFloor(); } } } _corridor.CreateCorridors(pos, _width, _height); GetComponent <InteriorGenerator>().Fill(pos, _height, _width); }
public static void Apply(Level level, Parameters parameters, int seed) { var stopwatch = new System.Diagnostics.Stopwatch(); stopwatch.Start(); BSPAlgorithm bsp = new BSPAlgorithm(); bsp.m_minNodeSize = new Vector2Int(level.Grid.Tiles.GetLength(0) / parameters.MinNumRooms, level.Grid.Tiles.GetLength(1) / parameters.MinNumRooms); //Random.InitState(seed); var root = new Node(level.Grid.Tiles); bsp.DivideGrid(level.Grid, root); level.Rooms = new HashSet <Tile[, ]>(); var roomGenerator = new RoomGenerator(level, new Vector2Int(parameters.MinRoomWidth, parameters.MinRoomHeight)); roomGenerator.PlaceRooms(root); var corridorGenerator = new CorridorGenerator(level.Grid, parameters.CorridorType); corridorGenerator.PlaceCorridors(root); stopwatch.Stop(); Debug.Log("BSPAlgorithm took " + stopwatch.ElapsedMilliseconds + " milliseconds to complete."); }
public List <Node> CalculateDungeon(int maxPasses, int roomMinWidth, int roomMinLength, float bottomCornerModifier, float topCornerModifier, int roomOffset, int corridorWidth, EnemySpawner enemSpawner, DungeonCreator dungeonCreator, ItemSpawner itemSpawner, ObjectSpawner objSpawner) { BinarySpacePartitioner bsp = new BinarySpacePartitioner(dungeonWidth, dungeonLength); allSpaceNodes = bsp.PrepareNodesCollection(maxPasses, roomMinWidth, roomMinLength); List <Node> roomSpaces = StructureHelper.TraverseGraphToExtractLowestLeaves(bsp.rootNode); RoomGenerator roomGenerator = new RoomGenerator(maxPasses, roomMinLength, roomMinWidth); List <RoomNode> roomList = roomGenerator.GenerateRoomsInGivenSpaces(roomSpaces, bottomCornerModifier, topCornerModifier, roomOffset); //first room will be the spawn point for the player RoomNode firstRoom = roomList[0]; UnityEngine.CharacterController player = GameObject.FindWithTag("Player").GetComponent <UnityEngine.CharacterController>(); Vector2Int firstRoomCenter = StructureHelper.CalculateMiddlePoint(firstRoom.BottomLeftCorner, firstRoom.TopRightCorner); Vector3 newPos = new Vector3(firstRoomCenter.x, 0.0f, firstRoomCenter.y); player.enabled = false; player.transform.position = newPos; player.enabled = true; //generate spawn points before we add the corridors for (int i = 0; i < roomList.Count; i++) { RoomNode room = roomList[i]; if (enemSpawner != null) { GameObject newRoom = new GameObject("RoomObj", typeof(Room), typeof(BoxCollider)); newRoom.tag = "RoomObject"; BoxCollider col = newRoom.GetComponent <BoxCollider>(); col.isTrigger = true; col.size = new Vector3(1.2f, 1.2f, 1.2f); Vector2Int roomPos = StructureHelper.CalculateMiddlePoint(room.BottomLeftCorner, room.TopRightCorner); newRoom.transform.position = new Vector3(roomPos.x, 2, roomPos.y); Room roomComp = newRoom.GetComponent <Room>(); room.roomObjReference = roomComp; newRoom.transform.localScale = new Vector3(room.Width, 4, room.Length); roomComp.enemySpawnPoints = new List <Vector3>(); roomComp.itemSpawnPoints = new List <Vector3>(); roomComp.weaponSpawnPoints = new List <Vector3>(); roomComp.objectSpawnPoints = new List <Vector3>(); roomComp.doors = new List <Transform>(); dungeonCreator.spawnedRooms.Add(roomComp); if (i != 0) { enemSpawner.GenerateEnemySpawnPointsForRoom(room); itemSpawner.GenerateItemSpawnPointsForRoom(room); objSpawner.GenerateObjectSpawnPointsForRoom(room); } } } CorridorGenerator corridorGenerator = new CorridorGenerator(); var corridorList = corridorGenerator.CreateCorridor(allSpaceNodes, corridorWidth); dungeonCreator.GenerateDoors(corridorList); return(new List <Node>(roomList).Concat(corridorList).ToList()); }
private void CalculateNewPosition() { // Movement calculation in x-direction float rawNewXPos = transform.localPosition.x + xMovement * xMovementPerTick; float clampedXPos = Mathf.Clamp(rawNewXPos, characterStartingXPosition - xRange, characterStartingXPosition + xRange); // Movement calculation in z-direction // Do not allow z movement if already moved in x, x is preferred axis if (xMovement != 0) { zMovement = 0f; } float rawNewZPos = transform.localPosition.z + zMovement * zMovementPerTick; // Check the obstacle tracker to see if we can move to where we want CorridorGenerator corridorGenerator = sceneCorridorGenerator.GetComponent <CorridorGenerator>(); // todo remove this - only here for debug purposes // print("z: " + rawNewZPos + "\nx: " + clampedXPos); if (corridorGenerator.obstacleListTracker[((int)rawNewZPos - 1) / 2][((int)clampedXPos - 1) / 2] == 0) { // Change to new position playerMoveDirector.targetPosition = new Vector3(clampedXPos, transform.localPosition.y, rawNewZPos); // Score the player if they moved forward if (zMovement > Mathf.Epsilon) { score.GetComponent <ScoreTracker>().ScoreMovementForward(); } } }
public List <Node> CalculateDungeon( int maxIterations, int roomWidthMin, int roomLengthMin, float roomBottomCornerModifier, float roomTopCornerModifier, int roomOffset, int corridorWidth) { //splits the room BinarySpacePartitioner bsp = new BinarySpacePartitioner(dungeonWidth, dungeonLength); allNodesCollection = bsp.PrepareNodesCollection(maxIterations, roomWidthMin, roomLengthMin); List <Node> roomSpaces = StructureHelper.TraverseGraphToExtractLowestLeafes(bsp.RootNode); RoomGenerator roomGenerator = new RoomGenerator(maxIterations, roomLengthMin, roomWidthMin); List <RoomNode> roomList = roomGenerator.GenerateRoomsInGivenSpace(roomSpaces, roomBottomCornerModifier, roomTopCornerModifier, roomOffset); CorridorGenerator corridorGenerator = new CorridorGenerator(); var corridorList = corridorGenerator.CreateCorridor(allNodesCollection, corridorWidth); return(new List <Node>(roomList).Concat(corridorList).ToList()); }
private void MovementAttempt() { if (!enemyAlive) { return; } // Randomise if we're going to make a movement on this beat float movementRandomRoll = UnityEngine.Random.Range(0f, 1f); // Then if we did hit the chance to move, we randomise what direction we're going to move in if (movementRandomRoll <= chanceToMove) { int movementRandomDirection = UnityEngine.Random.Range(0, 4); CorridorGenerator corridorGenerator = sceneCorridorGenerator.GetComponent <CorridorGenerator>(); // Target position (moving forward, backward, left or right in that order) // is based on the roll from the random generator switch (movementRandomDirection) { case 0: targetX = transform.position.x; targetZ = transform.position.z - zMovementPerTick; break; case 1: targetX = transform.position.x; targetZ = transform.position.z + zMovementPerTick; break; case 2: targetX = Mathf.Clamp(transform.position.x - xMovementPerTick, enemyStartingPosition.x - xRange, enemyStartingPosition.x + xRange); targetZ = transform.position.z; break; default: targetX = Mathf.Clamp(transform.position.x + xMovementPerTick, enemyStartingPosition.x - xRange, enemyStartingPosition.x + xRange); targetZ = transform.position.z; break; } // First, want to prevent the enemy moving back further than the corridor actually is if ((((int)targetZ - 1) / 2) > (corridorGenerator.obstacleListTracker.Count - 1)) { targetZ = transform.position.z; } // Now we check the corridor generator to see if there's an obstacle on // that tile. If there isn't, we set the enemy target position to its new position if (corridorGenerator.obstacleListTracker[((int)targetZ - 1) / 2][((int)targetX - 1) / 2] == 0) { // Change to new position enemyMoveDirector.targetPosition = new Vector3(targetX, transform.position.y, targetZ); } } }
public LevelGenerator() { rp = new RoomPlacer(); ra = new RoomArranger(); cg = new CorridorGenerator(); op = new ObjectPlacer(); llRooms = new List<Room>(); }
public override void OnInspectorGUI() { CorridorGenerator corridor = target as CorridorGenerator; if (DrawDefaultInspector()) { corridor.GenerateMap(); } }
private void Awake() { jsonManager = GetComponent <JsonManager>(); objectData = GetComponent <ObjectData>(); corridorGenerator = GameObject.Find("CorriderGenerator").GetComponent <CorridorGenerator>(); obstacleList = new GameObject[objectData.obstacleList.Length]; for (int i = 0; i < obstacleList.Length; i++) { obstacleList[i] = objectData.obstacleList[i]; } }
void Start() { rooms = GetComponent <RoomGenerator>(); corridors = GetComponent <CorridorGenerator>(); walls = GetComponent <WallGenerator>(); chests = GetComponent <ChestGenerator>(); playerAndMerchantsGenerator = GetComponent <PlayerAndMerchantsGenerator>(); enemiesGenerator = GetComponent <EnemiesGenerator>(); StartCoroutine(CreateDungeon()); // }
//public IStairGenerator StairGenerator { get; set; } //public ITreasureGenerator TreasureGenerator { get; set; } //public IMonsterGenerator MonsterGenerator { get; set; } public void Generate(Game gameInstance) { try { Logger.Info("Generating dungeon..."); Logger.Info("Generating terrain..."); gameInstance.Terrain = new TerrainMap(Rng.Next(MinSize.Width, MaxSize.Width), Rng.Next(MinSize.Height, MaxSize.Height), () => new Wall() { Material = new Rock() }); if (CorridorGenerator != null) { Logger.Info("Generating corridors..."); CorridorGenerator.GenerateCorridors(gameInstance.Terrain); } if (RoomGenerator != null) { Logger.Info("Generating rooms..."); RoomGenerator.GenerateRooms(gameInstance.Terrain); } if (DoorGenerator != null) { Logger.Info("Generating doors..."); DoorGenerator.GenerateDoors(gameInstance.Terrain); } WalkTheDungeon(gameInstance); if (DoorGenerator != null) { DoorGenerator.RemoveInvalidDoors(gameInstance.Terrain); DoorGenerator.ResetDoors(gameInstance.Terrain); } //if (StairGenerator != null) StairGenerator.GenerateStairs(dungeon); //if (TreasureGenerator != null) TreasureGenerator.GenerateTreasure(dungeon); //if (MonsterGenerator != null) MonsterGenerator.GenerateMonsters(dungeon); Logger.Info("Dungeon generation complete."); } catch (Exception ex) { Logger.Error("An error occurred during dungeon generation", ex); throw; } }
public List <Node> BuildLevel( int maxIterations, int roomWidthMin, int roomLengthMin, int corridorWidth, float bottomLeftPointModifier, float topRightPointModifier, int offset) { BinarySpacePartitioner bsp = new BinarySpacePartitioner(levelWidth, levelLength); allNodesCollection = bsp.PrepareNodesCollection(maxIterations, roomWidthMin, roomLengthMin); List <Node> roomSpaces = StructureHelper.TraverseGraphToExtractLowestLeaf(bsp.RootNode); RoomGenerator roomGenerator = new RoomGenerator(maxIterations, roomLengthMin, roomWidthMin); List <RoomNode> roomList = roomGenerator.GenerateRoomInGivenSpace(roomSpaces, bottomLeftPointModifier, topRightPointModifier, offset); CorridorGenerator corridorGenerator = new CorridorGenerator(); List <Node> corridors = corridorGenerator.CreateCorridor(allNodesCollection, corridorWidth); return(new List <Node>(roomList).Concat(corridors).ToList()); }
//--------------------------------------------------------------------------- private List <Corridor> GenerateCorridors(Dictionary <Room, List <Room> > graph) { List <Corridor> corridors = new List <Corridor>(); m_CorridorGenerator = new CorridorGenerator(CreateMap(Size), Size); //m_CorridorGenerator = new CorridorGenerator(CreateMap(1), 1); foreach (KeyValuePair <Room, List <Room> > kvp in graph) { foreach (Room room in kvp.Value) { Exit leftExit = null; Exit rightExit = null; Room.GetClosestExits(kvp.Key, room, out leftExit, out rightExit); Corridor corridor = m_CorridorGenerator.Execute(leftExit, rightExit); if (corridor != null) { corridors.Add(corridor); } } } return(corridors); }
// START: /// <summary> /// Start this instance. /// </summary> public void Start() { // Get the components dungeonGenerator = GetComponent<DungeonGenerator> (); sceneryGenerator = GetComponent<SceneryGenerator> (); stairsGenerator = GetComponent<StairsGenerator> (); roomGenerator = GetComponent<RoomGenerator> (); perimeterGenerator = GetComponent<PerimeterGenerator> (); entranceGenerator = GetComponent<EntranceGenerator> (); corridorGenerator = GetComponent<CorridorGenerator> (); voidGenerator = GetComponent<VoidGenerator> (); pitGenerator = GetComponent<PitGenerator> (); chestGenerator = GetComponent<ChestGenerator> (); caveGenerator = GetComponent<CaveGenerator> (); terrainCarver = GetComponent<TerrainCarver> (); terrainGenerator = GetComponent<TerrainGenerator> (); textureGenerator = GetComponent<TerrainTextureGenerator> (); // Initialize the instances stairsGenerator.Start (); roomGenerator.Start (); perimeterGenerator.Start (); entranceGenerator.Start (); corridorGenerator.Start (); voidGenerator.Start (); pitGenerator.Start (); chestGenerator.Start (); caveGenerator.Start (); terrainCarver.Start (); terrainGenerator.Start (); textureGenerator.Start (); // Initialize the floor array allFloors = new Floor[dungeonGenerator.numFloors]; }
// Start is called before the first frame update void Start() { sceneCorridorGenerator = GameObject.FindGameObjectWithTag("CorridorGen"); corridorGenerator = sceneCorridorGenerator.GetComponent <CorridorGenerator>(); }
// START: /// <summary> /// Start this instance. /// </summary> public void Start() { // Get the dungeon generator component dungeonGenerator = GetComponent<DungeonGenerator> (); corridorGenerator = GetComponent<CorridorGenerator> (); }
private void Awake() { instance = this; }