There’s a couple of stories I want to implement in this post;
- Rotating blocks
- Bounds checking. We want to make sure blocks don’t move outside the playing field.
- When a block reaches the bottom of the playing field it has to stack up in a wall.
The first big change I made was to implement rotation. It’s actually pretty easy to rotate a block 90 degrees. Just swap the x and y coordinates of all the squares that make up the block and make one negative. Negative x means rotate left and negative y means rotate right. To make it possible to rotate around other points than 0,0 I just move the block before and after the rotation. Blocks now have a center of rotation property too.
In order to implement this easily I had to shift around some object responsibilities. Blocks didn’t know about their position, so in order to know if a block was outside of the playing fields I had combine information from the game object (block position) with information from the block (block shape). It would be more logical to have blocks maintain their own position. After I made this change it was pretty easy to check if a block moved outside of it’s bounds.
This seems like a pretty big feature but most of the functionality for it was already there. All the wall has to do was maintain an array of blocks and do some collision detection. I had this implemented in a couple of minutes. Hooking it into the presenter and the game object was easy too. I actually spent some time moving the bounds checking logic into the wall object too.
Oh, and there’s no parameter overloading for functions.