Sign in to follow this  
Sean_Seanston

Game Object Factory Questions

Recommended Posts

I'm making a Top-Down Shooter and I think the Factory pattern would be useful. I'm planning to use the Flyweight pattern for my game entities, which seems a Factory would make easier to implement, and keeping track of entities seems like it would be easier with a Factory system.

My current plan is to have something like this:

class EntityFactory
- List of Flyweights
- Code to handle Flyweights
- Separate function to create each class which inherits from Entity, e.g. createCreature(), createWeapon(), createItem() etc.

1. Would separate Factory objects for each type be better? Or could that layout be improved?

2. Logically, it doesn't make sense for a Creature to be able to be assigned a Flyweight designed for a Weapon. So I've decided I should probably have WeaponFlyweight, CreatureFlyweight etc. and have them (possibly) inherit from Flyweight.
If they inherit, then I suppose I could have a single database of Flyweight objects and then cast them to the correct type when an Entity is being created. Does that sound like it makes design sense?
Another option would be to scrap the base class and just maintain lists of the specific Flyweight objects, but that might be unnecessarily messy.

3. When it comes to keeping track of objects (such as lists of Enemies or Projectiles that must refer to every object of that type in order to properly determine collisions etc.), how should a Factory come into play? I figure an obvious step is to add an Entity to the appropriate list after creation, but how much should the Factory be involved?
Would it be a bad idea to let the Factory manage the lists and instead maybe have a separate "GameWorld" class that deals with such Entity management?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this