
#import <gamekit/gamekit.h>

@implementation GKCollisionGroup

- init
{
	id ret = [super init];
	actorLists[0] = [[List alloc] init];
	actorLists[1] = [[List alloc] init];
	collisionDetector = [GKCollider new];
	delegate = nil;
	return ret;
}

- (int)tag { return tag; }
- setTag:(int)anInt { tag = anInt; return self; }

- addActor:anActor toList:(int)number
{
	return [actorLists[number] addObjectIfAbsent:anActor];
}

- removeActor:anActor fromList:(int)number
{
	return [actorLists[number] removeObject:anActor];
}

- delegate { return delegate; }
- setDelegate:anObject { delegate = anObject; return self; }

- calculateCollisions:sender
{	// This algorithm is n*m (i.e. n^2) so don't make both lists large!!!
	int i, j;
	for (i=0; i<[actorLists[0] count]; i++) {
		id actor1 = [actorLists[0] objectAt:i];
		for (j=0; j<[actorLists[1] count]; j++) {
			id actor2 = [actorLists[1] objectAt:j];
			if ([collisionDetector object:actor1 collidesWith:actor2]) {
				// notify the actors...
				[actor1 collidedWith:actor2];
				[actor2 collidedWith:actor1];
				if ([delegate respondsTo:@selector(actor:collidedWith:)])
					[delegate actor:actor1 collidedWith:actor2];
			}
		}
	}
	return self;
}

@end
