- Home /
Question by
TrivialRoo · Jun 20, 2015 at 03:16 AM ·
c#proceduralgenerationmodelingdungeon
How would I change my procedurally generated map change cells if it detects cells are around it?
Hi, so I have two models, an "intersection" piece, and a "straight line" piece. The straight line piece has walls.
I'm having trouble figuring out how to change the tiles into different tiles if the "cell" or tile detects other tiles already near it.
Here's my script:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Instance : MonoBehaviour {
public LevelCell cellPrefab;
private LevelCell[,] cells;
public IntVector2 size;
public LevelPassage[] passages;
public LevelCell GetCell (IntVector2 coordinates) {
return cells[coordinates.x, coordinates.z];
}
public void Generate() {
cells = new LevelCell[size.x, size.z];
List<LevelCell> activeCells = new List<LevelCell>();
DoFirstGenerationStep(activeCells);
while (activeCells.Count > 0) {
DoNextGenerationStep(activeCells);
}
if (this.transform.childCount <= 5) {
Generate ();
}
}
private void DoFirstGenerationStep (List<LevelCell> activeCells) {
activeCells.Add (CreateCell (RandomCoordinates));
}
private void DoNextGenerationStep (List<LevelCell> activeCells) {
int currentIndex = activeCells.Count - 1;
LevelCell currentCell = activeCells[currentIndex];
if (currentCell.IsFullyInitialized) {
activeCells.RemoveAt (currentIndex);
return;
}
LevelDirection direction = currentCell.RandomUninitializedDirection;
IntVector2 coordinates = currentCell.coordinates + direction.ToIntVector2();
if (ContainsCoordinates(coordinates) && GetCell(coordinates) == null) {
LevelCell neighbor = GetCell(coordinates);
if (neighbor == null) {
neighbor = CreateCell (coordinates);
CreatePassage(currentCell, neighbor, direction, 0);
activeCells.Add (neighbor);
}
else {
CreatePassage (currentCell, neighbor, direction, 1);
activeCells.RemoveAt (currentIndex);
}
}
else {
activeCells.RemoveAt (currentIndex);
}
}
private void CreatePassage(LevelCell cells, LevelCell otherCell, LevelDirection direction, int tileID) {
LevelPassage passage = Instantiate (passages[tileID]) as LevelPassage;
passage.Initialize (cells, otherCell, direction);
if (otherCell != null) {
passage = Instantiate (passages[1]) as LevelPassage;
passage.Initialize(otherCell, cells, direction.GetOpposite());
}
}
public IntVector2 RandomCoordinates {
get {
return new IntVector2(Random.Range (0, size.x), Random.Range (0, size.z));
}
}
public bool ContainsCoordinates(IntVector2 coordinate) {
return coordinate.x >= 0 && coordinate.x < size.x && coordinate.z >= 0 && coordinate.z < size.z;
}
private LevelCell CreateCell(IntVector2 coordinates) {
LevelCell newCell = Instantiate (cellPrefab) as LevelCell;
cells[coordinates.x, coordinates.z] = newCell;
newCell.coordinates = coordinates;
newCell.name = "Maze Cell " + coordinates.x + ", " + coordinates.z;
newCell.transform.parent = transform;
newCell.transform.localPosition = new Vector3(coordinates.x - size.x * 0.5f + 0.5f, 0f, coordinates.z - size.z * 0.5f + 0.5f);
return newCell;
}
}
Comment