public override void Generate(BoardGenerator boardGenerator) { GridPosition startPos = null; if (useRandomTunnelStartPositions) { startPos = boardGenerator.GetRandomGridPosition(); for (int i = 0; i < numTunnels; i++) { GridPosition randomGoalPosition = boardGenerator.GetRandomGridPosition(); if (spawnRoomsAtTunnelEnds) { SpawnRoomTemplateAtTunnelEnd(boardGenerator, randomGoalPosition); } DigTunnel(boardGenerator, startPos, randomGoalPosition); } } else if (connectLastStrataLayer) { List <GridPosition> goalPositions = BuildTunnelGoalList(boardGenerator); for (int i = 0; i < goalPositions.Count; i++) { startPos = goalPositions[i]; int loopingGoalPositionIndex = ((i + 1) % goalPositions.Count); GridPosition targetPosition = goalPositions[loopingGoalPositionIndex]; DigTunnel(boardGenerator, startPos, targetPosition); } } }
// Use this for initialization void Start() { counter = waveRate; num = 10; board = gameObject.GetComponent <BoardGenerator>(); tile = board.GetComponent <Tilemap>(); //Debug.Log(board); for (int i = 0; i < num; i++) { Vector2 pos = board.GridPositionToVector2(board.GetRandomGridPosition()); //Debug.Log(pos); Vector3Int p = new Vector3Int((int)(pos.x + 2), (int)pos.y + 2, 0); //tile.SetColor(p, Color.red); //Debug.Log(tile.GetColor(p)); Instantiate(enemy, p, Quaternion.identity); } for (int i = 0; i < 5; i++) { Vector2 pos = board.GridPositionToVector2(board.GetRandomGridPosition()); //Debug.Log(pos); Vector3Int p = new Vector3Int((int)(pos.x + 2), (int)pos.y + 2, 0); //tile.SetColor(p, Color.red); //Debug.Log(tile.GetColor(p)); Instantiate(powerpotion, p, Quaternion.identity); } }
int GetSurroundingWallCount(int gridX, int gridY, BoardGenerator boardGenerator) { int wallCount = 0; for (int neighbourX = gridX - 1; neighbourX <= gridX + 1; neighbourX++) { for (int neighbourY = gridY - 1; neighbourY <= gridY + 1; neighbourY++) { if (neighbourX >= 0 && neighbourX < boardGenerator.boardGenerationProfile.boardHorizontalSize && neighbourY >= 0 && neighbourY < boardGenerator.boardGenerationProfile.boardVerticalSize) { if (neighbourX != gridX || neighbourY != gridY) { if (boardGenerator.boardGridAsCharacters[neighbourX, neighbourY] == charForFill) { wallCount++; } } } else { wallCount++; } } } return(wallCount); }
void SpawnRoomTemplateAtTunnelEnd(BoardGenerator boardGenerator, GridPosition spawnPosition) { if (tunnelEndTemplates.Length > 0) { RoomTemplate templateToSpawn = tunnelEndTemplates[Random.Range(0, tunnelEndTemplates.Length)]; boardGenerator.DrawTemplate(spawnPosition.x, spawnPosition.y, templateToSpawn, overwriteFilledSpaces, true); } }
void GenerateMap(BoardGenerator boardGenerator) { RandomFillMap(boardGenerator); for (int i = 0; i < 5; i++) { SmoothMap(boardGenerator); } }
public override void Generate(BoardGenerator boardGenerator) { if (useLibraryDefaultEmptyCharForEmptySpace) { emptySpaceChar = boardGenerator.boardGenerationProfile.boardLibrary.GetDefaultEmptyChar(); } GenerateMap(boardGenerator); }
void RandomFillMap(BoardGenerator boardGenerator) { for (int x = 0; x < boardGenerator.boardGenerationProfile.boardHorizontalSize; x++) { for (int y = 0; y < boardGenerator.boardGenerationProfile.boardVerticalSize; y++) { boardGenerator.WriteToBoardGrid(x, y, (Random.Range(0, 100) < randomFillPercent) ? charForFill : emptySpaceChar, overwriteFilledSpaces, false); } } }
//public GameObject room = GameObject.Find("Room"); // Use this for initialization void Start() { board = gameObject.GetComponent <BoardGenerator>(); Debug.Log(board.name); GridPosition doory = board.GetRandomGridPosition(); Vector2 testy = new Vector2(5, 5); Vector2 pos = board.GetDoorPos(); Debug.Log(pos); Instantiate(door, pos, Quaternion.identity); }
private RoomChain SetupRoomChainComponent(BoardGenerator boardGenerator) { GameObject oldRoomChain = GameObject.Find("RoomChainHolder"); if (oldRoomChain != null) { DestroyImmediate(oldRoomChain); } RoomChain roomChainComponent = new GameObject("RoomChainHolder").AddComponent <RoomChain>(); return(roomChainComponent); }
public void DigTunnel(BoardGenerator boardGenerator, GridPosition startPosition, GridPosition tunnelGoal) { GridPosition currentDigPosition = startPosition; for (int i = 0; i < maxLengthPerTunnel; i++) { if (currentDigPosition.x < tunnelGoal.x) { currentDigPosition.x++; } else if (currentDigPosition.x > tunnelGoal.x) { currentDigPosition.x--; } else { if (spawnRoomsAtTunnelEnds) { SpawnRoomTemplateAtTunnelEnd(boardGenerator, currentDigPosition); } break; } for (int j = 0; j < tunnelWidth; j++) { boardGenerator.WriteToBoardGrid(currentDigPosition.x, currentDigPosition.y + j, GetCharToWriteForTunnel(boardGenerator), true, true); } } for (int k = 0; k < maxLengthPerTunnel; k++) { if (currentDigPosition.y < tunnelGoal.y) { currentDigPosition.y++; } else if (currentDigPosition.y > tunnelGoal.y) { currentDigPosition.y--; } else { if (spawnRoomsAtTunnelEnds) { SpawnRoomTemplateAtTunnelEnd(boardGenerator, currentDigPosition); } break; } for (int s = 0; s < tunnelWidth; s++) { boardGenerator.WriteToBoardGrid(currentDigPosition.x + s, currentDigPosition.y, GetCharToWriteForTunnel(boardGenerator), true, true); } } }
public void BuildBorder(BoardGenerator boardGenerator) { float leftEdgeX = -1f; float rightEdgeX = boardGenerator.boardGenerationProfile.boardHorizontalSize + 0f; float bottomEdgeY = -1f; float topEdgeY = boardGenerator.boardGenerationProfile.boardVerticalSize + 0f; InstantiateVerticalOuterWall(leftEdgeX, bottomEdgeY, topEdgeY, boardGenerator); InstantiateVerticalOuterWall(rightEdgeX, bottomEdgeY, topEdgeY, boardGenerator); InstantiateHorizontalOuterWall(leftEdgeX + 1f, rightEdgeX - 1f, bottomEdgeY, boardGenerator); InstantiateHorizontalOuterWall(leftEdgeX + 1f, rightEdgeX - 1f, topEdgeY, boardGenerator); }
char GetCharToWriteForTunnel(BoardGenerator boardGenerator) { char charToWrite; if (useCustomEmptySpaceCharForTunnels) { charToWrite = customEmptySpaceChar; } else { charToWrite = boardGenerator.boardGenerationProfile.boardLibrary.GetDefaultEmptyChar(); } return(charToWrite); }
public override void OnInspectorGUI() { DrawDefaultInspector(); BoardGenerator myScript = (BoardGenerator)target; if (GUILayout.Button("Clear And Generate")) { myScript.ClearAndRebuild(); } if (GUILayout.Button("Clear")) { myScript.ClearLevel(); } }
private void WriteFilledRooms(BoardGenerator boardGenerator, RoomChain roomChainComponent, int horizontalRoomsToFill, int verticalRoomsToFill, RoomTemplate[,] finalRoomGrid) { int roomChainNumber = 0; for (int x = 0; x < horizontalRoomsToFill; x++) { for (int y = 0; y < verticalRoomsToFill; y++) { Vector2 roomPos = new Vector2(x * roomSizeX, y * roomSizeY); RoomTemplate templateToWrite = finalRoomGrid[x, y]; if (templateToWrite != null) { roomChainNumber++; WriteChainRoomToGrid(boardGenerator, roomChainComponent, roomPos, templateToWrite, roomChainNumber, true); } } } }
public GameObject GenerateRoomPlaceHolderGameObject(BoardGenerator boardGenerator, Vector2 roomOrigin, RoomTemplate roomTemplate, int chainNumber, bool isOnPath, string namePrefix) { GameObject roomMarker; if (isOnPath) { roomMarker = new GameObject(namePrefix + "Path Room " + chainNumber + " " + roomTemplate.name); } else { roomMarker = new GameObject(namePrefix + "Random fill Room " + roomTemplate.name); } roomMarker.transform.position = roomOrigin; roomMarker.transform.SetParent(this.transform); return(roomMarker); }
void SmoothMap(BoardGenerator boardGenerator) { for (int x = 0; x < boardGenerator.boardGenerationProfile.boardHorizontalSize; x++) { for (int y = 0; y < boardGenerator.boardGenerationProfile.boardVerticalSize; y++) { int neighbourWallTiles = GetSurroundingWallCount(x, y, boardGenerator); if (neighbourWallTiles > 4) { boardGenerator.WriteToBoardGrid(x, y, charForFill, overwriteFilledSpaces, false); } else if (neighbourWallTiles < 4) { boardGenerator.WriteToBoardGrid(x, y, emptySpaceChar, overwriteFilledSpaces, false); } } } }
private List <GridPosition> BuildTunnelGoalList(BoardGenerator boardGenerator) { List <GridPosition> goalPositions = new List <GridPosition>(); for (int i = 0; i <= boardGenerator.currentGeneratorIndexIdForEmptySpaceTracking; i++) { for (int j = 0; j < numTunnels; j++) { if (boardGenerator.emptySpaceLists[i].gridPositionList.Count > 0) { int index = Random.Range(0, boardGenerator.emptySpaceLists[i].gridPositionList.Count); GridPosition emptyPosition = boardGenerator.emptySpaceLists[i].gridPositionList[index]; boardGenerator.emptySpaceLists[i].gridPositionList.RemoveAt(index); goalPositions.Add(emptyPosition); } } } return(goalPositions); }
public override void SpawnBoardSquare(BoardGenerator boardGenerator, Vector2 location, BoardLibraryEntry inputEntry) { if (inputEntry != null) { if (inputEntry.prefabToSpawn == null) { Vector3Int pos = new Vector3Int((int)location.x, (int)location.y, 0); boardGenerator.tilemap.SetTile(pos, inputEntry.tile); } else { Vector3Int pos = new Vector3Int((int)location.x, (int)location.y, 0); TileBase defaultTile = boardGenerator.boardGenerationProfile.boardLibrary.GetDefaultTile(); boardGenerator.tilemap.SetTile(pos, defaultTile); Instantiate(inputEntry.prefabToSpawn, location, Quaternion.identity); } } else { Debug.LogError("Returned null from library, something went wrong when trying to draw tiles."); } }
public override void Generate(BoardGenerator boardGenerator) { int horizontalRoomsToFill = boardGenerator.boardGenerationProfile.boardHorizontalSize / roomSizeX; int verticalRoomsToFill = boardGenerator.boardGenerationProfile.boardVerticalSize / roomSizeY; int generationAttempts = 0; bool generationSucceeded = false; while (!generationSucceeded) { generationAttempts++; RoomChain roomChainComponent = SetupRoomChainComponent(boardGenerator); RoomTemplate[,] roomTemplateGrid = new RoomTemplate[horizontalRoomsToFill, verticalRoomsToFill]; roomTemplateGrid = BuildRoomSequence(roomChainComponent, horizontalRoomsToFill, verticalRoomsToFill); if (roomTemplateGrid != null) { WriteFilledRooms(boardGenerator, roomChainComponent, horizontalRoomsToFill, verticalRoomsToFill, roomTemplateGrid); generationSucceeded = true; Destroy(roomChainComponent.gameObject); break; } else { roomChainComponent = SetupRoomChainComponent(boardGenerator); roomTemplateGrid = BuildRoomSequence(roomChainComponent, horizontalRoomsToFill, verticalRoomsToFill); } if (fillEmptySpaceWithRandomRooms) { FillUnusedSpaceWithRandomRooms(horizontalRoomsToFill, verticalRoomsToFill, roomTemplateGrid); } if (generationAttempts > maximumAttempts) { Debug.LogError("Generation failed after " + maximumAttempts + " try to tweak your parameters to create something more likely to succeed by lowering minimum generated rooms and raising chance to continue growing."); Destroy(roomChainComponent.gameObject); break; } } }
private void WriteChainRoomToGrid(BoardGenerator boardGenerator, RoomChain roomChainComponent, Vector2 roomOrigin, RoomTemplate roomTemplate, int chainNumber, bool isOnPath) { #if UNITY_EDITOR roomChainComponent.GenerateRoomPlaceHolderGameObject(boardGenerator, roomOrigin, roomTemplate, chainNumber, isOnPath, "Chain Room"); #endif int charIndex = 0; for (int i = 0; i < roomSizeX; i++) { for (int j = 0; j < roomSizeY; j++) { char selectedChar = roomTemplate.roomChars[charIndex]; if (selectedChar != '\0') { Vector2 spawnPos = new Vector2(i, j) + roomOrigin; int x = (int)spawnPos.x; int y = (int)spawnPos.y; boardGenerator.WriteToBoardGrid(x, y, selectedChar, overwriteFilledSpaces, isOnPath); } charIndex++; } } }
void InstantiateVerticalOuterWall(float xCoord, float startingY, float endingY, BoardGenerator boardGenerator) { float currentY = startingY; while (currentY <= endingY) { Vector2 spawnPos = new Vector2(xCoord, currentY); boardGenerator.CreateMapEntryFromGrid(borderChar, spawnPos); currentY++; } }
void InstantiateHorizontalOuterWall(float startingX, float endingX, float yCoord, BoardGenerator boardGenerator) { float currentX = startingX; while (currentX <= endingX) { Vector2 spawnPos = new Vector2(currentX, yCoord); boardGenerator.CreateMapEntryFromGrid(borderChar, spawnPos); currentX++; } }
public GridPosition GetRandomEmptyGridPositionFromLastEmptySpaceGeneratorInStack(BoardGenerator boardGenerator) { int genIndex = 0; for (int i = 0; i < boardGenerationProfile.generators.Length; i++) { if (boardGenerationProfile.generators[i].generatesEmptySpace) { genIndex = i; } } GridPosition randPosition = emptySpaceLists[genIndex].gridPositionList[UnityEngine.Random.Range(0, emptySpaceLists[genIndex].gridPositionList.Count)]; return(randPosition); }
public override void Generate(BoardGenerator boardGenerator) { BuildBorder(boardGenerator); }
public abstract void SpawnBoardSquare(BoardGenerator boardGenerator, Vector2 location, BoardLibraryEntry inputEntry);
//public GameObject room = GameObject.Find("Room"); // Use this for initialization void Start() { tilemap = GameObject.Find("Grid"); board = tilemap.GetComponentInChildren <BoardGenerator>(); }