void PlaceSelf() { List<List<Vector3>> floorParts = floor.GetPartialFloorRects(false).ToList(); List<Vector3> outerWall = floor.GetCircumferance(false).ToList(); int outerWallsLength = outerWall.Count; bool validated = false; while (!validated) { List<Vector3> compartment = floorParts[Random.Range(0, floorParts.Count)]; Vector3 v1 = compartment[1] - compartment[0]; Vector3 v2 = compartment[2] - compartment[1]; //Vector3 c = compartment.Aggregate(Vector3.zero, (sum, e) => sum + e) / compartment.Count; Vector3 pt = compartment[0] + Random.value * v1 + Random.value * v2; validated = true; for (int i=0; i<outerWallsLength; i++) { float d = ProcGenHelpers.GetMinDist(pt, outerWall[i], outerWall[(i + 1) % outerWallsLength]); if (d < proximityThreshold) { Debug.Log("Invalid"); validated = false; break; } } if (validated) { transform.position = pt; } } GetComponentInParent<RotateMe>().rotating = false; PlayerController.Instance.Spawn(transform); }
IEnumerator <WaitForSeconds> _Build() { //TODO: Best way to remove convex points //TODO: Instert point in next wall behind current rather than far wall //TODO: Have points of interest (non concave) and use for corners too? //TODO: Linear points to wall building. //TODO: Allow inner wall free building. gizmoWalls.Clear(); int rooms = Mathf.Clamp(Random.Range(1, 3) + Random.Range(1, 4) + Random.Range(2, 4), 4, 7); perimeter.Clear(); perimeter.AddRange(ProcGenHelpers.Simplify(floor.GetCircumferance(false).Select(v => transform.InverseTransformPoint(v)).ToList())); yield return(new WaitForSeconds(0.05f)); for (int i = 0, l = perimeter.Count; i < l; i++) { Vector3 pt = perimeter[i]; Vector3 rhs = pt - perimeter[(l + i - 1) % l]; Vector3 lhs = perimeter[(i + 1) % l] - pt; int rotation = ProcGenHelpers.XZRotation(lhs, rhs); if (rotation == -1) { //Debug.Log(string.Format("{0}: convex", i)); convex.Add(pt); } else if (rotation == 1) { //Debug.Log(string.Format("{0}: concave", i)); nonConcave.Add(pt); } } yield return(new WaitForSeconds(0.01f)); int attempts = 0; while (rooms > 0 && attempts < 300) { bool addedRoom = false; RoomBuildType nextRoom = NextRoomType; if (nextRoom == RoomBuildType.ConvexCornerToStraightToWall) { if (MapCornerStraightWall()) { rooms--; addedRoom = true; } } else if (nextRoom == RoomBuildType.ConvexCornerToConvexCorner) { if (MapCornerToCornerWall()) { rooms--; addedRoom = true; } } else if (nextRoom == RoomBuildType.WallStraightToWall) { if (MapWallToWall()) { rooms--; addedRoom = true; } } attempts++; yield return(new WaitForSeconds(addedRoom ? 0.05f : 0.01f)); } Debug.LogWarning(string.Format("Ended with remaining {0} rooms after {1} attempts", rooms, attempts)); foreach (List <Vector3> innerWall in wallLines) { ConstructInnerWall(innerWall); yield return(new WaitForSeconds(0.05f)); innerWall.Reverse(); ConstructInnerWall(innerWall); yield return(new WaitForSeconds(0.05f)); } mCol.sharedMesh = null; mCol.sharedMesh = mesh; generated = true; generating = false; }
IEnumerator <WaitForSeconds> _Build() { Vector3 up = new Vector3(0, height, 0); int corner = 0; int vertsPerWall = 2; while (true) { if (floor.Generated) { if (generated) { yield return(new WaitForSeconds(0.05f)); } else { if (!generating) { corners.AddRange(floor.GetCircumferance(false).Select(v => transform.InverseTransformPoint(v))); corner = -1; generating = true; //Debug.Log(string.Format("{0} Corners to make walls on", corners.Count)); } corner++; if (corner > corners.Count) { generated = true; } else { int nextCorner = (corner + 1) % corners.Count; if (corner == 0) { verts.Add(corners[corner]); verts.Add(corners[corner] + up); UVs.Add(new Vector2(corner % 2, 0)); UVs.Add(new Vector2(corner % 2, 1)); } if (nextCorner != 0) { verts.Add(corners[nextCorner]); verts.Add(corners[nextCorner] + up); UVs.Add(new Vector2(nextCorner % 2, 0)); UVs.Add(new Vector2(nextCorner % 2, 1)); } tris.Add(nextCorner * vertsPerWall); tris.Add(corner * vertsPerWall); tris.Add(corner * vertsPerWall + 1); tris.Add(nextCorner * vertsPerWall); tris.Add(corner * vertsPerWall + 1); tris.Add(nextCorner * vertsPerWall + 1); mesh.Clear(); mesh.SetVertices(verts); mesh.SetTriangles(tris, 0); mesh.SetUVs(0, UVs); mesh.RecalculateBounds(); mesh.RecalculateNormals(); mCol.sharedMesh = null; mCol.sharedMesh = mesh; yield return(new WaitForSeconds(delay)); } } } else { if (generated || generating) { generated = false; generating = false; verts.Clear(); UVs.Clear(); tris.Clear(); corners.Clear(); mesh.Clear(); mesh.SetVertices(verts); mesh.SetTriangles(tris, 0); mesh.SetUVs(0, UVs); mesh.RecalculateBounds(); mesh.RecalculateNormals(); mCol.sharedMesh = null; mCol.sharedMesh = mesh; } yield return(new WaitForSeconds(0.05f)); } } }