Background: I'm developing a game for the iPhone/iPad using cocos2d-iphone. Our engine provides the ability for the level designer to create new bad-guys by defining a number of properties such as speed, AI, weapons and main sprite texture in .plist files.
Problem: Currently, we build an NSDictionary of all of the prototypes before the level starts. For each entity we store a class name and an NSDictionary describing the parameters required to build a new instance of this entity. We create an instance of each prototype at load time to precache textures but destroy it immediately. When a new bad guy spawns, we use the stored NSDictionary and pass it to the init method like so (stripped of error checking for brevity);
IKPrototype* prototype = [prototypes objectForKey:prototypeID];
IKBaseEntity* entity = [[[NSClassFromString([prototype prototypeClass]) alloc] init] autorelease];
[entity setPrototypeID:prototypeID];
[entity parseParameters:[prototype parameters]];
return entity;
Now, this works a treat - but creating a number of bullets in the same frame causes noticeable slow down (a frame or two dropped) as the engine parses the parameters (sometimes 20-30 values). However, they're essentially the same every time and only change when added to the world (which happens later).
Question: What would be the most efficient method to store a prototype object for later duplicates to be spawned. Would serialization/deserialization work with a subclass of a CCNode, and would it be any quicker than what I'm doing? Would it be better to store a copy of the final object in my prototype class, and then do a 'deep copy' of that object for spawning? Would I have to worry about CCTexture references if I did this? Please briefly explain the required steps for a proposed solution.
Have been trying to think of the most efficient method for solving this problem for a while. I'm wondering if there's a method I've not thought of above that might be better? Thanks for reading this far.