Log inGet started
World

Pixel Ledges: 2D Platformer Kit with Sprites, Tilemaps, Wall Jumps and Dash

README.md

events

A tiny local event bus for twoDKit's game moments: jump, land, dash, wallJump, gem, checkpoint, death, gameOver, restart and goal. A HUD, a sound bank, an achievement tracker or your own game code listens to them instead of polling fields.

Moments fire on every peer, for every player that peer sees, from state that is already synced (Sprite2D's animation changes, Player2D's gems and finished, Trapline's checkpoint, lifeLost, gameOver and restarted events), so listening costs no network traffic. The kit's own effects and sounds (twoDKit.juice) are driven the same way.

API

  • Events.on(name, fn) -> connection: call fn(payload) for every name moment ("*" hears all of them). connection:Disconnect() stops it.
  • Events.once(name, fn) -> connection: only the next one.
  • Events.emit(name, payload?) -> payload: fire a moment on this peer (the kit does this; your game may emit its own names too).
  • Events.count(name): listeners of a moment.
  • Events.NAMES: the kit's moment names.

Every payload carries event, player (the body's entity id), x and y (its feet, in world units), face (1 or -1) and isLocal (true when it is this peer's own player). Extra fields: land has hard and drop (tiles fallen); dash has dx, dy (its direction, fired a couple of frames in); gem has count; checkpoint has first and label; goal has gems and time; gameOver has deaths. death fires as the body goes down (its hurt animation starts).

local Events = require("twoDKit.events")
Events.on("gem", function(e)
    if e.isLocal then print("gems now", e.count) end
end)