private SectionSpawner GetSectionSpawner(SectionSpawner originalSpawner, Direction dir) { Vector2Int coordinates = originalSpawner.coordinates; switch (dir) { case Direction.Left: coordinates += new Vector2Int(-1, 0); break; case Direction.Right: coordinates += new Vector2Int(1, 0); break; case Direction.Up: coordinates += new Vector2Int(0, 1); break; case Direction.Down: coordinates += new Vector2Int(0, -1); break; } if (CheckIfWithinMapLimit(coordinates)) { return(mapSectionSpawners[coordinates.x, coordinates.y]); } else { return(null); } }
private void Awake() { sectionSpawner = FindObjectOfType <SectionSpawner>(); lobbyEcsController = FindObjectOfType <LobbyEcsController>(); resourcesAccrualSceneManager = FindObjectOfType <ResourcesAccrualSceneManager>(); insufficientResourceErrorHandler = FindObjectOfType <InsufficientResourceErrorHandler>(); }
/// <summary> /// Adds all directions which can be used to create paths into other mapsections, but not into the end or start-area /// </summary> private List <Direction> GetAllowedSideRoomDirections(SectionSpawner startSection) { List <Direction> possibleDirections = new List <Direction>(); //UP Direction testingDirection = Direction.Up; SectionSpawner spawnerToTest = GetSectionSpawner(startSection, testingDirection); if (spawnerToTest != null) { if (spawnerToTest.sectionType == SectionSpawner.SectionType.NormalSection || spawnerToTest.sectionType == SectionSpawner.SectionType.Free) { possibleDirections.Add(testingDirection); } } //RIGHT testingDirection = Direction.Right; spawnerToTest = GetSectionSpawner(startSection, testingDirection); if (spawnerToTest != null) { if (spawnerToTest.sectionType == SectionSpawner.SectionType.NormalSection || spawnerToTest.sectionType == SectionSpawner.SectionType.Free) { possibleDirections.Add(testingDirection); } } //DOWN testingDirection = Direction.Down; spawnerToTest = GetSectionSpawner(startSection, testingDirection); if (spawnerToTest != null) { if (spawnerToTest.sectionType == SectionSpawner.SectionType.NormalSection || spawnerToTest.sectionType == SectionSpawner.SectionType.Free) { possibleDirections.Add(testingDirection); } } //LEFT testingDirection = Direction.Left; spawnerToTest = GetSectionSpawner(startSection, testingDirection); if (spawnerToTest != null) { if (spawnerToTest.sectionType == SectionSpawner.SectionType.NormalSection || spawnerToTest.sectionType == SectionSpawner.SectionType.Free) { possibleDirections.Add(testingDirection); } } return(possibleDirections); }
/// <summary> /// Adds opening to siderooms through recursive methods /// </summary> private void SetUpSideSections(SectionSpawner startSpawner) { List <Direction> possibleDirections = GetAllowedSideRoomDirections(startSpawner); for (int i = 0; i < possibleDirections.Count; i++) { int rng = Random.Range(0, 100); if (rng < extraAreaSpawnChance) { Direction newDir = possibleDirections[Random.Range(0, possibleDirections.Count)]; startSpawner.AddOpenDirection(newDir); SectionSpawner newSpawner = GetSectionSpawner(startSpawner, newDir); newSpawner.AddOpenDirection(GetOppositeDirection(newDir)); if (newSpawner.sectionType == SectionSpawner.SectionType.Free) { newSpawner.sectionType = SectionSpawner.SectionType.NormalSection; SetUpSideSections(newSpawner); } } } }
public void GenerateLevel(int criticalPathLength) { mapSectionSpawners = new SectionSpawner[mapSectionsVectorLimit, mapSectionsVectorLimit]; for (int x = 0; x < mapSectionSpawners.GetLength(0); x++) { for (int y = 0; y < mapSectionSpawners.GetLength(1); y++) { mapSectionSpawners[x, y] = new SectionSpawner(); mapSectionSpawners[x, y].spawnPos = new Vector2(this.transform.position.x + (x * sectionSize), this.transform.position.y + (y * sectionSize)); mapSectionSpawners[x, y].coordinates = new Vector2Int(x, y); } } //Start //Setup Critical path Vector2Int startCoordinates = new Vector2Int(Mathf.RoundToInt(mapSectionsVectorLimit / 2), Mathf.RoundToInt(mapSectionsVectorLimit / 2)); SetUpCriticalPath(startCoordinates, criticalPathLength, SectionSpawner.SectionType.StartSection); //Setup Extra Sections for (int i = 0; i < criticalPathSpawners.Count; i++) { if (criticalPathSpawners[i].sectionType == SectionSpawner.SectionType.NormalSection) { SetUpSideSections(criticalPathSpawners[i]); } } //Spawn Rooms foreach (var mapSpawner in mapSectionSpawners) { GameObject spawnedObject = null; switch (mapSpawner.sectionType) { case SectionSpawner.SectionType.Free: break; case SectionSpawner.SectionType.StartSection: spawnedObject = Instantiate(FindSectionWithMatchingOpenings(mapSpawner.GetOpenDirections(), startSectionBlueprints)); spawnedObject.transform.position = mapSpawner.spawnPos; break; case SectionSpawner.SectionType.NormalSection: spawnedObject = Instantiate(FindSectionWithMatchingOpenings(mapSpawner.GetOpenDirections(), normalSectionBlueprints)); spawnedObject.transform.position = mapSpawner.spawnPos; break; case SectionSpawner.SectionType.EndSection: spawnedObject = Instantiate(FindSectionWithMatchingOpenings(mapSpawner.GetOpenDirections(), endSectionBlueprints)); spawnedObject.transform.position = mapSpawner.spawnPos; break; } if (spawnedObject != null) { spawnedObject.name = mapSpawner.coordinates.ToString() + " " + spawnedObject.name + " " + mapSpawner.sectionType.ToString(); } } GameObject playerObject = GameObject.FindGameObjectWithTag("Player"); playerObject.transform.position = new Vector2(startCoordinates.x * sectionSize, startCoordinates.y * sectionSize); }
private void SetUpCriticalPath(Vector2Int mapSectionIndex, int roomsToSpawn, SectionSpawner.SectionType sectionType) { //If at the last section, end here if (sectionType == SectionSpawner.SectionType.EndSection) { SectionSpawner sectionSpawner = mapSectionSpawners[mapSectionIndex.x, mapSectionIndex.y]; //Set type of section sectionSpawner.sectionType = sectionType; } else { //Test to add a new opening to next area Direction inDirection = Direction.Left; Direction outDirection = (Direction)Random.Range(0, 4); Vector2Int testIndex = new Vector2Int(mapSectionIndex.x, mapSectionIndex.y); switch (outDirection) { case Direction.Left: testIndex += new Vector2Int(-1, 0); inDirection = Direction.Right; break; case Direction.Right: testIndex += new Vector2Int(1, 0); inDirection = Direction.Left; break; case Direction.Up: testIndex += new Vector2Int(0, 1); inDirection = Direction.Down; break; case Direction.Down: testIndex += new Vector2Int(0, -1); inDirection = Direction.Up; break; } if (CheckValidAndFreeSectionIndex(testIndex)) { SectionSpawner sectionSpawner = mapSectionSpawners[mapSectionIndex.x, mapSectionIndex.y]; //Set type of section sectionSpawner.sectionType = sectionType; //Add out direction to this section sectionSpawner.AddOpenDirection(outDirection); //Add to critical path spawner list criticalPathSpawners.Add(sectionSpawner); //Switch active index mapSectionIndex = testIndex; //Add in direction to the next section mapSectionSpawners[mapSectionIndex.x, mapSectionIndex.y].AddOpenDirection(inDirection); roomsToSpawn--; switch (sectionType) { case SectionSpawner.SectionType.Free: Debug.LogError("Not possible spawn"); break; case SectionSpawner.SectionType.StartSection: sectionType = SectionSpawner.SectionType.NormalSection; break; case SectionSpawner.SectionType.NormalSection: if (roomsToSpawn == 1) { sectionType = SectionSpawner.SectionType.EndSection; } break; case SectionSpawner.SectionType.EndSection: return; } //Recursion SetUpCriticalPath(mapSectionIndex, roomsToSpawn, sectionType); } else { //Re-Do SetUpCriticalPath(mapSectionIndex, roomsToSpawn, sectionType); } } }