예제 #1
0
        public void FillBucket(GameObject gameObject)
        {
            CollisionBucket bucket = FetchBucket(gameObject);

            if (bucket == null)
            {
                return;
            }
            bucket.AddElement(gameObject);
        }
예제 #2
0
        public Boolean RemoveFromBucketIfExists(GameObject gameObject)
        {
            var             t      = gameObject;
            CollisionBucket bucket = FetchBucket(gameObject);

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

            return(bucket.RemoveElement(gameObject));
        }
예제 #3
0
        private CollisionBucket FetchBucket(GameObject gameObject)
        {
            Vector2 coordinates = GetCoordinates(gameObject.Position);

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

            CollisionBucket bucket = collisionBuckets[(int)coordinates.X][(int)coordinates.Y];

            return(bucket);
        }
예제 #4
0
        private CollisionBucket FetchBucket(GameObject gameObject, int xOffset, int yOffset)
        {
            Vector2 coordinates = GetCoordinates(gameObject.Position);

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

            CollisionBucket bucket = collisionBuckets[(int)coordinates.X + xOffset][(int)coordinates.Y + yOffset];

            return(bucket);
        }
예제 #5
0
        public HashSet <GameObject> GetObjectsCollided(GameObject gameObject, Type type) /* Optional filter parameter */
        {
            HashSet <GameObject> ret = new HashSet <GameObject>();
            double radius            = gameObject.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 neighboring cells */
            for (int i = 0; i < 9; ++i)
            {
                int xOffset = dirs[i, 0];
                int yOffset = dirs[i, 1];

                CollisionBucket cb = FetchBucket(gameObject, xOffset, yOffset);
                /* null indicates that the coordinates were invalid */
                if (cb != null)
                {
                    /* For all game objects in the bucket, if within the collision region add to return set */
                    foreach (GameObject go in cb.GetObjects())
                    {
                        if (!go.GetType().IsSubclassOf(type) && !go.GetType().Equals(type))
                        {
                            continue;
                        }
                        if (gameObject.BoundsContains(go.Position) ||
                            go.BoundsContains(gameObject.Position))
                        {
                            ret.Add(go);
                        }
                    }
                }
            }
            return(ret);
        }