
#import <gamekit/gamekit.h>
#import <objc/objc-runtime.h>

@implementation GKActorManager

- init
{
	id ret = [super init];
	actorHash = [[HashTable alloc] initKeyDesc:"@" valueDesc:"@" capacity:0];
	return ret;
}

- listOfActorsOfClass:aClass
{	// get the list of actors for the given class.  If no list yet, make one.
	id list = [actorHash valueForKey:aClass];
	if (!list) {
		list = [[List alloc] init];
		[actorHash insertKey:aClass value:list];
	}
	return list;
}

- addDeadActor:anActor
{
	id list = [self listOfActorsOfClass:[anActor class]];
	// I'd use -addObjectIfAbsent: but this is faster.  Don't add an object
	// twice, though, or actors will start behaving _really_ strangely!
	// (Like jumping around, disappearing, or suddenly "starting over")
	[list addObject:anActor];
	return self;
}

- getActorOfClass:aClass
{	// list should NEVER be nil!
	id list = [actorHash listOfActorsOfClass:aClass];
	int count = [list count]; //send message once...cache the result
	if (!count) return [[aClass alloc] init];
	// removing from end is fastest
	return [list removeObjectAt:(count-1)];
}

- getActorOfClassNamed:(char *)className
{
	[self getActorOfClass:objc_lookUpClass(className)];
	return self;
}

@end
