public void Add(Component component) { CollisionRegion region = GetRegion(component); if (region == null) { return; } region.AddComponent(component); }
public bool Remove(Component component) { CollisionRegion region = GetRegion(component); if (region == null) { return(false); } return(region.RemoveComponent(component)); }
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); }
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); }
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); }