private bool checkGridPointsAreType(int x, int y, int w, int h, GridPoint.Type type) { if (gridPoints == null) { return(false); } for (int j = y; j < y + h; j++) { // Check if position is within bounds of array if (j < 0 || j >= height) { return(false); } for (int i = x; i < x + w; i++) { if (i < 0 || i >= width) { return(false); } if (gridPoints[i, j].type != type) { return(false); } } } return(true); }
// Set a rectangle of points within a grid to a specific type private void setGridPointsFromRect(int x, int y, int w, int h, GridPoint.Type type) { if (gridPoints == null) { return; } for (int j = y; j < y + h; j++) { // Check if position is within bounds of array if (j < 0 || j >= height) { continue; } for (int i = x; i < x + w; i++) { if (i < 0 || i >= width) { continue; } gridPoints[i, j].type = type; } } }
private bool checkGridPointsAreTypeWorldPos(float x, float y, float w, float h, GridPoint.Type type) { int w_i = Mathf.CeilToInt(x + w) - Mathf.FloorToInt(x); int h_i = Mathf.CeilToInt(y + h) - Mathf.FloorToInt(y); int x_i = (int)(Mathf.FloorToInt(x) + width / 2f); int y_i = (int)(Mathf.FloorToInt(y) + height / 2f); return(checkGridPointsAreType(x_i, y_i, w_i, h_i, type)); }
private void setGridPointsFromRectWorldPos(float x, float y, float w, float h, GridPoint.Type type) { int w_i = Mathf.CeilToInt(x + w) - Mathf.FloorToInt(x); int h_i = Mathf.CeilToInt(y + h) - Mathf.FloorToInt(y); int x_i = (int)(Mathf.FloorToInt(x) + width / 2f); int y_i = (int)(Mathf.FloorToInt(y) + height / 2f); setGridPointsFromRect(x_i, y_i, w_i, h_i, type); }
private void setGridPointFromWorldPos(Vector3 worldPos, GridPoint.Type type) { int x = Mathf.FloorToInt(worldPos.x + width / 2f); int y = Mathf.FloorToInt(worldPos.z + height / 2f); if (x < 0 || x >= width || y < 0 || y >= height || gridPoints == null) { return; } gridPoints[x, y].type = type; }
public void Generate(bool randomSeed) { if (randomSeed) { seed = (int)System.DateTime.UtcNow.Ticks; // Get unique seed based on system time } Random.InitState(seed); MathHelper.stopwatch.Restart(); nodes = new List <BSPNode>(); BSPNode rootNode = new BSPNode(0, 0, width - borderSize * 2, height - borderSize * 2, minNodeSize); splitBSPNode(rootNode); gridPoints = new GridPoint[width, height]; for (int j = 0; j < height; j++) { for (int i = 0; i < width; i++) { GridPoint.Type type = GridPoint.Type.Grass; // Make sure nothing spawns right at the edge next to the wall if (i == 0 || i == width - 1 || j == 0 || j == height - 1) { type = GridPoint.Type.Obstacle; } gridPoints[i, j] = new GridPoint(i, j, type); } } Build(); // Set grid connections after placing objects so grid point types have been set for (int j = 0; j < height; j++) { for (int i = 0; i < width; i++) { setGridPointConnections(gridPoints[i, j]); } } roadGridPoints = GetGridPoints(GridPoint.Type.Path, GridPoint.Type.Pavement); MathHelper.stopwatch.Stop(); Debug.LogFormat("Generated town (seed={0}) with {1} BSP nodes and {2} objects in {3}ms", seed, nodes.Count, objectContainer.childCount, (float)MathHelper.stopwatch.ElapsedTicks / System.TimeSpan.TicksPerMillisecond); }