Hey all!
I've recently worked on a couple of different libraries for the PICO-8, which are available on Github. I've posted them around on the subreddit and Discord server to good responses, however have held back from posting here until now!
So without further adieu..
PICO-Tween - Repository |
PICO-Tween is a port of the easing functions developed by Robert Penner. This port was based on the Lua port by EmmanuelOga and optimised to suit the PICO-8 by refactoring to remove unnecessary tokens and making it compatible with the way the PICO-8 handles math functions.
It comes packaged with some various sourced and ported additional math functions to make some things work, such as asin and acos.
It comes with a pretty hefty token count if using every tween, at 2083 tokens, however on average each easing function is only around 40-60 tokens so can just be picked out and slotted in where appropriate along with any math dependencies.
It should come pretty easy to use, if you have any experience with tweens, particularly of the Penner variety, in any other language or framework. Here's a small code example that you can paste directly into a cart to see how to get set up with something basic like linear tween:
function linear(t, b, c, d) return c * t / d + b end local distance = 50 local duration = 1 function downFunc(v) return linear(v, 0, distance, duration) end function upFunc(v) return linear(v, distance, -distance, duration) end local easeProp = 0 local timeElapsed = 0 local currentFunc = downFunc local lastTime = time() local dt = 0 function _update() t = time() dt = t - lastTime lastTime = t timeElapsed += dt if timeElapsed > duration then timeElapsed = 0 if currentFunc == downFunc then currentFunc = upFunc else currentFunc = downFunc end end easeProp = currentFunc(timeElapsed) end function _draw() rectfill(0, 0, 128, 128, 3) circfill(64, 40 + easeProp, 20, 15) end |
Which should net you the following results on screen:
For more example gifs, a demo cart which allows you to cycle through all available tweening functions, and of course the source code, please check out the repository here: Repository
PICO-TweenMachine - Repository |
However, whilst that's all well and good, it can get quite cumbersome to keep track of and add tween code. There's a lot of increasingly painful boilerplate and it can introduce mess into your codebase pretty quickly.
Not to mention that it can be a bit confusing to both read and write for those less experienced!
That's where PICO-TweenMachine comes in! PICO-TweenMachine is a small wrapper extension library intended to support the PICO-Tween library by removing the excess noise of writing tweens and driving them from a centralized place.
Instead of the traditional approach, that's all taken care of by the tween_machine object, leaving you to simply set up your tween object with the values you want, such as start and end point, duration and tween type, then leave the wrapper to drive the rest.
All you need to do is add your object to the wrapper, set up any callbacks you may like to occur during step events or upon tween completion and then call it in your update function.
Here's a code example taking the earlier sample and porting it for use with the new library (this sample assumes the PICO-TweenMachine library is pasted to the top of the cart and won't run just by pasting in this sample):
function linear(t, b, c, d) return c * t / d + b end local move_distance = 30 local move_duration = 1 local easeprop = 0 function set_ball_position(position) easeprop = position end function reverse_ball_direction(tween) tween.v_start = tween.v_end tween.v_end = -tween.v_end tween:restart() end function _init() ball_tween = tween_machine:add_tween({ func = linear, v_start = -move_distance, v_end = move_distance, duration = move_duration }) ball_tween:register_step_callback(set_ball_position) ball_tween:register_finished_callback(reverse_ball_direction) end function _update() tween_machine:update() end function _draw() rectfill(0, 0, 128, 128, 3) circfill(64, 60 + easeprop, 20, 15) end |
As you can see, this approach is much clearer and easy to both understand and extend.
The library is both simple to set up and use, as well as very light at only 239 tokens.
Example Images |
Links |
Hopefully some of you will appreciate this, if so, please go ahead and test out the demo carts, download the sources and use these libraries in your own projects! I would also appreciate the stars on Github. ;)
PICO-Tween - Repository
PICO-TweenMachine - Repository
Thank you all for reading, I appreciate it!
[Please log in to post a comment]