Return of the Great Refactor

2 minute read

52 Days Until I Can Walk

Today marks the return of The Great Refactor. It’s actually nice to be returning to this problem after a short break doing something else. I feel more than refreshed and ready to start fixing some horrible, horrible code.

One of the first things I have done today is split the little demo level that is visible on engine.fourteenscrews.com out into its own crate. Not only does this tidy up the code for the engine considerably, but it is also helps with thinking about what constitutes core engine functionality, and what would I expect a user of the engine to be able to do.

After some shuffling of code around into modules and submodules, I’ve begun to define a simple JSON schema that I think will form the basis for how users will share levels with each other. The core engine at least will have parsers to load instances of structs from JSON objects. So in the demo, it is completely feasible to share a JSON object with friends, and have the engine load and render it.

Currently the structure is quite simple, as shown below:

module.exports = {
  camera: { x: 160, y: 160, angle: 0, horizon: 100 },
  scene: {
  	width: 5,
  	height: 5,
  	x_walls: [ 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1 ],
  	y_walls: [ 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1 ],
  	ceiling: [ 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42 ],
  	floor  : [ 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23 ]
  },
  textures: "/* snip */"

Camera is reasonably self-explanatory. This will be a view point that is dropped in at (160, 160), facing 0 degrees with the vertical view centred.

Scene is also reasonably straightforward, although I would like to make it a little more readable. x_walls and y_walls are our vertical and horizontal grid lines respectively. The integer value is a texture code. The same goes for floor and ceiling. Later these will be converted to arrays of objects as I will want to make walls passable, or place secret doors, etc. For now, though, this will do.

The snipped texture object is a base64 encoded string containing the images to be used for texturing. These have already been converted to the rotated format that I described in a previous post. There’s definitely a better way to do this, but it works quite well for now.

Obviously with all this shuffling around, the code is going to break. And I have been a little anxious about that. But today I have taken the first step towards committing to deleting the old, messy code. With a bit of luck, tomorrow I will delete all the mess. This will leave me with a broken engine, but the code will be better structured and hopefully a quick day of bug fixing will pull everything back into place.

But overall, things are already looking a lot better. So I’m going to keep working to improve things and hopefully be back implementing some new cool features (like doors, objects and enemies) very soon.