Ejemplo n.º 1
0
        public void Add(Component component)
        {
            CollisionRegion region = GetRegion(component);

            if (region == null)
            {
                return;
            }
            region.AddComponent(component);
        }
Ejemplo n.º 2
0
        public bool Remove(Component component)
        {
            CollisionRegion region = GetRegion(component);

            if (region == null)
            {
                return(false);
            }

            return(region.RemoveComponent(component));
        }
Ejemplo n.º 3
0
        private CollisionRegion GetRegion(Component gameObject)
        {
            Vector2 coordinates = GetCoordinates(gameObject.Position);

            if (!IsValidRegion((int)coordinates.X, (int)coordinates.Y))
            {
                return(null);
            }

            CollisionRegion bucket = collisionRegions[(int)coordinates.X][(int)coordinates.Y];

            return(bucket);
        }
Ejemplo n.º 4
0
        private CollisionRegion GetOffsetRegion(Component gameObject, int xOffset, int yOffset)
        {
            Vector2 coordinates = GetCoordinates(gameObject.Position);

            /* If the coordinates are out of bounds, we return null. */
            if (!IsValidRegion((int)coordinates.X + xOffset, (int)coordinates.Y + yOffset))
            {
                return(null);
            }

            CollisionRegion bucket = collisionRegions[(int)coordinates.X + xOffset][(int)coordinates.Y + yOffset];

            return(bucket);
        }
Ejemplo n.º 5
0
        public HashSet <Component> GetObjectsCollided(Component component, Type type)
        {
            HashSet <Component> ret = new HashSet <Component>();
            double radius           = component.HitBoxRadius;

            /* Directions */
            int[,] dirs = new int[, ] {
                { 1, 1 }, { 0, 1 }, { -1, 1 },
                { 1, 0 }, { 0, 0 }, { -1, 0 },
                { 1, -1 }, { 0, -1 }, { -1, -1 }
            };

            // Check all neighbors
            for (int i = 0; i < 9; ++i)
            {
                for (int j = 1; j <= 1; ++j)
                {
                    int xOffset = dirs[i, 0];
                    int yOffset = dirs[i, 1];

                    CollisionRegion collision = GetOffsetRegion(component, xOffset, yOffset);
                    //null indicates that the coordinates were invalid
                    if (collision != null)
                    {
                        //For all game objects in the bucket, if within the collision region add to return set
                        foreach (Component componentObj in collision.GetComponents())
                        {
                            if (!componentObj.GetType().IsSubclassOf(type) && !componentObj.GetType().Equals(type))
                            {
                                continue;
                            }
                            if (componentObj.BoundsContains(componentObj) ||
                                componentObj.BoundsContains(componentObj))
                            {
                                ret.Add(componentObj);
                            }
                        }
                    }
                }
            }
            return(ret);
        }