// Body of instructions to apply in every step public override DecoratorCellState[,] OnCellStep(int x, int y, DecoratorCellState[,] map) { if (map[x, y] == DecoratorCellState.False && x + 1 < map.GetLength(0) && y + 1 < map.GetLength(1) && x > 0 && y > 0) { int emptyNeighboursCells = checkNeighboursForBuildings(map, x, y); if (emptyNeighboursCells > 3 && B2BGUtils.GetRand01() < _decorationProbability) { map[x, y] = DecoratorCellState.Origin; map[x + 1, y] = DecoratorCellState.Fragment; map[x, y + 1] = DecoratorCellState.Fragment; map[x + 1, y + 1] = DecoratorCellState.Fragment; GameObject g = Instantiate(_buildings[Random.Range(0, _buildings.Length)]); GameObject parentCell = _terrainData[x, y]; Bounds bounds; if (g.GetComponent <Renderer>() == null) { bounds = g.GetComponentInChildren <Renderer>().bounds; } else { bounds = g.GetComponent <Renderer>().bounds; } g.transform.position = parentCell.transform.position; g.transform.position += new Vector3(1, 0, 1); g = RotateRandomNSEW(g); g.transform.SetParent(parentCell.transform); _decorationData[x, y] = g; } } return(map); }
//Returns the number of cells in a ring around (x,y) that are alive. private int countAliveNeighbours(DecoratorCellState[,] map, int x, int y) { int count = 0; for (int i = -1; i < 2; i++) { for (int j = -1; j < 2; j++) { int neighbour_x = x + i; int neighbour_y = y + j; //If we're looking at the middle point if (i == 0 && j == 0) { //Do nothing, we don't want to add ourselves in! } //In case the index we're looking at it off the edge of the map else if (neighbour_x < 0 || neighbour_y < 0 || neighbour_x >= map.GetLength(0) || neighbour_y >= map.GetLength(1)) { count = count + 1; } //Otherwise, a normal check of the neighbour else if (map[neighbour_x, neighbour_y] == DecoratorCellState.True) { count = count + 1; } } } return(count); }
public void StartWorldGeneration() { ResetBaseFloor(); DeleteCurrentModel(); _map = CleanMap(_map); CreateWorld(); }
public override DecoratorCellState[,] OnCellStep(int x, int y, DecoratorCellState[,] map) { if (map[x, y] == DecoratorCellState.False && x + 1 < map.GetLength(0) && y + 1 < map.GetLength(1) && x > 0 && y > 0) { if (B2BGUtils.GetRand01() < _decorationProbability) { map[x, y] = DecoratorCellState.Tree; GameObject parentCell = _terrainData[x, y]; GameObject g = Instantiate(_treesArray[Random.Range(0, _treesArray.Length)]); Bounds bounds; if (g.GetComponent <Renderer>() == null) { bounds = g.GetComponentInChildren <Renderer>().bounds; } else { bounds = g.GetComponent <Renderer>().bounds; } g.transform.position = parentCell.transform.position; g.transform.SetParent(parentCell.transform); _decorationData[x, y] = g; } } return(map); }
private void DecorateWorld() { // Decoration Steps for (int i = 0; i < _decorators.Length; i++) { _map = _decorators[i].CreateMap(_map); } //RotateBaseFloor(-45f); }
private void DebugLogMap(DecoratorCellState[,] map) { for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { Debug.Log(string.Format("On :{0} {1} the value is {2}", x, y, map[x, y])); } } }
// Private // Public // Methods public override DecoratorCellState[,] ApplyDecorationRules(DecoratorCellState[,] map) { // Road Map Logic map = ModMap(map, DiagonalValidation); map = EdgeRoads(map); map = EdgeRoads(map); map = EdgeRoads(map); map = EdgeRoads(map); return(map); }
protected virtual void DefaultMapLoop(DecoratorCellState[,] map, DefaultLoop callback = null) { for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { callback(i, j, map); } } }
public override DecoratorCellState[,] CreateMap(DecoratorCellState[,] mainMap) { SetDimensionsData(); DecoratorCellState[,] newMap = generateMap(mainMap); // Instancing DefaultMapLoop(newMap, OnStepLoopMap); return(newMap); }
private DecoratorCellState[,] EdgeRoads(DecoratorCellState[,] map) { // Variable that determines if bool onXAxis = B2BGUtils.GetRand01() > 0.5; int startEdge; do { startEdge = Random.Range(12, _selectableEdge); } while (onXAxis ? map[0, startEdge] != DecoratorCellState.True : map[startEdge, 0] != DecoratorCellState.True); for (int i = 0; i < _maxDirectionalSteps; i++) { if (onXAxis) { GameObject g; // Instantiate road stripe Chunk if (map[i, startEdge] == DecoratorCellState.RoadDiagonal) { g = Instantiate(_roadSmoothChunk); map[i, startEdge] = DecoratorCellState.RoadSmooth; } else { g = Instantiate(_roadStripeChunk); map[i, startEdge] = DecoratorCellState.RoadStripe; } g.transform.position = _gen.OriginPoint.position; Bounds b = g.GetComponentInChildren <Renderer>().bounds; g.transform.position += new Vector3(startEdge * b.size.x, 0.02f, i * b.size.z); g.transform.Rotate(new Vector3(0, -90, 0)); SaveToGroundData(startEdge, i, g); } else { GameObject g; // Instantiate road stripe Chunk if (map[startEdge, i] != DecoratorCellState.True) { g = Instantiate(_roadSmoothChunk); } else { g = Instantiate(_roadStripeChunk); } map[startEdge, i] = DecoratorCellState.RoadStripe; g.transform.position = _gen.OriginPoint.position; Bounds b = g.GetComponentInChildren <Renderer>().bounds; g.transform.position += new Vector3(i * b.size.x, 0.02f, startEdge * b.size.z); SaveToGroundData(startEdge, i, g); } } return(map); }
private DecoratorCellState[,] generateMap(DecoratorCellState[,] cellmap) { //Set up the map with random values cellmap = ApplyDecorationRules(cellmap); //And now run the simulation for a set number of steps for (int i = 0; i < numberOfSteps; i++) { cellmap = DoSimulationStep(cellmap); } return(cellmap); }
private DecoratorCellState[,] CleanMap(DecoratorCellState[,] map) { map = new DecoratorCellState[width, height]; for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { map[x, y] = DecoratorCellState.True; } } return(map); }
private DecoratorCellState[,] DoSimulationStep(DecoratorCellState[,] oldMap) { DecoratorCellState[,] newMap = new DecoratorCellState[width, height]; //Loop over each row and column of the map for (int x = 0; x < oldMap.GetLength(0); x++) { for (int y = 0; y < oldMap.GetLength(1); y++) { int nbs = countAliveNeighbours(oldMap, x, y); //The new value is based on our simulation rules //First, if a cell is alive but has too few neighbours, kill it. if (oldMap[x, y] == DecoratorCellState.True) { if (nbs < deathLimit) { newMap[x, y] = DecoratorCellState.False; } else { newMap[x, y] = DecoratorCellState.True; } } //Otherwise, if the cell is dead now, check if it has the right number of neighbours to be 'born' else { if (nbs > birthLimit) { newMap[x, y] = DecoratorCellState.True; } else { newMap[x, y] = DecoratorCellState.False; } } if (oldMap[x, y] == DecoratorCellState.RoadDiagonal || oldMap[x, y] == DecoratorCellState.RoadSmooth || oldMap[x, y] == DecoratorCellState.RoadStripe) { // Re write the new map with the road data newMap[x, y] = oldMap[x, y]; } } } return(newMap); }
public override DecoratorCellState[,] OnCellStep(int x, int y, DecoratorCellState[,] map) { if (Random.Range(0f, 1f) < chanceToStartAlive && map[x, y] == DecoratorCellState.True) { map[x, y] = DecoratorCellState.True; } else if (map[x, y] == DecoratorCellState.True) { map[x, y] = DecoratorCellState.False; } if (x <= 10 && y <= 10 || (x == y) || (((x > 0 && y > 0)) && x + 1 == y - 1) || (((x < map.GetLength(0) && y < map.GetLength(1))) && x - 1 == y + 1)) { map[x, y] = DecoratorCellState.True; } return(map); }
protected virtual DecoratorCellState[,] ModMap(DecoratorCellState[,] map, DecorationCellStep callback = null) { if (callback == null) { return(map); } else { for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { map = callback(i, j, map); } } return(map); } }
private int checkNeighboursForBuildings(DecoratorCellState[,] map, int x, int y) { int count = 0; for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { int neighbour_x = x + i; int neighbour_y = y + j; if (map[neighbour_x, neighbour_y] == DecoratorCellState.False) { count = count + 1; } } } return(count); }
private DecoratorCellState[,] DiagonalValidation(int x, int y, DecoratorCellState[,] map) { // Diagonal Validation if (x == y && x > 4 && y > 4) { // If we are in the diagonal map[x, y] = DecoratorCellState.RoadDiagonal; // Instantiate road stripe Chunk GameObject g = Instantiate(_roadStripeChunk); g.transform.position = _gen.OriginPoint.position; Bounds b = g.GetComponentInChildren <Renderer>().bounds; g.transform.position += new Vector3(x * b.size.x, 0.01f, y * b.size.z); g.transform.Rotate(new Vector3(0, -45, 0)); SaveToGroundData(x, y, g); } return(map); }
private void OnStepLoopMap(int x, int y, DecoratorCellState[,] map) { if (map[x, y] == DecoratorCellState.True) { GameObject g = Instantiate(_trueChunk); g.transform.position = _gen.OriginPoint.position; Bounds b = g.GetComponentInChildren <Renderer>().bounds; g.transform.position += new Vector3(y * b.size.z, 0, x * b.size.x); g.transform.SetParent(_gen.NavMeshFloor.transform); _gen.GroundCellsMap[x, y] = g; _decorationData[x, y] = g; } else if (map[x, y] == DecoratorCellState.False) { GameObject g = Instantiate(_falseChunk); g.transform.position = _gen.OriginPoint.position; Bounds b = g.GetComponentInChildren <Renderer>().bounds; g.transform.position += new Vector3(y * b.size.z, 0, x * b.size.x); g.transform.SetParent(_gen.NavMeshFloor.transform); _gen.GroundCellsMap[x, y] = g; _decorationData[x, y] = g; } }
public virtual DecoratorCellState[,] OnCellStep(int x, int y, DecoratorCellState[,] map) { return(map); }
public virtual DecoratorCellState[,] ApplyDecorationRules(DecoratorCellState[,] map) { return(ModMap(map, OnCellStep)); }
public virtual DecoratorCellState[,] CreateMap(DecoratorCellState[,] mainMap) { SetDimensionsData(); return(ApplyDecorationRules(mainMap)); }