pico-kit
pico-kit copied to clipboard
:triangular_ruler: An opinionated set of Pico-8 helpers
Pico-Kit :triangular_ruler:
A collection of helpers to make your life easier. Comes with four libraries:
-
oop
-
oop.class(properties?, parents?, constructor?, metatable?) -> tableA multi-purpose class creator that returns a table that can be called with a table of properties to generate class instances. The class's constructor is automatically generated and does the following for every parent and then current instance:
- checks that all required properties are satisfied
- copies over first the default properties and then the properties passed to the constructor to the instance table
- calls the provided
constructorargument (if it exists)
Detailed explanation of arguments:
properties: A table of property keys/values that will be copied down to every instances. Properties with a value of"req"are considered unset and are required to be provided on an instance creation.parents: A list of parents. The class's__indexis set to these parents. Their constructors are also automatically called upon an instance creation.constructor: A function called at the end of instance creation.metatable: A metatable passed to every instance.
-
-
tools
-
tools.assign(table, initial={}) -> tableSimilar to javascript's
Object.assign: copies over a table to an empty table, or, if provided, an existing table. -
tools.assigndeep(table, initial={}) -> tableSimilar to
tools.assignbut it iterates through tables, effectively performing a deep copy.
-
-
debug
-
debug.tstr(table, indent?) -> strConverts a table to a string, optionally indenting each line with
indentnumber of spaces. -
debug.print(...)Prints values to the console with
printh. If it encounters nil it printsnil, and if it encounters a table it usesdebug.tstrto print out the entire table. Very useful for debugging because not only can you print multiple values at once, buttstrallows you to see the entire contents of even a nested table.
-
-
physics
-
physics.collided(body1, body2) -> boolReturns whether two bodies intersect.
-
physics.world(parameters) -> world-
properties
-
bodies={} -
gravity={0, .5}a vector that is applied withbody.shoveto every body duringworld.update, taking into consideration each bodies'weightandmass
-
-
physics.world:update()For each non-static body: applies gravity, friction, checks for collisions and then updates the position. Call this every frame.
-
physics.world:addbody(body) -> bodyAdds a body to the world and returns it.
-
-
physics.body(parameters) -> bodyA single physics body.
-
properties
pos="req"size="req"vel={0, 0}mass={1, 1}proportionally affects allbody.shoveandbody.slowcallsweight={1, 1}proportionally affects all gravity applied to the bodyfriction={.1, 0}applied withbody.sloweveryworld.updatecall, proportionally affects velocitycollisions={}a list of object collisions, reset duringworld.updatecallstatic=falsea static object that is ignored duringworld.updatelayer=0x1a binary field that represents collision layers. Everyworld.updatecalls abandoperation between two bodies'layerproperties determines whether they will be checked for a collision.
-
physics.body:update()Adds the current velocity to the position.
-
physics.body:shove(vector)For x and y, adds the vector multiplied by
body.massto the current velocity. -
physics.body:slow(vector)For x and y, subtracts the vector multiplied by
body.massfrom the current velocity. If the result of this operation crosses 0, the velocity on the corresponding axis is set to 0. -
physics.body:checkcollided(body)For x and y, checks if the current body and provided body would have collided with that axis of the body's velocity, and if so:
- Adds the provided body to
body.collisions. - Adjusts the body's position on that axis to the edge of the provided object.
- Sets that axis of the body's velocity to 0.
- Adds the provided body to
-
-
Setting up
To use this library, just download the starter.p8 file and open it in Pico. Check out example.p8 for a little example game.
Contributing
Suggested edits are welcome! Just create a pull request and make sure to edit both the starter and example files.