void FindCellLines() { foreach (LineSegment l in spanningTree) { GeneratorCell cellStart = GetCellByPoint(l.p0.Value.x, l.p0.Value.y); if (cellStart != null) { l.cellStart = cellStart; } else { Debug.LogError("Could not find cell start for " + l.p0.Value); } GeneratorCell cellEnd = GetCellByPoint(l.p1.Value.x, l.p1.Value.y); if (cellEnd != null) { l.cellEnd = cellEnd; } else { Debug.LogError("Could not find cell end for " + l.p1.Value); } } }
void FindPathRoomsBetweenMainRooms() { foreach (Path p in paths) { foreach (GeneratorCell c in cells) { if (!c.isMainRoom && !c.isPathRoom) { foreach (BlockPath bp in p.path) { if (LineRectangleInteresection(bp, c)) { c.isPathRoom = true; break; } } } } } int c_index = 0; while (c_index < cells.Count) { GeneratorCell c = cells[c_index]; if (c.isMainRoom || c.isPathRoom) { maxX = Mathf.Max(c.posX + c.width, maxX); maxY = Mathf.Max(c.posY + c.height, maxY); minX = Mathf.Min(c.posX, minX); minY = Mathf.Min(c.posY, minY); c_index++; } else { cells.Remove(c); } } foreach (GeneratorCell c in cells) { c.posX += Mathf.CeilToInt(Mathf.Abs(minX)); c.posY += Mathf.CeilToInt(Mathf.Abs(minY)); maxX = Mathf.Max(c.posX, c.width, maxX); maxY = Mathf.Max(c.posY + c.height, maxY); } foreach (Path p in paths) { foreach (BlockPath bp in p.path) { bp.start.x += Mathf.Abs(minX); bp.start.y += Mathf.Abs(minY); bp.end.x += Mathf.Abs(minX); bp.end.y += Mathf.Abs(minY); } } }
void SeparateCells() { bool cellCollision = true; int loop = 0; while (cellCollision) { loop++; cellCollision = false; if (debug) { // Debug.Log("Loop " + loop); } for (int i = 0; i < cells.Count; i++) { GeneratorCell c = cells[i]; for (int j = i + 1; j < cells.Count; j++) { GeneratorCell cb = cells[j]; if (c.CollidesWith(cb)) { cellCollision = true; int cb_x = Mathf.RoundToInt((c.x + c.width) - cb.x); int c_x = Mathf.RoundToInt((cb.x + cb.width) - c.x); int cb_y = Mathf.RoundToInt((c.y + c.height) - cb.y); int c_y = Mathf.RoundToInt((cb.y + cb.height) - c.y); if (c_x < cb_x) { if (c_x < c_y) { c.Shift(c_x, 0); } else { c.Shift(0, c_y); } } else { if (cb_x < cb_y) { cb.Shift(cb_x, 0); } else { cb.Shift(0, cb_y); } } } } } } }
void SeparateCells() { bool cellCollision = true; int loop = 0; while (cellCollision) { loop++; cellCollision = false; for (int i = 0; i < cells.Count; i++) { GeneratorCell cell = cells[i]; ///Check all the other cells in the list if it's colliding with our cell for (int j = i + 1; j < cells.Count; j++) { GeneratorCell cell2 = cells[j]; if (cell.CollidesWith(cell2)) { cellCollision = true; int cell2XOverlap = Mathf.RoundToInt((cell.posX + cell.width) - cell2.posX); int cellXOverlap = Mathf.RoundToInt((cell2.posX + cell2.width) - cell.posX); int cell2YOverlap = Mathf.RoundToInt((cell.posY + cell.height) - cell2.posY); int cellYOverlap = Mathf.RoundToInt((cell2.posY + cell2.height) - cell.posY); //If the cells are colliding. Move the one on the left. if (cellXOverlap < cell2XOverlap) { //Move horizontally if distance from the origin has more bias towards Y. //Otherwise, Move Vertically if (cellXOverlap < cellYOverlap) { cell.OffsetPosition(cellXOverlap, 0); } else { cell.OffsetPosition(0, cellYOverlap); } } else { if (cell2XOverlap < cell2YOverlap) { cell2.OffsetPosition(cell2XOverlap, 0); } else { cell2.OffsetPosition(0, cell2YOverlap); } } } } } } }
public bool CollidesWith(GeneratorCell cell) { bool retVal = true; if (cell.posX >= this.posX + this.width || cell.posY >= this.posY + this.height || cell.posX + cell.width <= this.posX || cell.posY + cell.height <= this.posY) { retVal = false; } return(retVal); }
public bool CollidesWith(GeneratorCell cell) { bool retVal = true; if (cell.x >= this.x + this.width || cell.y >= this.y + this.height || cell.x + cell.width <= this.x || cell.y + cell.height <= this.y) { retVal = false; } return(retVal); }
/// <summary> /// Finds a cell at the given point. /// </summary> /// <param name="x"></param> /// <param name="y"></param> /// <returns></returns> GeneratorCell GetCellByPoint(float x, float y) { GeneratorCell retCell = null; foreach (GeneratorCell c in cells) { if (c.posX < x && c.posY < y && c.posX + c.width > x && c.posY + c.height > y) { retCell = c; break; } } return(retCell); }
bool LineRectangleInteresection(BlockPath line, GeneratorCell rect) { bool retVal = false; Vector2 intersection; BlockPath topLine = new BlockPath(); topLine.start = new Vector2(rect.posX, rect.posY + rect.height); topLine.end = new Vector2(rect.posX + rect.width, rect.posY + rect.height); if (LineIntersects(line.start, line.end, topLine.start, topLine.end, out intersection)) { retVal = true; } BlockPath rightLine = new BlockPath(); rightLine.start = new Vector2(rect.posX + rect.width, rect.posY + rect.height); rightLine.end = new Vector2(rect.posX + rect.width, rect.posY); if (LineIntersects(line.start, line.end, rightLine.start, rightLine.end, out intersection)) { retVal = true; } BlockPath bottomLine = new BlockPath(); bottomLine.start = new Vector2(rect.posX, rect.posY); bottomLine.end = new Vector2(rect.posX + rect.width, rect.posY); if (LineIntersects(line.start, line.end, bottomLine.start, bottomLine.end, out intersection)) { retVal = true; } BlockPath leftLine = new BlockPath(); leftLine.start = new Vector2(rect.posX, rect.posY); leftLine.end = new Vector2(rect.posX, rect.posY + rect.height); if (LineIntersects(line.start, line.end, leftLine.start, leftLine.end, out intersection)) { retVal = true; } return(retVal); }
void CreateCells() { RandomFromDistribution.ConfidenceLevel_e conf_level = RandomFromDistribution.ConfidenceLevel_e._80; int numberOfCells = levelStats.numberOfCells; float roomCircleRadius = levelStats.roomCircleRadius; percFromGraphToPaths = levelStats.percFromGraphToPaths; mainRoomMeanCutoff = levelStats.mainRoomCutoff; float cellMinWidth = levelStats.cellMinWidth; float cellMaxWidth = levelStats.cellMaxWidth; float cellMinHeight = levelStats.cellMinHeight; float cellMaxHeight = levelStats.cellMaxHeight; for (int i = 0; i < numberOfCells; i++) { float minWidthScalar = cellMinWidth; float maxWidthScalar = cellMaxWidth; float minHeightScalar = cellMinHeight; float maxHeightScalar = cellMaxHeight; GeneratorCell cell = new GeneratorCell(); cell.width = Mathf.RoundToInt(RandomFromDistribution.RandomRangeNormalDistribution(minWidthScalar, maxWidthScalar, conf_level)); cell.height = Mathf.RoundToInt(RandomFromDistribution.RandomRangeNormalDistribution(minHeightScalar, maxHeightScalar, conf_level)); Vector2 pos = GetRandomPointInCirlce(roomCircleRadius); cell.posX = Mathf.RoundToInt(pos.x); cell.posY = Mathf.RoundToInt(pos.y); cell.index = i; cells.Add(cell); widthAvg += cell.width; heightAvg += cell.height; } widthAvg /= cells.Count; heightAvg /= cells.Count; }