Monday, September 29, 2014

Refactoring

So I took a look at the Sprite and GameObject classes in the CommonGameLib and they needed reworking. So I started with a GameObject class, which handled position, color, rotation, origin, scale factor, mirroring, and layer depth. These were all data I knew would be concurrent across all classes that were to inherit from this abstract class. This led to creating the Sprite class. I made a bunch of copy constructors for all cases I could think of. The Sprite class constructors all call the GameObject constructors. This class handled a texture2d, a texture file, a source rectangle, a width and a height (mostly for orientation). This was the first class with a Draw function. Next, I made my Animated Object class, inherited from Sprite. This just handled Cel animation, with all the capabilities of the previous classes. Lastly, I was tired of repeating all these text instantiations all over my code, so I made a TextObject class, a child of GameObject.
if (altOrigin != "") { switch (altOrigin) { case "topleft": mOrigin = Vector2.Zero; break; case "topcenter": mOrigin = new Vector2(mWidth / 2, 0); break; case "topright": mOrigin = new Vector2(mWidth, 0); break; case "centerleft": mOrigin = new Vector2(0, mHeight / 2); break; case "center": mOrigin = new Vector2(mWidth / 2, mHeight / 2); break; case "centerright": mOrigin = new Vector2(mWidth, mHeight / 2); break; case "bottomleft": mOrigin = new Vector2(0, mHeight); break; case "bottomcenter": mOrigin = new Vector2(mWidth / 2, mHeight); break; case "bottomright": mOrigin = new Vector2(mWidth, mHeight); break; default: break; } }
I wrote this along with an optional parameter in the constructor so I could do all the orientations in a way that made sense. With this, you can just specify where you want the orientation from a list of 9 choices.