Wall Collisions and Some Data Refactoring

Pencil work for the refactor

Series:
Posted on

Day 4

For this day, the main plan was to add in map collision (to keep our player from walking off the map) and rethink and refactor how prop data is stored and used.

Wall Collisions

There’s really not a lot to say here. I treated the map collisions a little bit differently than props, in that the map collision data is ready to use right away, whereas the prop collision information needs to be translated based on the x,y position on the map. Because of that, I introduced a new function for getting collisions called get_collisions_v2(). The goal here is to update the prop collision code to use get_collisions_v2() during the refactor.

Wall collisions

One thing to note: That bug with the guy and stool on the far right is still present. Still ignoring it 😉

Data Refactor

Up to this point, I had a lot of repeat data spread across each prop. Every stool on the screen has a copy of the collision data, the x and y offset used for drawing, and the tile number used for the stool. That’s not bad unless you need to make a change to one value across all of those stools. Pro tip: You always will need to make a change to one value across all of those stools.

This is something that I didn’t trust myself to just get to coding on and have it work right away, so I went to pencil and paper!

Pencil work for the refactor

In the end, I settled on storing the x and y position and the prop name on an entity, and storing the draw offset, collision data and tiles on a prop definition. So there are currently three prop definitions: guy, stool and wall-1; and there are ten entities that take advantage of those prop definitions.

And to make that change, I needed to update the code that handles rendering, draw order, movement and collisions. Basically everything I’ve put together at this point. But better now than later.

Movement and collision after the refactor

According to my time tracking app, that refactor took me about 2.5 hours and now I will have less duplicate data spread across the game’s code base. Also, if you look closely, you’ll see that the draw order bug with the guy and stool on the far right is gone. It’s probably gone because I simplified the draw order sorting to use just the collision info in order to determine which prop to draw first. I honestly can’t be 100% sure though.

Progress Summary

Successes:

  • Map collision
  • Eliminated repeat data by implementing shared prop data
  • Accidentally fixed the draw order bug that happened with one of the guy’s and stools

Potential plan for the next day of work:

  • Animated props
  • Prop states (idle, walking, pushing)

Lessons learned:

  • Sometimes if you ignore a bug, a future refactor will make it go away

Leave a Reply

Your email address will not be published. Required fields are marked *