private Vector2 FindSlopeDirection(int x, int y, Vector2 direction, float radius, bool trigo) { float angleDiff = 10f; float angle = -270f; if (!trigo) { angleDiff *= -1; angle *= -1; } float length = SLOPE_LENGTH * heightPointPerUnity; direction.Normalize(); direction = TerrainGenerator.RotateVector(direction, angle); while (Mathf.Abs(angle) <= 270) { direction = TerrainGenerator.RotateVector(direction, angleDiff); angle += angleDiff; Vector2 normal = TerrainGenerator.RotateVector(direction, 90); Vector2 nearL = -normal * radius + new Vector2(x, y) + direction * length; Vector2 nearR = normal * radius + new Vector2(x, y) + direction * length; Vector2 farL = nearL + radius * 2 * direction; Vector2 farR = nearR + radius * 2 * direction; if (FreePlace(nearL) && FreePlace(nearR) && FreePlace(farL) && FreePlace(farR)) { return(direction); } } return(Vector2.zero); }
// in height unity private void AddBranch(int x, int y, float minRadius, float maxRadius) { Vector2 direction = TerrainGenerator.Vector2FromAngle(Random.value * 360f); Vector2 position = new Vector2(x, y); float radius = (minRadius + maxRadius) / 2f; bool free = true; for (int i = 0; i < 30; i++) { direction = TerrainGenerator.RotateVector(direction, Random.value * 80f - 40f); ModifyRadius(ref radius, minRadius, maxRadius); position += direction * heightPointPerUnity; free = AddCircle((int)position.x, (int)position.y, radius, REQUIRED_FREE_SPACE_BRANCH); if (!free) { break; } } }
private void GenerateAccesses() { // clear the slope list slopes = new List <Slope>(); int[] xBorder = new int[6]; int[] yBorder = new int[6]; Vector2 direction = new Vector2(0, 1); // find 6 border points and find the farthest from the center float maxDist = 0; int maxI = 0; for (int i = 0; i < 6; i++) { FindBorderPoint(xLocalCenter, yLocalCenter, TerrainGenerator.RotateVector(direction, i * 60), out xBorder[i], out yBorder[i]); float dist = (xLocalCenter - xBorder[i]) * (xLocalCenter - xBorder[i]) + (yLocalCenter - yBorder[i]) * (yLocalCenter - yBorder[i]); if (dist > maxDist) { maxDist = dist; maxI = i; } } CreateAccess(xBorder[maxI], yBorder[maxI], TerrainGenerator.RotateVector(direction, maxI * 60), 4f); // find farthest between the 3 opposite border points maxDist = 0; int newIStart = maxI + 2; for (int i = newIStart; i < newIStart + 3; i++) { float dist = (xLocalCenter - xBorder[i % 6]) * (xLocalCenter - xBorder[i % 6]) + (yLocalCenter - yBorder[i % 6]) * (yLocalCenter - yBorder[i % 6]); if (dist > maxDist) { maxDist = dist; maxI = i % 6; } } CreateAccess(xBorder[maxI], yBorder[maxI], TerrainGenerator.RotateVector(direction, maxI * 60), 4f); }
// returns every point in the slope. The second vector2 contains downPosition (1 is top, 0 is bot) // and sidePosition (-1 is max right when going down, 0 center and 1 left) public Dictionary <Vector2Int, Vector2> GetPointList(float radiusExtension = 0, float topExtension = 0, float botExtension = 0) { float length = (botPoint - topPoint).magnitude; Vector2 direction = (botPoint - topPoint).normalized; Vector2 normal = TerrainGenerator.RotateVector(direction, 90); Vector2 topL = -normal * (radius + radiusExtension) + topPoint - direction * topExtension; Vector2 topR = normal * (radius + radiusExtension) + topPoint - direction * topExtension; Vector2 botL = -normal * (radius + radiusExtension) + botPoint + direction * botExtension; Vector2 botR = normal * (radius + radiusExtension) + botPoint + direction * botExtension; int minX = (int)Mathf.Min(topL.x, topR.x, botL.x, botR.x); int minY = (int)Mathf.Min(topL.y, topR.y, botL.y, botR.y); int maxX = (int)Mathf.Max(topL.x, topR.x, botL.x, botR.x) + 1; int maxY = (int)Mathf.Max(topL.y, topR.y, botL.y, botR.y) + 1; Dictionary <Vector2Int, Vector2> list = new Dictionary <Vector2Int, Vector2>(); // for each point in the area, if in slope, adjust height. for (int i = minX; i <= maxX; i++) { for (int j = minY; j <= maxY; j++) { Vector2 pointFromTop = new Vector2(i, j) - topPoint; float downDist = Vector2.Dot(pointFromTop, direction); float sideDist = Vector2.Dot(pointFromTop, normal); // if in slope if (downDist > -topExtension && downDist < length + botExtension && Mathf.Abs(sideDist) <= radius + radiusExtension) { list.Add(new Vector2Int(i, j), new Vector2((length - downDist) / length, sideDist)); } } } return(list); }