public ThreadDispatcher()
        {
            Utils.Log("Starting ThreadDispatcher");

            if (_instance != null)
                throw new InvalidOperationException("ThreadDispatcher singleton already exists");

            _instance = this;
            MainThread = Thread.CurrentThread;
            Worker = new Thread(DoWork);
            WorkerQuitRequested = false;
            Worker.Start();

            GameEvents.onLevelWasLoaded.Add(LevelWasLoaded);
        }
Beispiel #2
0
        public void UpdatePosition(Coordinates coord)
        {
            DebugUtils.Assert(ThreadDispatcher.IsMainThread);

            int NextTile = FindTile(coord, CurrentTile);

            //if (NextTile == CurrentTile)
            //	return;
            CurrentTile = NextTile;

            lock (this)
                NextPosition = coord;

            ThreadDispatcher.QueueToWorker(() => UpdatePositionWorker());

            if (tileOverlayObj == null)
            {
                tileOverlayObj     = new GameObject();
                tileOverlay        = tileOverlayObj.AddComponent <MapOverlayTiles>();
                tileOverlay.Body   = Body;
                tileOverlay.planet = this;
            }

            var tileColors = new Dictionary <int, Color32>();

            /*int city = -1;
             * if (data.ContainsKey(CurrentTile))
             *      city = data[CurrentTile].ClosestCity;*/

            foreach (var i in data)
            {
                byte r, g, b;
                //if (i.Value.Color(out r, out g, out b, city))
                if (i.Value.Color(out r, out g, out b))
                {
                    tileColors[i.Key] = new Color32(r, g, b, 255);
                }
            }

            if (data.ContainsKey(CurrentTile) && IsNodeAllowed(CurrentTile))
            {
                var knownCities = data.Where(x => x.Value.HasCity.GetValueOrDefault()).Select(x => x.Key).ToArray();

                if (knownCities.Length > 0)
                {
                    var pf   = new Pathfinding(this, knownCities, CurrentTile);
                    var path = pf.GetPath(CurrentTile).ToArray();
                    int city = data[CurrentTile].ClosestCity;

                    if (city >= 0)
                    {
                        foreach (var i in data)
                        {
                            if (IsNodeAllowed(i.Key) && data[i.Key].Level >= TileData.ComputationLevel.CITY_BORDER_FOUND && city == data[i.Key].ClosestCity && !data[i.Key].HasCity.GetValueOrDefault())
                            {
                                tileColors[i.Key] = XKCDColors.Yellow;
                            }
                        }

                        for (int i = 0; i < path.Length - 1; i++)
                        {
                            tileColors[path[i]] = XKCDColors.Orange;
                        }

                        tileColors[CurrentTile] = XKCDColors.Pink;

                        var tmp = TileData._frontier;
                        if (tmp != null)
                        {
                            foreach (var i in tmp)
                            {
                                tileColors[i] = XKCDColors.BabyPukeGreen;
                                //DebugUtils.Assert(data[i].ClosestCity != city);
                            }
                        }
                    }
                }
            }

            tileOverlay.UpdateTiles(tileColors);
        }
        void Destroy()
        {
            GameEvents.onLevelWasLoaded.Remove(LevelWasLoaded);

            Utils.Log("Stopping ThreadDispatcher");
            DebugUtils.Assert(Worker != null);
            DebugUtils.Assert(IsMainThread);

            ClearAndQueueToWorker(() => WorkerQuitRequested = true);

            // Get the last log messages
            DequeueFromWorker();

            if (Worker.Join(5000))
            {
                Utils.Log("Worker thread stopped");
            }
            else
            {
                Utils.Log("Worker thread not stopped, aborting");
                Worker.Abort();
            }

            Worker = null;

            _instance = null;
        }