Esempio n. 1
0
    public void CreateHallwayRooms(List <Room> rooms, List <Room> relevantRooms)
    {
        var toEvict = new List <Room>();

        foreach (var room in rooms)
        {
            if (relevantRooms.Contains(room))
            {
                continue;
            }

            if (GridFiller.HasLinkedCorridor(grid, room))
            {
                GridFiller.FillRoom(grid, room);
            }
            else
            {
                toEvict.Add(room);
            }
        }

        rooms.RemoveAll(room => toEvict.Contains(room));
    }
Esempio n. 2
0
    void Start()
    {
        var watch = new Stopwatch();

        watch.Start();

        rand = new Random();

        Color backgroundColor;

        ColorUtility.TryParseHtmlString("#2F4F4F", out backgroundColor);

        var backgroundDummyRoom = GameObject.CreatePrimitive(PrimitiveType.Quad);

        backgroundDummyRoom.transform.position   = new Vector3(0, 0, 1);
        backgroundDummyRoom.transform.localScale = new Vector3(mapWidth, mapHeight, 1);
        backgroundDummyRoom.GetComponent <MeshRenderer>().material.color = backgroundColor;

        rooms = new List <Room>();
        var relevantRooms = new List <Room>();

        for (var i = 0; i < mainRooms; i++)
        {
            TryCreateRoom(1.25f, rooms);
        }

        relevantRooms.AddRange(rooms);
        for (var i = 0; i < roomAmount.Random - mainRooms; i++)
        {
            TryCreateRoom(1, rooms);
        }

        var query = rooms.Where(room =>
                                (room.Size.x > relevantRoomFactor * roomWidth.Average ||
                                 room.Size.y > relevantRoomFactor * roomHeight.Average) && !relevantRooms.Contains(room))
                    .ToList();

        relevantRooms.AddRange(query);

        var polygon = new Polygon();

        foreach (var room in relevantRooms)
        {
            room.IsMainRoom = true;
            var x = room.GetLeft() + (room.GetRight() - room.GetLeft()) / 2;
            var y = room.GetTop() + (room.GetBottom() - room.GetTop()) / 2;
            polygon.Add(new Vertex(x, y));
        }

        Debug.Log("Relevant rooms: " + relevantRooms.Count);
        var options =
            new ConstraintOptions {
            ConformingDelaunay = true, SegmentSplitting = 1
        };
        var mesh = (Mesh)polygon.Triangulate(options);

        graph = new Graph <Vector2>(mesh.Vertices.Select(vertex => vertex.toVec2()).ToList());

        foreach (var edge in mesh.Edges)
        {
            var v0   = mesh.vertices[edge.P0].toVec2();
            var v1   = mesh.vertices[edge.P1].toVec2();
            var dist = (int)(v0 - v1).magnitude;

            graph.SetEdge(v0, v1, dist);
        }

        var prim = new Prim <Vector2>();

        tree = new Dictionary <Graph <Vector2> .Node, Graph <Vector2> .Node>();
        var mainRoomPos = new Vector2(rooms[0].GetLeft() + (rooms[0].GetRight() - rooms[0].GetLeft()) / 2,
                                      rooms[0].GetTop() + (rooms[0].GetBottom() - rooms[0].GetTop()) / 2);

        prim.prim(graph, graph.FindVertex(mainRoomPos), ref tree);

        finalGraph = new Graph <Vector2>(Enumerable.Empty <Vector2>());
        foreach (var pair in tree)
        {
            if (!finalGraph.Contains(pair.Key.context))
            {
                finalGraph.AddVertex(pair.Key.context);
            }
            if (!finalGraph.Contains(pair.Value.context))
            {
                finalGraph.AddVertex(pair.Value.context);
            }
            finalGraph.SetEdge(pair.Key.context, pair.Value.context, 0);
        }

        foreach (var edge in mesh.Edges)
        {
            var v0 = graph.FindVertex(mesh.vertices[edge.P0].toVec2());
            var v1 = graph.FindVertex(mesh.vertices[edge.P1].toVec2());

            if (finalGraph.Contains(v0) &&
                finalGraph.FindVertex(v0.context).edges.Any(subEdge => subEdge.to.context.Equals(v1.context)))
            {
                continue;
            }
            if (rand.NextDouble() > 0.25)
            {
                continue;
            }
            if (!finalGraph.Contains(v0.context))
            {
                finalGraph.AddVertex(v0.context);
            }
            if (!finalGraph.Contains(v1.context))
            {
                finalGraph.AddVertex(v1.context);
            }

            finalGraph.SetEdge(v0.context, v1.context, 0);
        }

        grid = new MapGrid(mapWidth, mapHeight);

        foreach (var room in relevantRooms)
        {
            GridFiller.FillRoom(grid, room);
        }

        CreateCorridors(relevantRooms);
        CreateHallwayRooms(rooms, relevantRooms);

        SelectSpawnAndBossRoom(relevantRooms);

        watch.Stop();
        Debug.Log("Generated in " + watch.ElapsedMilliseconds + " ms");
        //Do generation
        Generate();
    }