Monday, July 9, 2012

Day 10 - Refactoring

My goal today was to have a block spawn a mushroom. I completed that goal, but it took much longer than I originally anticipated.

My code needed refactored (read better organized) rather badly. I had references being passed around to so many objects that it was hard to keep track. For example, if a block needed a texture it first needed to be created by my game class, passed to the tile manager class, and finally given to the block itself. The same could be said about sounds and animations. It was a big headache dealing with all the references and creating variables to store said references. Instead I simply created a few static methods so that these things could be accessed by any object! This made my code much clearer and easier to understand. I don't think I'm done with this change, but it works for now.

Before, when I wanted the block to create a mushroom I would need to pass a reference to the game object list through a bunch of classes to the block. Then the block could add the mushroom to the list.

Now, since the list is static, any object can create a new object just by saying World.AddNewObject(new object)!

No video today because the mushroom doesn't have any collision detection implemented. Tomorrow I'll create some generic algorithms for any object that needs to worry about collisions.

No comments:

Post a Comment