Пример #1
0
	// Use this for initialization
	void Start () 
	{
		CreateLineMaterial();
		
		mesh = new Mesh();
		Vertex2[] vertices = new Vertex2[NumberOfVertices];
		Vector3[] meshVerts = new Vector3[NumberOfVertices];
		int[] indices = new int[NumberOfVertices];
		
		Random.seed = 0;
		for (var i = 0; i < NumberOfVertices; i++)
		{
			vertices[i] = new Vertex2(size * Random.Range(-1.0f, 1.0f), size * Random.Range(-1.0f, 1.0f));
			meshVerts[i] = vertices[i].ToVector3();
			indices[i] = i;
		}
		
		mesh.vertices = meshVerts;
		mesh.SetIndices(indices, MeshTopology.Points, 0);
		//mesh.bounds = new Bounds(Vector3.zero, new Vector3((float)size,(float)size,(float)size));
		
		float now = Time.realtimeSinceStartup;
		voronoiMesh = VoronoiMesh.Create<Vertex2, Cell2>(vertices);
		float interval = Time.realtimeSinceStartup - now;

		Debug.Log("time = " + interval * 1000.0f);
		
	}
Пример #2
0
	// Use this for initialization
	void Start () 
	{
		CreateLineMaterial();

		mesh = new Mesh();
		Vertex2[] vertices = new Vertex2[NumberOfVertices];
		Vector3[] meshVerts = new Vector3[NumberOfVertices];
		int[] indices = new int[NumberOfVertices];

		Random.seed = 0;
		for (var i = 0; i < NumberOfVertices; i++)
		{
			vertices[i] = new Vertex2(size * Random.Range(-1.0f, 1.0f), size * Random.Range(-1.0f, 1.0f));
			meshVerts[i] = vertices[i].ToVector3();
			indices[i] = i;
		}

		mesh.vertices = meshVerts;
		mesh.SetIndices(indices, MeshTopology.Points, 0);
		//mesh.bounds = new Bounds(Vector3.zero, new Vector3((float)size,(float)size,(float)size));

		float now = Time.realtimeSinceStartup;
		ConvexHull<Vertex2, Face2> convexHull = ConvexHull.Create<Vertex2, Face2>(vertices);
		float interval = Time.realtimeSinceStartup - now;

		convexHullVertices = new List<Vertex2>(convexHull.Points);
		convexHullFaces = new List<Face2>(convexHull.Faces);

		Debug.Log("Out of the " + NumberOfVertices + " vertices, there are " + convexHullVertices.Count + " verts on the convex hull.");
		Debug.Log("time = " + interval * 1000.0f + " ms");

	}
Пример #3
0
    public Edge(Vertex2 start, Vertex2 end)
    {
        this.start = start;
        this.end = end;

        double width = Math.Abs (start.x - end.x);
        double height = Math.Abs (start.y - end.y);
        Size = (int)Math.Sqrt (Math.Pow (width, 2) + Math.Pow (height, 2));
    }
Пример #4
0
    public void AddPath(Edge e)
    {
        Vertex2 start = e.start;
        Vertex2 end = e.end;
        this.end = end;

        MarkRoom (map.GetRoom (start), PathGenerator.OPEN_NOT_ENDPOINT);
        DisableAll ();
        EnableRoom (map.GetRoom (end));
        CreatePath (start);
        EnableRoom (map.GetRoom (start));
        EnableAll ();
    }
Пример #5
0
    private List<PathNode> AllPotentialNeighbors(Vertex2 v)
    {
        List<PathNode> neighbors = new List<PathNode>();
        neighbors.Add (new PathNode(1, new Vertex2(v.x - 1, v.y)));
        neighbors.Add (new PathNode(1, new Vertex2(v.x + 1, v.y)));
        neighbors.Add (new PathNode(1, new Vertex2(v.x, v.y - 1)));
        neighbors.Add (new PathNode(1, new Vertex2(v.x, v.y + 1)));

        neighbors.Add (new PathNode(1.4, new Vertex2(v.x + 1, v.y + 1)));
        neighbors.Add (new PathNode(1.4, new Vertex2(v.x + 1, v.y - 1)));
        neighbors.Add (new PathNode(1.4, new Vertex2(v.x - 1, v.y + 1)));
        neighbors.Add (new PathNode(1.4, new Vertex2(v.x - 1, v.y - 1)));

        return neighbors;
    }
Пример #6
0
 private void EnablePoint(Vertex2 v)
 {
     tiles [v.y, v.x] = PathGenerator.OPEN_ENDPOINT;
 }
Пример #7
0
    private void CreatePath(Vertex2 start)
    {
        PriorityQueue frontier = new PriorityQueue ();
        frontier.Add (0, start);
        Dictionary<Vertex2, Vertex2> from = new Dictionary<Vertex2, Vertex2> ();
        Dictionary<Vertex2, double> cost = new Dictionary<Vertex2, double> ();
        cost [start] = 0;
        from [start] = null;
        Vertex2 end = null;

        while (!frontier.IsEmpty()) {
            var current = frontier.GetMin ();
            if(IsGoal(current)) {
                end = current;
                break;
            }

            foreach(PathNode neighbor in GetNeighbors(current)) {
                Vertex2 vNeighbor = neighbor.location;
                double newCost = cost[current] + neighbor.cost;
                if(!cost.ContainsKey(vNeighbor) || newCost < cost[vNeighbor]) {
                    cost[vNeighbor] = newCost;
                    frontier.Add(newCost, vNeighbor);
                    from[vNeighbor] = current;
                }
            }
        }

        if (end != null) {
            Vertex2 current = end;
            while(!current.Equals(start)) {
                EnablePoint(current);
                current = from[current];
            }
        }
    }
Пример #8
0
 public void Add(double cost, Vertex2 v)
 {
     queue.Enqueue(v, -cost);
 }
Пример #9
0
 private bool IsGoal(Vertex2 v)
 {
     //return v.Equals (new Vertex2(0, 0));
     return tiles [v.y, v.x] == PathGenerator.OPEN_ENDPOINT;
 }
Пример #10
0
 public PathNode(double cost, Vertex2 location)
 {
     this.cost = cost;
     this.location = location;
 }
Пример #11
0
    private List<PathNode> GetNeighbors(Vertex2 v)
    {
        var pNeighbors = PotentialNeighbors(v);
        var neighbors = new List<PathNode> ();
        foreach(var pNeighbor in pNeighbors) {
            if (map.IsInside (pNeighbor.location) && IsAccessible (pNeighbor.location)) neighbors.Add (pNeighbor);
        }

        return neighbors;
    }
Пример #12
0
 private bool IsAccessible(Vertex2 v)
 {
     int state = tiles[v.y, v.x];
     return state == PathGenerator.OPEN_ENDPOINT || state == PathGenerator.OPEN_NOT_ENDPOINT;
 }
Пример #13
0
    public void Generate()
    {
        int tries = 0;
        while (Rooms.Count < RoomCount) {
            tries++;
            if(tries > RoomCount + 1000*1000) break;
            Random rand = new Random();
            int width = rand.Next(MinRoomSize, MaxRoomSize);
            int height = rand.Next(MinRoomSize, MaxRoomSize);
            int x = rand.Next(CellCount);
            int y = rand.Next(CellCount);

            Room room = new Room(width, height, x, y);
            if(IsInside (room) && !Overlaps(room, 2)) AddRoom (room);
        }
        //AddRoom (new Room (2, 2, 0, 0));

        RoomCount = Rooms.Count;

        Vertex2[] vertices = new Vertex2[Rooms.Count];

        int index = 0;
        foreach (Room r in Rooms) {
            vertices[index++] = r.GetCenterPoint();
        }

        voronoiMesh = VoronoiMesh.Create<Vertex2, Cell2> (vertices);
        foreach (var edge in voronoiMesh.Vertices) {
            int x1 = (int)edge.Vertices[0].x;
            int y1 = (int)edge.Vertices[0].y;
            int x2 = (int)edge.Vertices[1].x;
            int y2 = (int)edge.Vertices[1].y;
            edges.Add (new Edge(new Vertex2(x1, y1), new Vertex2(x2, y2)));

            x1 = x2;
            y1 = y2;
            x2 = (int)edge.Vertices[2].x;
            y2 = (int)edge.Vertices[2].y;
            edges.Add (new Edge(new Vertex2(x1, y1), new Vertex2(x2, y2)));

            x1 = x2;
            y1 = y2;
            x2 = (int)edge.Vertices[0].x;
            y2 = (int)edge.Vertices[0].y;
            edges.Add (new Edge(new Vertex2(x1, y1), new Vertex2(x2, y2)));
        }

        Edge[] edgeAry = new Edge[edges.Count];
        edges.CopyTo (edgeAry);

        c = new ConnectionGraph (new List<Edge>(edges));
        allEdges = edges;
        edges = c.GetEdges ();

        PathGenerator p = new PathGenerator (this);
        Edge[] pedge = new Edge[edges.Count];
        edges.CopyTo (pedge);
        Array.Sort (pedge);
        foreach (var edge in edges) {
            p.AddPath(edge);
        }

        p.GrowOpen ();
        savePaths (p);

        ptiles = p.tiles;
    }
Пример #14
0
 public bool IsConnected(Vertex2 e)
 {
     return  start.Equals (e) || end.Equals(e);
 }
Пример #15
0
 public bool IsInside(Vertex2 v)
 {
     int MapSize = CellCount;
     return  v.x < MapSize && v.x >= 0 && v.y < MapSize && v.y >= 0;
 }
Пример #16
0
    public bool IsInside(Room r, Vertex2 point)
    {
        int rx = r.GetX ();
        int ry = r.GetY ();
        int rw = r.GetWidth ();
        int rh = r.GetHeight ();

        bool isx = point.x >= rx && point.x <= rx + rw;
        bool isy = point.y >= ry && point.y < ry + rh;

        return isx && isy;
    }
Пример #17
0
    public Room GetRoom(Vertex2 point)
    {
        foreach(Room r in Rooms) {
            bool isInside = IsInside (r, point);
            if(isInside) return r;
        }

        return null;
    }
Пример #18
0
 private bool hasCycle(Vertex2 root)
 {
     var visited = new List<Vertex2> ();
     dfs (root, null, visited);
     bool cycle = visited.Contains (root);
     return cycle;
 }
Пример #19
0
 private void dfs(Vertex2 v, Vertex2 prev, List<Vertex2> visited)
 {
     HashSet<Vertex2> c = connectionMap[v];
     foreach (Vertex2 w in connectionMap[v]) {
         if(!visited.Contains(w) && (prev == null || !prev.Equals (w))) {
             visited.Add (w);
             dfs (w, v, visited);
         }
     }
 }