levels.module
README.md
levels
The per-peer registry of the twoDKit's running pieces: every Level2D level, every Player2D body and every Camera2D, and the questions they ask each other: which level is at this point, what are its bounds, did this box touch spikes, where is the start, which body is mine. Players use it for spikes and gem pickups, cameras for clamping to the level and the rooms grid, the HUD for gem totals and level travel.
Each Level2D registers when it builds and unregisters when it rebuilds or goes away; players and cameras add themselves on awake. Nothing is synced: every peer builds the same level from the same text.
API
register(entry),unregister(id),get(id): level entries{ id, grid, ox, oy, oz, tileset, background, theme, gems, camera };gridis atwoDKit.levelgrid and(ox, oy)the world position of its bottom-left corner.all(): every registered level whose entity still exists, in registration order.bounds(entry) -> x0, y0, x1, y1: the level's world rectangle.at(x, y): the level containing the point, or the nearest one.spikeHit(x0, y0, x1, y1): does the world box touch any level's spikes?start(entry) -> x, y: world position (feet) of the level'sPstart marker, or nil.addPlayer/removePlayer,players(): every Player2D body on this peer, sorted.localPlayer(): the Player2D body this peer controls, or nil.addCamera/removeCamera,cameras().
local Levels = require("twoDKit.levels")
local me = Levels.localPlayer()
if me then
local lv = Levels.at(me.position.x, me.position.y)
if lv then print("gems in this level:", #lv.gems, "start:", Levels.start(lv)) end
end