Refactoring is Not Malarkey


Hey all,

I spent a decent chunk of time the last few days lamenting over a graphical glitch. Hours spent neglecting my children, 'forgetting' to sleep and eat. Hours I would not have spent if instead, I made an earlier decision to *refactor*.

See, with most games, it's really important that your artwork is, at all times... how do I put this... drawn in the right position. Unfortunately, the engine's ability to accomplish this was sharply limited by my seemingly eternal lack of discipline.

By adding more artwork, more drawing layers, into my already overloaded 'render' method, and not having one clean, consistent way of determining WHERE to put these layers in terms of x, y coordinates, the game loop swiftly lost faith in me and began producing a result that was the engine equivalent of a middle finger in the face.

Here's how it looked when I simply panned the screen. I don't remember adding an earthquake mechanic.

Ultimately, the problem boiled down to two important facts I had overlooked before I began taking everything apart:

- I used to know what the code was doing

- I do not know what the code is doing now

Enter REFACTORING. See, in a rare moment of clarity, I had the idea to adjust how only the lighting tiles were drawn. Instead of layering pre-drawn shaded squares on the screen, I would use Java's fillRect method to draw little black squares of varying opacity. To accomplish this, I needed to DRASTICALLY simplify how the world <> screen coordinate conversions were done. 

Then:


The end result: great success.

Notice as I pan the screen, the lighting tiles following my cursor oh so nicely... unlike literally everything else.

By refactoring the way tiles were shaded, I actually solved the larger problem. Naturally, you'd expect me to recognize that I had done this. Instead, I spent the entire morning agonizing over the rest of the graphics engine. Why couldn't it behave itself properly? Because, dear readers, it got a taste of that sweet, sweet refactoring, and was on the verge of open rebellion.

Ladies and gentlemen, please be disciplined as you code and REFACTOR when your methods get unwieldy. You *will* forget what they are supposed to do. Take this as a cautionary tale. My children implore you.

:sniff: I love a story with a happy ending!

Cheers,

PajamaWolf

Get This Godforsaken Rock - Combat Demo

Leave a comment

Log in with itch.io to leave a comment.