-- DOCS - General
-- game.register(gameid) - nil
-- game.update() - nil
-- game.render() - nil
-- DOCS - Variables
-- variables.get(variableName) - nil
-- variables.set(variableName, value) - nil
-- variables.increment(variableName, by) - nil
-- variables.decrement(variableName, by) - nil
-- DOCS - Sprite Variables
-- sprite.getvariable(spriteName, variableName) - nil
-- sprite.setvariable(spriteName, variableName, value) - nil
-- sprite.incrementvariable(spriteName, variableName, by) - nil
-- sprite.decrementvariable(spriteName, variableName, by) - nil
-- DOCS - PSIZ(s)
-- psiz.create(x, y, width, height) - PSIZ
-- DOCS - Sprites
-- sprite.create(spriteName, picoSpriteId, PSIZ, collide) - string
-- sprite.updatepsiz(spriteName, PSIZ) - nil
-- sprite.changepsiz(spriteName, PSIZ) - nil
-- sprite.get(spriteName) - Sprite
-- sprite.render(spriteName) - nil
-- sprite.renderall() - nil
-- sprite.updateall() - nil
-- sprite.registereventhandler(spriteName, function(type, me, hitobject)) - nil
-- DOCS - Animations
-- NOTE: this only supports frames made with a sprite number
-- animation.create(animationId, arrayOfFrames) - string
-- sprite.playanimation(spriteName, animationId) - nil
-- DOCS - Printing
-- game.printw(string) - number
-- game.printc(string, x, y) - nil
-- DOCS - Collision (automatically done by game.updateallsprites())
-- game.collidecheck(object1, object2) - bool
-- DOCS - Frames
-- frame.create(spriteId, framesShown) - frame
-- frame.createwithpsiz(spsiz, framesShown) - frame
-- DOCS - Particle
-- particle.create(arrayOfFrames, PSIZ, framesPerParticleFrame) - particle
-- particle.renderall() - nil
-- particle.updateall() - nil
-- Code
game = {
id = "unknown",
sprites = {},
variables = {},
}
enum = {
eventtype = {
collision = 0
}
}
function game.register(id)
game.id = id
end
function game.update()
sprite.updateall()
particle.updateall()
end
function game.render()
particle.renderall()
sprite.renderall()
end
-- variable get/set/increment/decrement
variables = {}
function variables.get(tableid)
return variables[tableid]
end
function variables.set(tableid, value)
variables[tableid] = value
end
function variables.increment(tableid, value)
variables[tableid] += value
end
function variables.decrement(tableid, value)
variables[tableid] -= value
end
-- spritevariable get/set/increment/decrement
sprite = {}
function sprite.getvariable(tableid, variableid)
return game.sprites[tableid].variables[variableid]
end
function sprite.setvariable(tableid, variableid, value)
game.sprites[tableid].variables[variableid] = value
end
function sprite.incrementvariable(tableid, variableid, value)
game.sprites[tableid].variables[variableid] += value
end
function sprite.decrementvariable(tableid, variableid, value)
game.sprites[tableid].variables[variableid] -= value
end
-- psiz
psiz = {}
function psiz.create(x,y,w,h)
local psiz = {
x = (x or 0),
y = (y or 0),
w = (w or 8),
h = (h or 8)
}
return psiz
end
-- sprites
animations = {}
function sprite.create(tableid, spriteid, psiz, collide)
local sprite = {
id = tableid,
sid = spriteid,
x = psiz.x,
y = psiz.y,
w = psiz.w,
h = psiz.h,
collide = (collide or false),
eventhandlers = {},
variables = {},
animsid = spriteid,
f = {},
cf = 1,
upd = 0,
tpf = 5,
playinganim = false,
update = function()
local spr = game.sprites[tableid]
if (spr ~= nil) then
if (spr.playinganim) then
local tpf = spr.f[spr.cf].t
if (spr.upd < tpf) then
spr.upd += 1
else
spr.cf += 1
spr.upd = 0
end
if (spr.cf > #spr.f) then
spr.playinganim = false
spr.animsid = spr.sid
else
spr.animsid = spr.f[spr.cf].sid
end
end
end
game.sprites[tableid] = spr
end,
destroy = function()
game.sprites[tableid] = nil
end
}
game.sprites[tableid] = sprite
return tableid
end
function sprite.updatepsiz(tableid, psiz)
local sprite = game.sprites[tableid]
sprite.x = psiz.x
sprite.y = psiz.y
sprite.w = psiz.w
sprite.h = psiz.h
game.sprites[tableid] = sprite
return tableid
end
function sprite.changepsiz(tableid, psiz)
local sprite = game.sprites[tableid]
sprite.x += psiz.x
sprite.y += psiz.y
sprite.w += psiz.w
sprite.h += psiz.h
game.sprites[tableid] = sprite
return tableid
end
function sprite.get(tableid)
return game.sprites[tableid]
end
function sprite.render(tableid)
local sprite = game.sprites[tableid]
if (not sprite.playinganim) then
spr(sprite.sid,sprite.x,sprite.y)
else
spr(sprite.animsid,sprite.x,sprite.y)
end
end
function sprite.renderall()
for k,v in pairs(game.sprites) do
if (v == nil) then goto skip2 end
sprite.render(k)
::skip2::
end
end
function sprite.updateall()
for k,v in pairs(game.sprites) do
if (v == nil) then goto skip end
v.update()
if (v.collide == true) then
for k2,v2 in pairs(game.sprites) do
if (k ~= k2 and v2.collide) then
if (game.collidecheck(v,v2)) then
for v3 in all(v.eventhandlers) do
v3(enum.eventtype.collision, v, v2)
end
goto skip
end
end
end
end
::skip::
end
end
function sprite.registereventhandler(tableid,handler)
add(game.sprites[tableid].eventhandlers,handler)
return tableid
end
-- animation + sprite extension
animation = {}
function animation.create(animationid, frames)
local anim = {
f = frames
}
animations[animationid] = anim
return animationid
end
function sprite.playanimation(tableid, animationid)
local anim = animations[animationid]
local spr = game.sprites[tableid]
spr.f = anim.f
spr.cf = 1
spr.upd = 0
spr.playinganim = true
end
-- printing
function game.printw(s)
if #s == 0 then
return 0
end
w = 0
for i = 1, #s do
if sub(s,i,i) >= "\x80" then
w += 7
else
w += 3
end
end
return w + #s - 1
end
function game.printc(s, x, y)
print(s, x - game.printw(s)/2, y)
end
-- collision
function game.collidecheck(obj1,obj2)
local hit = false
if max(obj1.x,obj1.x+obj1.w) >= min(obj2.x, obj2.x+obj2.w) and
min(obj1.x,obj1.x+obj1.w) <= max(obj2.x, obj2.x+obj2.w) then
if max(obj1.y,obj1.y+obj1.h) >= min(obj2.y, obj2.y+obj2.h) and
min(obj1.y,obj1.y+obj1.h) <= max(obj2.y, obj2.y+obj2.h) then
hit=true
end
end
return hit
end
-- frame
frame = {}
function frame.createwithpsiz(spsiz, tpf)
local frm = {
psiz = spsiz,
sid = nil,
t = tpf
}
return frm
end
function frame.create(spriteid, tpf)
local frm = {
psiz = nil,
sid = spriteid,
t = tpf
}
return frm
end
-- particles
particles = {}
particle = {}
function particle.create(frames, psiz)
local id = #particles+1
local prticle = {
x = psiz.x,
y = psiz.y,
f = frames,
cf = 1,
upd = 0,
update = function()
if particles[id] then
prtcle = particles[id]
frm = prtcle.f[prtcle.cf]
up = prtcle.upd
prtcle.upd += 1
if (prtcle.upd > frm.t) then
prtcle.cf += 1
prtcle.upd = 0
end
if (prtcle.cf > #frames) then
prtcle.destroy()
else
particles[id] = prtcle
end
end
end,
render = function()
if particles[id] then
prtcle = particles[id]
frm = prtcle.f[prtcle.cf]
if (frm ~= nil and frm.sid ~= nil) then
spr(frm.sid,prtcle.x,prtcle.y)
elseif (frm ~= nil) then
sspr(frm.psiz.x,frm.psiz.y,frm.psiz.w,frm.psiz.h,prtcle.x,prtcle.y)
end
end
end,
destroy = function()
deli(particles,id)
end
}
particles[id] = prticle
return id
end
function particle.renderall()
for k,v in pairs(particles) do
v.render()
end
end
function particle.updateall()
for k,v in pairs(particles) do
v.update()
end
end