//The CreateCell is only used at the start to setup the complete grid. //After that, you can just access the cell and open or close it //Special cell content should probably be added when creating "rooms". protected LDCell CreateCell(IntVector2 coordinates, Vector3 cellScale, bool open) { LDCell newCell = Instantiate(cellPrefab) as LDCell; newCell.SetOpen(open); newCell.transform.localScale = cellScale; cells[coordinates.x, coordinates.z] = newCell; newCell.name = "LD Cell " + coordinates.x + ", " + coordinates.z; newCell.coordinates = coordinates; newCell.transform.parent = transform; newCell.transform.localPosition = new Vector3(cellScale.x * (coordinates.x - size.x * 0.5f + 0.5f), cellScale.y * 0.5f, cellScale.z * (coordinates.z - size.z * 0.5f + 0.5f)); return(newCell); }
//This is the mainloop called by the GameManager virtual public IEnumerator Generate() { WaitForSeconds delay = new WaitForSeconds(generationStepDelay); Init(); //Note that the single-agent level digger does not actually have an agent. //It has separate variables for position, direction, etc. IntVector2 agentPosition; GridDirection agentDirection = GridDirections.RandomValue; if (fixStartPosition) { agentPosition = startPosition; } else { agentPosition = RandomCoordinates; } LDCell currCell = GetCell(agentPosition); int steps = 0; //Poor man's "Create First Spawn Room" if (startWithRoom) { rooms.Add(CreateRoom(agentPosition, RandomRoomSize)); currCell.Highlight(); steps++; yield return(delay); } //Main Loop float turnProb = changeDirectionProb; float roomProb = makeRoomProb; while (steps < maxNrSteps) { Debug.Log("Step " + steps); currCell.Highlight(); yield return(delay); currCell.UnHighlight(); DoNextGenerationStep(ref agentPosition, ref agentDirection, ref currCell, ref turnProb, ref roomProb); steps++; } //Poor man's "Create Second Spawn Room" if (endWithRoom) { rooms.Add(CreateRoom(agentPosition, RandomRoomSize)); } //Make everything static for navmesh generation foreach (LDCell cell in cells) { cell.gameObject.isStatic = true; } yield return(delay); }
virtual protected void DoNextGenerationStep(ref IntVector2 currentPosition, ref GridDirection currentDirection, ref LDCell currentCell, ref float turnProb, ref float roomProb) { //First we move the agent. //We change direction by chance or if the next position in the current direction is not in the level. if (Random.value < changeDirectionProb || !ContainsCoordinates(currentPosition + currentDirection.ToIntVector2())) { //Randomly move the agent to a new valid position in the level. GridDirection newDir = ChangeDirection(currentPosition, currentDirection); currentDirection = newDir; turnProb = changeDirectionProb; } else if (dynamicProbabilities) { turnProb += changeDirDelta; } //Now we now the next position! currentPosition += currentDirection.ToIntVector2(); //Make a room? if (Random.value < roomProb) { rooms.Add(CreateRoom(currentPosition, RandomRoomSize)); roomProb = makeRoomProb; } else { //else just open current cell currentCell = GetCell(currentPosition); if (!currentCell.IsOpen) { currentCell.SetOpen(true); } if (dynamicProbabilities) { roomProb += makeRoomDelta; } } }
public void Add(LDCell cell) { cell.room = this; cells.Add(cell); }