예제 #1
0
        public List <GridObject2D> GetObjectsInMyZone(GridObject2D obj)
        {
            List <Vector2>      registeredZones = new List <Vector2>();
            List <GridObject2D> objList         = new List <GridObject2D>();

            if (objectRegisteredZones.ContainsKey(obj))
            {
                registeredZones = objectRegisteredZones[obj];
            }

            if (registeredZones.Count > 0)
            {
                for (int z = 0; z < registeredZones.Count; z++)
                {
                    for (int o = 0; o < gridObjects[registeredZones[z]].Count; o++)
                    {
                        if (gridObjects[registeredZones[z]][o] != obj)
                        {
                            if (!objList.Contains(gridObjects[registeredZones[z]][o]))
                            {
                                objList.Add(gridObjects[registeredZones[z]][o]);
                            }
                        }
                    }
                }
            }

            return(objList);
        }
예제 #2
0
        private Vector2 GetGridRef(GridObject2D obj)
        {
            Vector2 gridRef = Vector2.Zero;

            for (int x = 0; x < gridDimensions.X; x++)
            {
                for (int y = 0; y < gridDimensions.Y; y++)
                {
                    Vector2 checkRef = new Vector2(x, y);
                    if (theGrid[checkRef].Intersects(obj.Bounds))
                    {
                        return(checkRef);
                    }
                }
            }
            return(gridRef);
        }
예제 #3
0
 public void AddObject(GridObject2D obj)
 {
     listObjects.Add(obj);
     objectRegisteredZones.Add(obj, new List <Vector2>());
     RegisterObject(obj);
 }
예제 #4
0
        private void RegisterObject(GridObject2D obj)
        {
            bool                registered = false;
            Vector2             gridRef    = Vector2.Zero;
            List <GridObject2D> objList;

            // Get my zones.
            List <Vector2> registeredZones = new List <Vector2>();

            if (objectRegisteredZones.ContainsKey(obj))
            {
                registeredZones = objectRegisteredZones[obj];
            }

            // Am I already registered?
            if (registeredZones.Count == 0)
            {
                gridRef    = GetGridRef(obj);
                registered = false;
            }
            else
            {
                registered = true;
            }

            // If not then find my initial place in the grid..
            if (!registered)
            {
                // Register with this grid ref.
                objList = gridObjects[gridRef];
                objList.Add(obj);
                gridObjects[gridRef] = objList;
                registeredZones.Add(gridRef);
                objectRegisteredZones[obj] = registeredZones;
            }
            else // If so, then am I still in these zones?
            {
                for (int r = 0; r < registeredZones.Count; r++)
                {
                    if (!theGrid[registeredZones[r]].Intersects(obj.Bounds))
                    {
                        // Remove from this ref
                        // Remove me from the object list in the ref.
                        objList = gridObjects[registeredZones[r]];
                        objList.Remove(obj);
                        gridObjects[registeredZones[r]] = objList;

                        // Remove my registration with this ref.
                        registeredZones.Remove(registeredZones[r]);
                        objectRegisteredZones[obj] = registeredZones;
                    }
                }
            }
            // Am I also in neigbouring zones?
            for (int r = 0; r < objectRegisteredZones[obj].Count; r++)
            {
                gridRef = objectRegisteredZones[obj][r];
                for (int x = (int)gridRef.X - 1; x < gridRef.X + 2; x++)
                {
                    for (int y = (int)gridRef.Y - 1; y < gridRef.Y + 2; y++)
                    {
                        if (x >= 0 && x < gridDimensions.X && y >= 0 && y < gridDimensions.Y)
                        {
                            Vector2 thisRef = new Vector2(x, y);
                            if (!registeredZones.Contains(thisRef))
                            {
                                if (theGrid[thisRef].Intersects(obj.Bounds))
                                {
                                    // Register with this grid ref.
                                    objList = gridObjects[thisRef];
                                    objList.Add(obj);
                                    gridObjects[thisRef] = objList;
                                    registeredZones.Add(thisRef);
                                    objectRegisteredZones[obj] = registeredZones;
                                }
                            }
                        }
                    }
                }
            }
        }