Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [SerializeField, Range(30, 250)]
- private static float size = 20;
- [SerializeField]
- private static string seed = "Empty";
- [SerializeField, Range(0, 0.35f)]
- private static float complexity = 0.1f;
- public static DestructablePolygonHandler GenerateRandomPolygon(/*float minX, float maxX, float minY, float maxY*/Vector2 desiredSize, float cellsPrUnit = 0.8f) {
- /* Heatmap<bool> grid = new Heatmap<bool>(
- new Rect(
- Random.Range(minX, maxX),
- Random.Range(minX, maxX),
- Random.Range(minY, maxY),
- Random.Range(minY, maxY)
- ),
- cellsPrUnit,
- () => false,
- null,
- null
- );*/
- /* for (int x = 0; x < grid.LengthX; x++) {
- for (int y = 0; y < grid.LengthY; y++) {
- grid[x, y] = true;
- }
- }*/
- Heatmap<bool> grid;
- while (true) {
- var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
- var random = new System.Random();
- seed = new string(Enumerable.Repeat(chars, 8).Select(s => s[random.Next(s.Length)]).ToArray());
- Vector2 startPos = FindIsland();
- List<Vector2> island = GetIsland(startPos).ToList();
- Rect rect = getBounds(island);
- for (int i = 0; i < island.Count; i++) {
- Vector2 vector2 = island[i];
- vector2.x += -1 * rect.x;
- vector2.y += -1 * rect.y;
- island[i] = vector2;
- }
- rect.x = 0;
- rect.y = 0;
- float scaleX = desiredSize.x / rect.width;
- float scaleY = desiredSize.y / rect.height;
- for (int i = 0; i < island.Count; i++) {
- Vector2 vector2 = island[i];
- island[i] = new Vector2(vector2.x * scaleX, vector2.y * scaleY);
- }
- rect.width = desiredSize.x;
- rect.height = desiredSize.y;
- grid = new Heatmap<bool>(new Rect(rect.x, rect.y, rect.width + 1, rect.height + 1),
- cellsPrUnit, () => false, null, null);
- foreach (Vector2 vector2 in island) {
- grid[Mathf.FloorToInt(vector2.x), Mathf.FloorToInt(vector2.y)] = true;
- }
- if (island.Count > 20) {
- break;
- }
- }
- return new DestructablePolygonHandler(grid);
- }
- private static Rect getBounds(List<Vector2> island) {
- float minX = float.MaxValue;
- float maxX = float.MinValue;
- float minY = float.MaxValue;
- float maxY = float.MinValue;
- foreach (Vector2 vector2 in island) {
- if (vector2.x < minX) {
- minX = vector2.x;
- }
- if (vector2.x > maxX) {
- maxX = vector2.x;
- }
- if (vector2.y < minY) {
- minY = vector2.y;
- }
- if (vector2.y > maxY) {
- maxY = vector2.y;
- }
- }
- /*Debug.Log(minX);
- Debug.Log(maxX);
- Debug.Log(minY);
- Debug.Log(maxY);*/
- Rect rect = new Rect(minX, minY, Mathf.Abs(minX) + Mathf.Abs(maxX), Mathf.Abs(minY) + Mathf.Abs(maxY));
- return rect;
- }
- private static bool runOnce = false;
- private static Vector2 FindIsland() {
- Vector2 point = new Vector2(0, 0);
- while (IsOnIsland(point) == false) {
- point.x++;
- point.y++;
- }
- return point;
- }
- private static bool IsOnIsland(Vector2 point) {
- return Noise.Generate(40 + point.x / size, (seed.GetHashCode() / 3.33f) % 30, 40 + point.y / size) > (0.35f - complexity);
- }
- private static HashSet<Vector2> GetIsland(Vector2 startPos) {
- HashSet<Vector2> visited = new HashSet<Vector2>();
- HashSet<Vector2> unvisited = new HashSet<Vector2>();
- unvisited.Add(startPos);
- do {
- Vector2 currentPoint = unvisited.First();
- unvisited.Remove(currentPoint);
- visited.Add(currentPoint);
- // Left
- Vector2 searchDirection = new Vector2(currentPoint.x - 1, currentPoint.y);
- if (!visited.Contains(searchDirection) && IsOnIsland(searchDirection)) unvisited.Add(searchDirection);
- // Up
- searchDirection = new Vector2(currentPoint.x, currentPoint.y + 1);
- if (!visited.Contains(searchDirection) && IsOnIsland(searchDirection)) unvisited.Add(searchDirection);
- // Right
- searchDirection = new Vector2(currentPoint.x + 1, currentPoint.y);
- if (!visited.Contains(searchDirection) && IsOnIsland(searchDirection)) unvisited.Add(searchDirection);
- // Down
- searchDirection = new Vector2(currentPoint.x, currentPoint.y - 1);
- if (!visited.Contains(searchDirection) && IsOnIsland(searchDirection)) unvisited.Add(searchDirection);
- } while (unvisited.Count != 0);
- return visited;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement