Characters! (and moving around)

I finally added characters. Characters are a bit special compared to the other layers we’re drawing, since we can have more than one per cell, and they can be manipulated directly from outside the map code. Objects will fall into this category too.

But now that we have a character moving around, we need to keep track of exactly which cell it’s stepping in. For this I needed to have a better way of determining which cell an offset from the center of another cell falls into. In other words, if the little guy is displaced 10px to the left and 10px up from the center of the cell, is he still in this cell?

I was doing this for scrolling the map, but that considered cells to be rectangular, which won’t work for our little fellas. We need to take into account the “diagonals”.
This is the kind of thought process I really enjoy doing, because I feel like my very limited knowledge of math is actually useful.

So this is pretty simple, just replace X in those formulas, and check whether Y is on the other side of the line. Easy!

And, we can use this for our character, and also for the viewport scrolling. Cleaner code FTW!

The other thing we want to do is scroll the viewport when the character is getting near the edges. This is really simple to do; we just need to define a “walkable area without scrolling”, and also a “ScreenToMap” function that is basically the exact opposite of MapToScreen (although the math is completely different and WAY simpler).

By the way… I love it when I write one of these functions and they just work at the first try. That is so not the common case when I’m doing math.

And now we have a guy that can walk on a scrolling map!

Next step: Blocking the walk! We really need to stop the whole walking through walls thing!

Leave a Reply

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


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">