I didn’t make quite as much progress and I might have liked today, primarily due to having a Doctor’s appointment shuffled around to an earlier point and some problems cropping up at work that drew my attention away. This is not all bad; that particular Doctor usually tends to bump my appointment in the other direction, so having it happen sooner is a nice change of pace.
On to code!
For today’s progress, I implemented a Capsule entity. It wraps two instances of Segment and has properties that determine what colors it is and what orientation it’s in. Based on these properties it updates the segment properties to match.
Much like the original NES version of the game, the type of capsule is stored as 9 possible variations, each indicating what the two colors are. There are some overlaps here (e.g. Red-Blue and Blue-Red) but this simplifies the code quite a bit.
A nice tweak here is that the color combinations are set up in the same order as the underlying colors of the segments, which means a little bit of div/mod trickery allows us to extract the color information for both ends of the segment easily.
Capsule knows how to render itself regardless of its orientation, so now it only remains to implement the logic that rotates it around and we’re most of the way there to have an actual working game.
One thing to note here is that the capsule has a bit of a “weird” positioning scheme, in that its position represents the left hand segment when it’s in a horizontal orientation, but the bottom segment when it is in a horizontal orientation. When the rotation happens, this means that the X co-ordinate will jump around a bit when going from horizontal to vertical and back to horizontal in a non-clockwise orientation.
At least, I think that’s how it’s supposed to work. I guess I’ll have to play more Dr. Mario to get a better feel for this.