Git Product home page Git Product logo

roomy-playdate's Introduction

Roomy-Playdate

Toybox Compatible

Roomy-playdate is a scene management library for the Playdate console by Panic. It helps organize game code by the different "screens" in the game, such as the title screen, gameplay screen, and pause screen. Roomy was originally written for LÖVE by tesselode Github link

Install

Toybox

You can add it to your Playdate project by installing toybox.py, going to your project folder in a Terminal window and typing:

toybox add roomy
toybox update

Then, if your code is in the source folder, just import the following:

import '../toyboxes/toyboxes.lua'

Manual install

To install roomy, simply copy the roomy-playdate.lua file into your game directory and import it into your game.

import "roomy-playdate"

Usage

Defining scenes

A scene is defined by extending the Room or PauseRoom classes

class("Gameplay").extends(Room)

function Gameplay:enter(previous, ...)
	-- set up the level
end

function Gameplay:update(dt)
	-- update entities
end

function Gameplay:leave(next, ...)
	-- destroy entities and cleanup resources
end

function Gameplay:draw()
	-- draw the level
end

A scene table can contain anything, but it will likely have some combination of functions corresponding to input callbacks and Roomy events.

The PauseRoom is a convience class that will by default take a screen shot of the previous room and set it as the background sprite.

Creating a scene manager

local manager = Manager()

Creates a new scene manager. You can create as many scene managers as you want, but you'll most likely want one global manager for the main scenes of your game.

Switching scenes

manager:enter(scene, ...)

Changes the currently active scene.

Pushing/popping scenes

manager:push(scene, ...)
manager:pop()

Managers use a stack to hold scenes. You can push a scene onto the top of the stack, making it the currently active scene, and then pop it, resuming the previous state where it left off. This is useful for implementing pause screens, for example:

class("Pause").extends(PauseRoom)

function Pause:BButtonPressed()
	manager:pop()
end

class("Game").extends(Room)
function Game:BButtonPressed()
	manager:push(Pause())
end

There will always be at least one scene and calling pop when there is only one scene will have no effect.

Emitting events

manager:emit(event, ...)

Calls scene:[event] on the active scene if that function exists. Additional arguments are passed to scene.event.

Hooking into Playdate input callbacks

manager:hook(options)

Pushes an input handler that will emit events for each callback. options is an optional table with the following keys:

  • include - a list of callbacks to hook into. If this is defined, only these callbacks will be overridden.
  • exclude - a list of callbacks not to hook into. If this is defined, all of the callbacks except for these ones will be overridden.

As an example, the following code will cause the scene manager to hook into every callback except for AButtonDown and AButtonHeld.

manager:hook {
	exclude = {'AButtonDown', 'AButtonHeld'},
}

Scene callbacks

Scenes have a few special callbacks that are called when a scene is switched, pushed, or popped.

function scene:enter(previous, ...) end

Called when a manager switches to this scene or if this scene is pushed on top of another scene.

  • previous - the previously active scene, or {} if there was no previously active scene
  • ... - additional arguments passed to manager:enter or manager:push
function scene:leave(next, ...) end

Called when a manager switches away from this scene or if this scene is popped from the stack.

  • next - the scene that will be active next
  • ... - additional arguments passed to manager:enter or manager:pop
function scene:pause(next, ...) end

Called when a scene is pushed on top of this scene.

  • next - the scene that was pushed on top of this scene
  • ... - additional arguments passed to manager:push
  • the Room class will cache and remove all current sprites
function scene:resume(previous, ...) end

Called when a scene is popped and this scene becomes active again.

  • previous - the scene that was popped
  • ... - additional arguments passed to manager:pop
  • the Room class will add all sprites that were cached when the scene was paused

roomy-playdate's People

Contributors

robertcurry0216 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

roomy-playdate's Issues

Weird behavior when switching scene and keyRepeatTimers in place

Hello,
I found this odd behavior when switching scene with the usage of the KeyRepeat timers. I have reproduced my issue in this repository, which is a simple as possible. I am not sure what is going on honestly.
It may be linked to the fact that the code is still executed even after we switch scene, or the timers actually keep a reference to some handlers.
I would appreciate your input on this matter. Thanks!

If you run the example in the simulator. Just keep pressing A (holding it). When the counter reaches 11 the Pause Room will be called. It sometimes get stuck here (pressing B to pop the Pause Room will not be taken into account).

https://github.com/Schyzophrenic/keyRepeatRoomy

Room:leave is not called when room:enter(AnotherRoom()) is called

Hello,
I am not sure this is the expected behavior...
When I call roomManager:enter(AnotherRoom()) the function MyCurrentRoom:leave() does not seem to be called.

I noticed it by putting some transition code in the leave function but it would not trigger. Nonetheless, putting the same code just before the roomManager:enter(AnotherRoom()) works.

Is it the expected behavior?
Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.