Beispiel #1
0
    private static void Run()
    {
        // Runs on the thread.
        UnityEngine.Debug.Log("Running pathfinding multithread...");
        while (run)
        {
            if (!writing)
            {
                if (pending.Count == 0)
                {
                    Thread.Sleep(5);
                    continue;
                }

                PathfindingRequest request = pending.Dequeue();
                if (request.IsValid())
                {
                    watch.Reset();
                    watch.Start();

                    // Pathfind here.
                    List <Node> path = Pathfinding.Run(request.StartX, request.StartY, request.EndX, request.EndY, request.Layer);

                    acc++;

                    watch.Stop();
                    long elapsed = watch.ElapsedMilliseconds;
                    times.Add(elapsed);

                    if (times.Count > 100)
                    {
                        times.RemoveAt(0);
                    }

                    if (request.Done != null)
                    {
                        lock (hasPending)
                        {
                            hasPending.Remove(request.ID);
                        }
                        while (reading)
                        {
                            Thread.Sleep(10);
                        }
                        finished.Add(new KeyValuePair <UnityAction <List <Node> >, List <Node> >(request.Done, path));
                    }
                    else
                    {
                        lock (hasPending)
                        {
                            hasPending.Remove(request.ID);
                        }
                        UnityEngine.Debug.LogWarning("Wasted pathfinding (" + (path == null ? "NO PATH" : "PATH FOUND") + "), no receptor!");
                    }
                }
            }
        }
        UnityEngine.Debug.Log("Shutdown pathfinding multithread.");
    }
Beispiel #2
0
    public static bool Find(string ID, int x, int y, int ex, int ey, TileLayer layer, UnityAction <List <Node> > done)
    {
        if (!run)
        {
            if (done != null)
            {
                done.Invoke(null);
            }
            return(false);
        }

        if (pending.Count >= MAX_PENDING)
        {
            if (done != null)
            {
                done.Invoke(null);
            }
            return(false);
        }

        if (hasPending.Contains(ID))
        {
            return(false);
        }

        PathfindingRequest r = new PathfindingRequest()
        {
            StartX = x, StartY = y, EndX = ex, EndY = ey, Layer = layer, Done = done, ID = ID
        };

        if (r.IsValid())
        {
            writing = true;
            lock (hasPending)
            {
                hasPending.Add(ID);
            }
            pending.Enqueue(r);
            writing = false;
            return(true);
        }
        else
        {
            if (done != null)
            {
                done.Invoke(null);
            }
            return(false);
        }
    }