Have anyone tried to make an ecs system work with Codea? Made an attempt using tiny-ecs, but it seems a bit limited compared to Bevy in rust for example.
--# Main
-- ECS
function setup()
-- download tinyecs if needed
if readProjectTab("Tinyecs") == "" then
http.request("https://raw.githubusercontent.com/bakpakin/tiny-ecs/master/tiny.lua", function (data)
data = string.gsub(data, "local tiny =", "tiny =")
saveProjectTab("Tinyecs", data)
print("please restart")
end)
else
viewer.mode = FULLSCREEN
setupWorld()
end
end
function setupWorld()
-- system to bounce all entities with pos and vel
local bounce = tiny.processingSystem()
bounce.filter = tiny.requireAll("pos", "vel")
function bounce:process(e, dt)
e.pos = e.pos + e.vel * dt
if e.pos.x < 0 or e.pos.x > WIDTH then
e.vel.x = -e.vel.x
end
if e.pos.y < 100 or e.pos.y > (HEIGHT-100) then
e.vel.y = -e.vel.y
end
end
-- system to draw all entities with pos
local drawSystem = tiny.processingSystem()
drawSystem.filter = tiny.requireAll("pos")
function drawSystem:process(e, dt)
rect(e.pos.x, e.pos.y, 5,5)
end
world = tiny.world(bounce, drawSystem)
-- add 20 random particles
for i = 1, 20 do
tiny.addEntity(world, {
pos=vec2(100+i*5, HEIGHT/2),
vel=vec2(math.random(150,300),0):rotate(i*10)
})
end
end
function draw()
background(40, 40, 50)
if world then
world:update(DeltaTime)
end
end
function touched(touch)
if touch.state ~= ENDED then
tiny.addEntity(world, {
pos=touch.pos,
vel=vec2(math.random(150,300),math.random(150,300))
})
end
end
--# Tinyecs