Log In  
Log In  

@WaltCodes

Follow
Follow

Cart [#49937#] | Code | 2018-03-04 | No License | Embed
2

BROOKSATRON was a little "game jam" experiment I made with my little sister. We sat down one afternoon over Thanksgiving and I showed her what sort of wonders PICO-8 had to offer. Over the next couple days, she drew most of the sprites, composed the music, and drew out the levels. It was a lovely weekend.

P#49938 2018-03-04 15:49 ( Edited 2018-03-05 00:01)

Cart [#49906#] | Code | 2018-03-04 | License: CC4-BY-NC-SA | Embed
2

Hey y'all! I'm so excited to share an object-oriented component system I've been working on for PICO-8. I'm still super new to the platform, so I'm looking for any and all feedback you might have -- how to save tokens, smarter ways to loop over updates, cooler rendering techniques, anything!

I built it out as a toolset for the game I'm working on right now, so please forgive me if I've forgotten to take out anything too specialized. Let me know if you're able to use it on a project or if you have any feedback at all!

Thanks for reading! Below is the readme from the github page, linked below.

GITHUB: https://github.com/walt-er/compos

compos: reusable components for object-oriented PICO-8

compos: like "components", but with fewer characters!

compos are independent, reusable objects that can be added to your game's actors to give them certain behaviors. compos manage their own state, initialization, updating, and drawing. The only thing you might need to do is set some intitial values.

There's a fair amount of overhead for defining so many components right out of the gate. But hopefully the savings come down the line: it's easy to attach behaviors to actors independently, so defining large numbers of actors with similar behaviors is simple and doesn't require messy class inheritance. This system is build with procedural generation in mind -- it's easy to spawn complex actors on the fly, mixing and matching qualities without spending tokens.

The compos include:

  • Position
  • Size
  • Velocity
  • Gravity
  • Collider
  • Age
  • Patrol

This library also includes a number of helper functions, including methods for drawing sprites and primitives with outlines, integrated logging, generating vectors, copying tables, tiling sprites, and more.

More importantly, the methods for adding and removing actors from the active list, used in conjunction with the compos update pool (where actors and compos register to run their updates), mean that once you've defined an init(), update() or draw() function to an actor, they'll act just as you expect as they are added and removed from the global list of actors.

Starting with actors

compos loops over an "actors" array and runs the functions those actors and their components have registered for. For your entities to use the compos lifecycle, they will need to copy over the desired components and then be added to the global list of actors.

Here's an example of an object that draws an animating sprite in the middle of the screen:

local thingy = {
    physical = true, -- this inits the x, y, w, and y properties
    sprite = copy(sprite), -- this copies in the compo sprite component
    init = function(self) -- this runs on compo_init (or on demand if this actor is added with add_actor()
        translate(self, 60, 60) -- translate moves an actor to an x and y vector
        local spritesheet = split'0, 1, 2' -- split saves tokens by turning comma separated lists into arrays
        self.sprite:animate(self, spritesheet, 15, true) -- the third parameter is sprites-per-seocnd, the fourth is looping
    end
}
-- add to list of actors to be initialized and updated
add(actors, thingy)

Notice that the actor does not need to register any update or draw functions -- the sprite compo, when initialized, will register for all the lifecycle methods it requires.

If you're adding actors on the fly, use add_actor(). This method will run the required initialization and event registration before the actor is added to the scene.

Lifecycle and the update pool

compos will handle their own updates, but you'll need to add compos functions somewhere for them to run. If nothing else, add the three basic functions to your cart:

function _init()
    compos_init()
end

function _update()
    compos_update()
end

function _draw()
    cls() -- compos doesn't clear for you!
    compos_draw()
end

Behind the scenes, within those compos_* functions, there are various "pools" of actors and compos that have registered to run each frame. The update functions availible are early_update, update, late_update, and fixed_update, and drawing is done in early_update and update.

When an actor is initialized, it's update functions are registered in those pools and run in the order they are added. Keep that in mind for drawing -- actors added later will be drawn on top. (Note that I want to add an optional override for this soon! For now you can use early_draw to make sure things are drawn in first.)

It's important to remove actors by using remove_actor(), as opposed to, say, del(actors, thingy), because the remove_actor function also unregisters all events. Failing to use it could mean a memory leak as more and more actors are registered and none are rmeoved.

Integrating compos into your project

The most direct way to integrate compos into your project is simply copy pasting all of compos.lua into your cart, then deleting unwanted components and functions. There are a lot of functions included here, and cherrypicking what you want will save a ton of tokens. You probably don't need it all!

Integration can also be achieved using picotool, with some extra work. Just require('compos.lua') in your source pico8 file to include compos inside a "require" function. Just note that you'll need to delete the function wrapping the compo definitions for your code to reference them without errors. (NOTE: if you think I could get around this, let me know!)

You could also just hack the compo.p8 cart, using that as a jumping off point!

Demo: Bouncy Blobs

Here's some code that uses compos to draw hundreds of actors with positions, sizes, colors, and gravity:

SOURCE: https://github.com/walt-er/compos/blob/develop/demo.lua

Compos in action

P#49908 2018-03-03 19:38 ( Edited 2018-03-06 01:34)

Cart [#47851#] | Code | 2018-01-04 | No License | Embed

P#47852 2018-01-04 00:10 ( Edited 2018-01-04 16:37)

Cart [#47849#] | Code | 2018-01-04 | No License | Embed
1

This "demake" is a love letter to the amazing BLACK EMPEROR by Tomás Vicuña and BumbleBear Games. Race your motorcycle through the desert on randomly generated track that resets with the cart. Challenge your friends, memorize the course, and make it farther.

Each load of the cart generates a random course that increases in difficulty as you progress. One thing the game is definitely missing is music -- it's so important to the feeling of the original, but I'm worthless with music and so I failed to compose any. I currently have a friend working on a track -- here's the game as is, for now.

P#47850 2018-01-03 23:05 ( Edited 2018-03-05 18:53)

About | Contact | Updates | Terms of Use
Follow Lexaloffle:        
Generated 2019-05-27 09:02 | 0.086s | 4194k | Q:48