Example #1
0
        public IEnumerable <T> ObjectsFromCoords(Coord[] coords)
        {
            int counter = 0;

            foreach (Coord c in Coord.MultiDistanceArea(coords, 20000))            //to infinity
            {
                int hash = c.ToInt();
                if (grid.ContainsKey(hash))
                {
                    yield return(grid[hash]);

                    counter++;
                }
                if (counter >= grid.Count)
                {
                    break;
                }
            }
        }
Example #2
0
        public virtual void Deploy(CoordRect[] rects, Coord[] centers, bool allowMove = true)
        {
            Dictionary <int, T> newGrid = new Dictionary <int, T>();

            //adding nailed objs
            foreach (int hash in nailedHashes)
            {
                if (!grid.ContainsKey(hash))
                {
                    Debug.Log("Could not find nailed object");
                }

                T obj = grid[hash];
                if (obj != null)
                {
                    newGrid.Add(hash, obj);
                }
                grid.Remove(hash);
            }

            //adding objects within rect
            for (int r = 0; r < centers.Length; r++)
            {
                CoordRect rect = rects[r];
                Coord     min = rect.Min; Coord max = rect.Max;
                for (int x = min.x; x < max.x; x++)
                {
                    for (int z = min.z; z < max.z; z++)
                    {
                        Coord coord = new Coord(x, z);
                        int   hash  = coord.ToInt();

                        if (grid.ContainsKey(hash))
                        {
                            T obj = grid[hash];
                            if (obj != null)
                            {
                                newGrid.Add(hash, obj);
                            }
                            grid.Remove(hash);
                        }
                    }
                }
            }

            //creating an unused stack - sorted by distance
            Stack <T>   unused       = new Stack <T>();
            Stack <int> unusedHashes = new Stack <int>();

            if (grid.Count != 0)
            {
                foreach (Coord c in Coord.MultiDistanceArea(centers, 20000000))
                {
                    int hash = c.ToInt();
                    if (grid.ContainsKey(hash))
                    {
                        if (grid[hash] != null)
                        {
                            unused.Push(grid[hash]); unusedHashes.Push(hash);
                        }
                        grid.Remove(hash);
                        if (grid.Count == 0)
                        {
                            break;
                        }
                    }
                }
            }


            //filling empty areas with unused (or new) objects
            for (int r = 0; r < centers.Length; r++)
            {
                CoordRect rect   = rects[r];
                Coord     center = centers[r];

                foreach (Coord c in center.DistanceArea(rect))
                {
                    int hash = c.ToInt();
                    if (newGrid.ContainsKey(hash))
                    {
                        continue;
                    }

                    T obj = null;
                    if (unused.Count != 0 && allowMove)
                    {
                        obj = unused.Pop(); unusedHashes.Pop();                         //popping the furtherest unused
                        OnMove(obj, c);
                    }

                    else
                    {
                        obj = Construct();
                        OnCreate(obj, c);
                    }

                    newGrid.Add(hash, obj);
                }
            }

            //adding other unused to trail or removing them
            while (unused.Count > 0)
            {
                T   obj  = unused.Pop();
                int hash = unusedHashes.Pop();

                //if (!allowMove) OnRemove(obj);
                //else
                newGrid.Add(hash, obj);
            }

            //removing all other objs left
            //foreach(KeyValuePair<int,T> kvp in grid) OnRemove(kvp.Value);

            grid = newGrid;
        }