Refactoring makes me sleepy

Just perusing my git log for commits pushed today shows that my first commit was done at 11:51:55 PST and the last was at 22:13:53 PST. Of course I wasn’t coding for that entire time, but I DID know that today was going to be a bit of an investment, so I got started early and put in more time; my vacation started today and apparently I know how to let the good times roll.

I’ll go over the changes made, but to begin with I’d like to point out that this little refactoring and reorganization exercise cut the length of the Maze entity from an astounding 1846 lines down to 953, which is ~51% shorter. Before anyone goes off on how that’s still an excessive length, a quick perusal of the Maze.ts source file (or any of the source files) will show that I am pathologically verbose in commenting my code. So there.

The first big batch of refactoring involved creating a new class named MazeContent that contains the array that holds the maze contents as well as all of the accessor functions for getting and setting fields. An instance of this is stored in the Maze itself, and is shared out via a property so that anything that has access to the maze also has access to the ability to get at or modify the contents of the maze itself.

The second batch involved creating a new MazeGenerator class that is responsible for generating the content of the maze. It makes more logical sense to me to have this code be separate since it’s very specific to a particular part of the game. In order to get up and running some extra accessors were added to the Maze entity to allow getting any entity that comes from an ActorPool out of the proper pool in a single call. Between this and the new generator class having a property to set to tell it what entities to use for creating walls and black holes, everything was tied up nicely.

The last batch involved creating a new MazeDebugger class that contains all of the debug methods that interact with the maze, such as adding and deleting cells and such. This operates similar to the new generator class in that it needs to be told about specific entities so that it can tell when it’s trying to delete a wall or what entity to use for a black hole, but everything else was taken care of by the extra accessors added to the Maze entity to get the generator class to work.

There was also some incidental other changes made:

I added to the ActorPool and MazeCell classes the new ability for the cell to track what actor pool it was last added to, which means that given any cell we can easily dispose of it via entity.kill() without having to do a bunch of checks to see what pool it actually came from. This cleans up a lot of code, and in particular makes the new debugging class nicer because it allows an entity that is stored in a pool to be killed without having to expose the pool itself as another property on the maze.

I also removed all cases of the use of instanceof in the code. In ts-game-engine all entities have a name property, which was originally used for debug logging purposes, with the idea that every entity would have a unique name. In this project it actually represents the class of the entity (all bricks have a name of “brick”, for example) and so this provides the perfect ready made mechanism for determining what type an entity is without having to resort to instanceof.

Lastly I found a few places that had redundant code that was no longer needed based on the above changes and previous refactorings that I had not gotten around to fixing. I also pulled some common code out into accessor methods because my theory is “if I do it more than once, I probably want a method”.

Tomorrow I plan to start in on adding in actual game play and controls. I’m thinking to start a simple Player entity so that instead of clicking on a ball it will work the way that the source game does, you walk a little guy around and push the balls downward. I’m assuming that will go fairly smoothly, and if so I will also make some simple FSM changes to how the maze processes input so that the code is clearer than it is currently with all of the boolean flags.