If you recall from one of last month's posts, I was working on movement algorithms for sprites in Critterbits. I’ve been refining this particular feature, so the functionality has not changed much, but I’ve made it a lot easier to use from the scripting modules. As a refresher, here’s what it looks like in the game using simple linear movement:
// declare module
It takes a lot of setup just to get it walking back and forth on a path. Basically we’re manually lerping between two positions during the update cycle. Because this is a pretty common use case, I wanted to make it a lot easier to do, so I created a new
move_to function which leverages the animation subsystem to encapsulate this behavior. Here’s the new script:
// declare module
The new hotness is this line:
this.move_to(dest, MOVE_LENGTH * 1000, "lerp", callback);
This new function takes a destination and a duration and moves the sprite accordingly. Optional third and fourth arguments specify the algorithm to use (as with the previous post, I still only have lerp and quadratic ease-in defined) and a callback function. Now I can just use that callback to make the elk change direction when it reaches the end of the animation. From this perspective, it doesn’t look like a huge change, but this did take quite a bit of work. I had to refactor the animation system (it was hard-wired to do key frame animations) and then extend my callback system to be called from an arbitrary point in the engine (namely the animations).
Lastly you might’ve noticed my little
FIX_ME note. I realized as I was building this that some optimizations I’d done in the update loop broke expectations here. Because callbacks use the same context and timing as the update loop, it was easy to hook them into that part of the engine. But, because setting up the script calls is a non-zero-cost operation, I have an optimization which skips calling the script engine if the
update function doesn’t even exist. So thus I had to create an unneeded dummy
update to make sure callbacks would be called. I need to go back and detangle those pieces a bit to make sure this works.