예제 #1
0
        public IEnumerable <T> ObjectsFromCoord(Coord coord)
        {
            int counter = 0;

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

                    counter++;
                }
                if (counter >= grid.Count)
                {
                    break;
                }
            }
        }
예제 #2
0
        public IEnumerable <T> ObjectsFromCoord(Coord coord)
        {
            int counter = 0;

            foreach (Coord c in coord.DistanceArea(20000))             //to infinity
            {
                int aax = c.x >= 0? c.x:-c.x; int aaz = c.z >= 0? c.z :-c.z;
                int hash = (c.x >= 0? 0x40000000:0) | (c.z >= 0? 0x20000000:0) | ((aax & 0x3FFF) << 14) | (aaz & 0x3FFF);
                if (grid.ContainsKey(hash))
                {
                    yield return(grid[hash]);

                    counter++;
                }
                if (counter >= grid.Count)
                {
                    break;
                }
            }
        }
예제 #3
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;
        }
예제 #4
0
        public virtual void Deploy(CoordRect rect, Coord center, 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 stock rect
            Coord min = rect.Min - stockMargin; Coord max = rect.Max + stockMargin;

            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);
                    }
                }
            }

            //filling empty areas with unused (or new) objects
            foreach (Coord c in center.DistanceArea(rect))
            {
                int hash = c.ToInt();
                if (newGrid.ContainsKey(hash))
                {
                    continue;
                }

                T obj = null;
                if (grid.Count != 0 && allowMove)
                {
                    obj = grid.First().Value;
                    OnMove(obj, c);
                    grid.Remove(grid.First().Key);
                }

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

                newGrid.Add(hash, obj);
            }

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

            grid = newGrid;
        }
예제 #5
0
        public virtual void Deploy(CoordRect[] createRects, CoordRect[] removeRects, 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 && !obj.Equals(null))
                {
                    newGrid.Add(hash, obj);                                                //obj!=null will return true for deleted unity object
                }
                grid.Remove(hash);
            }

            //adding objects within remove rect
            for (int r = 0; r < centers.Length; r++)
            {
                CoordRect rect = removeRects[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 && !obj.Equals(null))
                            {
                                newGrid.Add(hash, obj);
                            }
                            grid.Remove(hash);
                        }
                    }
                }
            }

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

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

                    //moving
                    T obj = null;
                    if (grid.Count != 0 && allowMove)
                    {
                        KeyValuePair <int, T> first = grid.First();
                        obj = first.Value;
                        grid.Remove(first.Key);
                        OnMove(obj, c);
                    }

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

                    newGrid.Add(hash, obj);
                }
            }

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

            grid = newGrid;
        }