Exemplo n.º 1
0
    // Start is called before the first frame update
    void Start()
    {
        var SPoints = new List <Point2D>()
        {
            MakePoint(-20, -10), MakePoint(0, 30), MakePoint(20, -10)
        };
        var SuperTriangle = MakeTriangle(SPoints[0], SPoints[1], SPoints[2]);

        List <Point2D> ListOfPoints = new List <Point2D>();

        for (int i = 0; i < 75; i++)
        {
            ListOfPoints.Add(gameObject.MakeRandomPoint(Prefab));
        }

        DelaunayTriangulator      DelaOBJ   = new DelaunayTriangulator(Prefab, SPoints, ListOfPoints);
        HashSet <VirtualTriangle> Triangles = DelaOBJ.BowyerWatson();

        foreach (VirtualTriangle vtrs in Triangles)
        {
            if (!vtrs.ISPointExists(SPoints))
            {
                MakeTriangle(vtrs.Vertices[0], vtrs.Vertices[1], vtrs.Vertices[2]);
            }
        }
    }
    void DelaunayTriangulation(List <Point2D> RandomPoints)
    {
        List <Point2D> SPoints = new List <Point2D>()
        {
            MakePoint(-2100, -1100), MakePoint(0, 3100), MakePoint(2100, -1100)
        };
        DelaunayTriangulator      Triangulator = new DelaunayTriangulator(Prefab, SPoints, RandomPoints);
        HashSet <VirtualTriangle> Triangles    = Triangulator.BowyerWatson();

        foreach (VirtualTriangle tr in Triangles)
        {
            if (!tr.ISPointExists(SPoints))
            {
                MakeTriangle(tr.Vertices[0], tr.Vertices[1], tr.Vertices[2]);
            }
        }
    }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            var delaunay      = new DelaunayTriangulator();
            var points        = delaunay.GeneratePoints(2500, 512, 512);
            var triangulation = delaunay.BowyerWatson(points);

            newBitmap();
            var image = new Bitmap(Image.FromFile("map.png"));

            using (var g = Graphics.FromImage(image)) {
                foreach (var var in triangulation)
                {
                    g.DrawLines(White, var.Vertices.Select(var2 => new Point((int)var2.X, (int)var2.Y)).ToArray());
                }

                image.Save("map.png");
                image.Dispose();
                Console.WriteLine("world generated!");
            }
        }
Exemplo n.º 4
0
    void Start()
    {
        Cursor.lockState = CursorLockMode.None;
        Cursor.visible   = true;

        float minDistance             = pointPrefab.GetComponent <CircleCollider2D>().radius * 2;
        DelaunayTriangulator delaunay = new DelaunayTriangulator();
        var points        = delaunay.GeneratePoints(numberOfPoints, spawnArea, minDistance);
        var triangulation = delaunay.BowyerWatson(points);
        //remove junctions that are subjectively too long
        float maxDistanceBetweenPoints = Player.Instance.maxTravelDistance / 3;

        SpawnPOIs(pointPrefab, points, maxDistanceBetweenPoints, parentCanvas);
        Vector2 southernPoint = FindStartingPosition(points);

        playerToken.transform.SetPositionAndRotation(southernPoint, Quaternion.identity);
        playerToken.GetComponent <PlayerToken>().OnReachedDestination
            += GetComponent <RandomEventController>().StartEvent;

        OnPlayerReady?.Invoke(playerToken.transform.position);
    }
Exemplo n.º 5
0
    public Room GenerateConnections(List <Room> rooms)
    {
        var triangulator  = new DelaunayTriangulator();
        var roomMidpoints = new List <DelaunayVoronoi.Point>();
        var minX          = float.MaxValue;
        var minY          = float.MaxValue;
        var maxX          = float.MinValue;
        var maxY          = float.MinValue;

        foreach (var room in rooms)
        {
            var x = room.position.x;
            var y = room.position.y;

            minX = Mathf.Min(x, minX);
            minY = Mathf.Min(y, minY);
            maxX = Mathf.Max(x, maxX);
            maxY = Mathf.Max(y, maxY);

            roomMidpoints.Add(new DelaunayVoronoi.Point(room.position.x, room.position.y));
        }

        // Add buffer to the sides.
        minX -= 100;
        minY -= 100;
        maxX += 100;
        maxY += 100;
        minX  = Mathf.CeilToInt(minX);
        minY  = Mathf.CeilToInt(minY);
        maxX  = Mathf.CeilToInt(maxX);
        maxY  = Mathf.CeilToInt(maxY);

        this.minX = minX;
        this.minY = minY;
        this.maxX = maxX;
        this.maxY = maxY;

        var point0          = new DelaunayVoronoi.Point(minX, minY);
        var point1          = new DelaunayVoronoi.Point(minX, maxY);
        var point2          = new DelaunayVoronoi.Point(maxX, maxY);
        var point3          = new DelaunayVoronoi.Point(maxX, minY);
        var borderTriangle1 = new Triangle(point0, point1, point2);
        var borderTriangle2 = new Triangle(point0, point2, point3);

        triangulator.border = new List <Triangle>()
        {
            borderTriangle1, borderTriangle2
        };

        var delaunayTriangles = new List <Triangle>(triangulator.BowyerWatson(roomMidpoints));

        foreach (var triangle in delaunayTriangles)
        {
            // Check if this is actually part of the delaunay triangulation, or if it's a bordering region.
            var valid = true;
            foreach (var vertex in triangle.Vertices)
            {
                if (vertex.X == minX || vertex.X == maxX || vertex.Y == minY || vertex.Y == maxY)
                {
                    valid = false;
                    break;
                }
            }

            if (!valid)
            {
                continue;
            }

            var edges = new List <System.Tuple <DelaunayVoronoi.Point, DelaunayVoronoi.Point> >()
            {
                new System.Tuple <DelaunayVoronoi.Point, DelaunayVoronoi.Point>(triangle.Vertices[0], triangle.Vertices[1]),
                new System.Tuple <DelaunayVoronoi.Point, DelaunayVoronoi.Point>(triangle.Vertices[1], triangle.Vertices[2]),
                new System.Tuple <DelaunayVoronoi.Point, DelaunayVoronoi.Point>(triangle.Vertices[2], triangle.Vertices[0]),
            };

            foreach (var edge in edges)
            {
                var p1 = new Vector2((float)edge.Item1.X, (float)edge.Item1.Y);
                var p2 = new Vector2((float)edge.Item2.X, (float)edge.Item2.Y);

                var r1 = GetRoomByMidpoint(rooms, p1);
                var r2 = GetRoomByMidpoint(rooms, p2);
                if (r1 == null || r2 == null)
                {
                    continue;
                }
                r1.connectedRooms.Add(r2);
                r2.connectedRooms.Add(r1);
            }
        }

        Room spawnRoom           = null;
        var  spawnRoomDistanceSq = float.MaxValue;

        foreach (var room in rooms)
        {
            var distanceSq = room.position.x * room.position.x + room.position.y * room.position.y;
            if (distanceSq < spawnRoomDistanceSq)
            {
                spawnRoomDistanceSq = distanceSq;
                spawnRoom           = room;
            }
        }

        if (GeneratorDebugSettings.DebugStartingRoom)
        {
            Debug.DrawLine(new Vector3(spawnRoom.position.x, 0, spawnRoom.position.y),
                           new Vector3(spawnRoom.position.x, 5, spawnRoom.position.y),
                           Color.yellow, 100000f);
        }

        // Center room has no entrance; you spawn there.
        spawnRoom.DeleteConnectionsIntoSelf();

        // Remove as many connections as possible while preventing islands.
        spawnRoom.DeleteAsManyConnectionsAsPossible();

        // Remove any loops while preventing islands.
        spawnRoom.DeleteLoops();

        // Add some more connections randomly.
        var extraConnections = rooms.Count * 0.1;
        var madeConnections  = 0f;

        while (madeConnections < extraConnections)
        {
            var randomRoomA = rooms[Random.Range(0, rooms.Count - 1)];
            var randomRoomB = rooms[Random.Range(0, rooms.Count - 1)];
            if (randomRoomA == randomRoomB)
            {
                madeConnections += 0.1f;
                continue;
            }
            if (randomRoomA.connectedRooms.Contains(randomRoomB) &&
                randomRoomB.connectedRooms.Contains(randomRoomA))
            {
                madeConnections += 0.1f;
                continue;
            }
            if (randomRoomB == spawnRoom)
            {
                madeConnections += 0.1f;
                continue;
            }
            randomRoomA.connectedRooms.Add(randomRoomB);
            madeConnections++;
        }

        if (GeneratorDebugSettings.DebugGraph)
        {
            spawnRoom.DebugGraph();
        }

        foreach (var room in rooms)
        {
            room.AlignToHallwayGrid(hallwaySize, minX, minY);
            room.FinalizePosition();
        }

        return(spawnRoom);
    }