The Great Refactor (Part 2)
63 Days Until I Can Walk
Another quiet day of refactoring, but I’m starting to converge on an organisation for my code that I like.
I’ve continued to break my code up into modules and have started thinking more about how I am representing information in the application. My ray caster has been broken into three structs—one for walls, one for floors, and one for ceilings. Using traits or enums (I haven’t decided yet), I should be able to implement a renderer where it is possible to swap in and out the structs that define the logic for rendering the three types of surface. Mostly this will be useful if we need to save some CPU cycles at runtime, so we might switch off floor or ceiling texturing.
I’m not entirely sure what the most “Rust” way to do this is, however. Normally I would write some sort of builder or factory, but I’m not sure if that is appropriate here.
Another dimension to the problem is that I have spent too long writing Java over the last few years, and I’m fighting my brain to stop from creating multiple abstractions and traits in the name of modularity and Object Orientation. At one point I found myself writing the following enum to keep track of what texture code is assigned to a wall/floor/ceiling tile:
The idea being that it is possible for a surface to not have a texture. But if there is a texture then I should use a numerical code to index my texture map and extract it. But as I looked at this, I felt strong YAGNI vibes and decided to simply store u8
s in my tiles. If I really want to have the option of a surface being untextured, I’ll use an Optional, but I doubt I’m going to need it.
I’ve started to move my code for colouring and blending into a single sensible struct:
This is still a little messier than I’d like. In particular, I want to get rid of all those floating point numbers. But this grouping makes a lot more sense than the mish-mash of functions I had before. This will also make it much easier to write colour data to the output buffer.
I have started a package for collision detection which will separate code for intersection with walls and objects from the code for the player. I can see this code undergoing a significant change, but my modifications may make it slower. Right now there is a single large function which checks for intersections with walls and objects (the latter being stubbed in, but it will happen). I could create collision objects—bounding boxes etc. and bind these to objects so that different objects can support different types of collision. But realistically, every object is going to use a bounding box in this type of engine. So I’m at risk of overcomplicating things here.
This refactor is going to take longer than expected. Partially because it is complicated and I want to take my time with it. But also because tomorrow I won’t have as much time to code as I have another hospital appointment in the afternoon.
I’ll keep posting, but my hope is that the Great Refactor will be over soon.