How does that look to you?

I started messing with XNA some more and found a disk containing a copy of the small engine that I was working on before I moved to Vista. Looking over the code almost made me sick. Things were so hard to follow and changing one thing led to a million other changes.

One major problem I noticed was the mountain of objects that kept getting passed around. For example, in my screen manager, and instance of LogManager was getting passed everywhere and sometimes the ILogManagerService was being pulled from Game.Services. There's no consistency. Yes, I am to blame for most of that, but I believe I was inconsistent because there was no one way to do things.

So, for the third rewrite, I figured I should come up with a catch-all pattern that will keep everything organized for me: ObjectRegistry.

Basically, I have some classes, such as InputManager and LogManager that only need to be created once (except in some rare cases). When I create the instance of this object, I add it to the ObjectRegistry, which is a singleton and accessible from all other objects.

InputManager input = new InputManager();
ObjectManager.Instance.AddObject("InputManager", input);

Now, say I want to use that same object in a class that has nothing to do with the class in which the original InputManager was created. Instead of doing this:

public SomeOtherClass(Game game) : base(game) {
    InputManager input = (InputManager)game.Services.GetService(typeof(IInputManager));
}

I do this:

InputManager input = (InputManager)ObjectRegistry.Instance["InputManager"];

I don't even need an instance of the Game class for that to work (which is another part of the clutter). Here's what my ObjectRegistry class looks like for the most part:

class ObjectRegistry {
    // Dictionary<>() initialization stuff and methods
    // to add and remove objects from the Registry
    // omitted because it's all pretty standard

    public object this[string key] {
        get { return objects[key]; }
    }

    // Singleton
    private static readonly object padLock = new object();
    private static ObjectRegistry instance = null;
    public static ObjectRegistry Instance {
        get {
            lock (padLock) {
                if (instance == null) {
                    instance = new ObjectRegistry();
                }
            }
        }
    }
}

Anyway, my question: Is this a good solution to the problem or will it soon become just as cluttered as before? Do you have any other suggestions for keeping an extremely clean and organized game engine structure?

Technorati Tags: ,,,,