public void CreateLinkBetweenNodes(FloorNode a, FloorNode b, FloorNode.directionEnum directionFromAToB) { //if both nodes have no neighbour at the chosen direction if (a.GetNeighbourNode(directionFromAToB) == null && b.GetNeighbourNode(GetOppositeDirection(directionFromAToB)) == null) { //add the new room as a neighbour of the existing room a.SetNeighbourNode(directionFromAToB, b); //add the existing room as a neighbour of the new room b.SetNeighbourNode(GetOppositeDirection(directionFromAToB), a); } }
private void CreateRandomNode(FloorNode.roomTypeEnum roomType) { bool canBeCreated = false; //debug count int count = 0; while (!canBeCreated && count < 1000) { count++; //take an existing room FloorNode existingNode = nodeList[Random.Range(0, nodeList.Count)]; //choose a random direction var randomDirection = (FloorNode.directionEnum)Random.Range(0, 4); //set the new room coord based on the existing room we used to place it (int x, int y) = existingNode.GetCoord(); if (randomDirection == FloorNode.directionEnum.east) { x += 1; } if (randomDirection == FloorNode.directionEnum.west) { x -= 1; } if (randomDirection == FloorNode.directionEnum.north) { y += 1; } if (randomDirection == FloorNode.directionEnum.south) { y -= 1; } //if the existing room is a regular room, is not occupied, has no room at the direction chosen //and does not exceed one of the maximum coordinate, we create the room there if (existingNode.GetNeighbourNode(randomDirection) == null && !isTileOccupied(x, y) && existingNode.GetRoomType() == FloorNode.roomTypeEnum.regular && x < 4 && x > -4 && y < 4 && y > -4) { FloorNode newNode = new FloorNode(); newNode.SetRoomType(roomType); //connect the new room with the existing room CreateLinkBetweenNodes(existingNode, newNode, randomDirection); newNode.SetCoord(x, y); if (existingNode.GetRoomType() == FloorNode.roomTypeEnum.regular) { //check if there are rooms existing around the new room and connect them. //This allow the level to have loops and not look like a tree ConnectNodeNeighbours(newNode); } nodeList.Add(newNode); canBeCreated = true; } } }