ecs refactor
This commit is contained in:
parent
851aa65d6e
commit
c4bb52b896
8 changed files with 147 additions and 107 deletions
|
|
@ -1,24 +0,0 @@
|
||||||
local browser = {}
|
|
||||||
|
|
||||||
browser.to = function(url,refererer)
|
|
||||||
print("surfing to " .. url)
|
|
||||||
local URL = api.url.parse(url)
|
|
||||||
foreach( api.protocol, function(name, p)
|
|
||||||
if URL.protocol:match("^"..name) then
|
|
||||||
protocol = p
|
|
||||||
end
|
|
||||||
end)
|
|
||||||
if protocol then
|
|
||||||
local status, data, headers = protocol.request(url)
|
|
||||||
local ctx = {
|
|
||||||
succes = (status >= 200 and status < 300),
|
|
||||||
status = status,
|
|
||||||
data = data,
|
|
||||||
headers = headers,
|
|
||||||
URL = URL
|
|
||||||
}
|
|
||||||
api.exec( api.ext, 'renderURI', ctx )
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
return browser
|
|
||||||
32
src/ext/3DFile/main.lua
Normal file
32
src/ext/3DFile/main.lua
Normal file
|
|
@ -0,0 +1,32 @@
|
||||||
|
local api = ...
|
||||||
|
local ecs = api.ecs
|
||||||
|
|
||||||
|
return {
|
||||||
|
name = "3DFile",
|
||||||
|
enabled = true,
|
||||||
|
|
||||||
|
init = function()
|
||||||
|
modelLoader = ecs.processingSystem()
|
||||||
|
modelLoader.updatesystem = true
|
||||||
|
modelLoader.filter = ecs.requireAll('URL', 'method', 'data', 'ok')
|
||||||
|
function modelLoader:process(req, dt)
|
||||||
|
if req.ok and req.model == nil then
|
||||||
|
if req.URL.extension == 'OBJ' or
|
||||||
|
req.URL.extension == 'GLB' or
|
||||||
|
req.URL.extension == 'GLTF' then
|
||||||
|
|
||||||
|
req.model = api.graphics.newModel( api.data.newBlob(req.data) )
|
||||||
|
api.world.add({
|
||||||
|
x = 0,
|
||||||
|
y = 0,
|
||||||
|
z = 0,
|
||||||
|
model = req.model,
|
||||||
|
req = req
|
||||||
|
})
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
api.world.add(modelLoader)
|
||||||
|
end
|
||||||
|
|
||||||
|
}
|
||||||
38
src/ext/browser/main.lua
Normal file
38
src/ext/browser/main.lua
Normal file
|
|
@ -0,0 +1,38 @@
|
||||||
|
local api = ...
|
||||||
|
local ecs = api.ecs
|
||||||
|
|
||||||
|
return {
|
||||||
|
name = "browser",
|
||||||
|
enabled = true,
|
||||||
|
|
||||||
|
init = function()
|
||||||
|
urlListener = ecs.processingSystem({
|
||||||
|
updatesystem = true,
|
||||||
|
filter = ecs.requireAll('url', 'method'),
|
||||||
|
onAdd = function(self,obj)
|
||||||
|
api.ext.browser.to(obj.url, nil, obj)
|
||||||
|
end
|
||||||
|
})
|
||||||
|
api.world.add(urlListener)
|
||||||
|
end,
|
||||||
|
|
||||||
|
to = function(url,refererer, opts)
|
||||||
|
opts = opts or {method = 'GET' }
|
||||||
|
print("surfing to " .. url)
|
||||||
|
local URL = api.url.parse(url)
|
||||||
|
foreach( api.protocol, function(name, p)
|
||||||
|
if URL.protocol:match("^"..name) then
|
||||||
|
protocol = p
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
if protocol then
|
||||||
|
local status, data, headers = protocol.request(url)
|
||||||
|
opts.ok = (status >= 200 and status < 300)
|
||||||
|
opts.status = status
|
||||||
|
opts.data = data
|
||||||
|
opts.headers = headers
|
||||||
|
opts.URL = URL
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -1,27 +0,0 @@
|
||||||
local api = ...
|
|
||||||
|
|
||||||
local gltf = {
|
|
||||||
name = "gltf",
|
|
||||||
enabled = true,
|
|
||||||
|
|
||||||
init = function() end,
|
|
||||||
|
|
||||||
renderURI = function(ctx)
|
|
||||||
local URL = ctx.URL
|
|
||||||
local me = api.ext.gltf
|
|
||||||
if ctx.succes and URL.extension == 'GLB' or URL.extension == 'GLTF' then
|
|
||||||
local model = api.graphics.newModel( api.data.newBlob(ctx.data) )
|
|
||||||
local ecs = api.ecs
|
|
||||||
ecs.add( ecs.world, {
|
|
||||||
x = 0,
|
|
||||||
y = 0,
|
|
||||||
z = 0,
|
|
||||||
model = model
|
|
||||||
})
|
|
||||||
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
return gltf
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
local api = ...
|
local api = ...
|
||||||
|
local ecs = api.ecs
|
||||||
local util = api.util
|
local util = api.util
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|
@ -6,30 +7,37 @@ return {
|
||||||
enabled = true,
|
enabled = true,
|
||||||
|
|
||||||
init = function()
|
init = function()
|
||||||
|
JMLLoader = ecs.processingSystem()
|
||||||
|
JMLLoader.updatesystem = true
|
||||||
|
JMLLoader.filter = ecs.requireAll('URL', 'method', 'data', 'ok')
|
||||||
|
function JMLLoader:process(req, dt)
|
||||||
|
if req.ok and req.xml == nil then
|
||||||
|
|
||||||
local xml = api.parser.xml.newParser()
|
-- JML heuristic
|
||||||
local xmlstr = [[
|
if req.data.find("<fireboxroom>") or req.data.match("<room[ >]") then
|
||||||
<room use_local_asset="room3">
|
|
||||||
<object id="foo.glb"/>
|
local JML0
|
||||||
<object id="cube"/>
|
local JML
|
||||||
</room>
|
req.xml = api.parser.xml.newParser()
|
||||||
]]
|
local xmlstr = req.data
|
||||||
util.traverseXML( xml:ParseXmlText(xmlstr), function(node,raw)
|
-- cleanup JML0
|
||||||
print_r(node)
|
local JML0 = xmlstr:gsub("[=%w].*<room>","<fireboxroom")
|
||||||
end)
|
JML0 = JML0:gsub("[=%w]</room>","</room>")
|
||||||
|
util.traverseXML( req.xml:ParseXmlText(JML0), function(node,raw)
|
||||||
|
print_r(node)
|
||||||
|
end)
|
||||||
|
os.exit()
|
||||||
|
--api.world.add({
|
||||||
|
-- x = 0,
|
||||||
|
-- y = 0,
|
||||||
|
-- z = 0,
|
||||||
|
-- model = req.model,
|
||||||
|
-- req = req
|
||||||
|
--})
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
api.world.add(JMLLoader)
|
||||||
|
|
||||||
end,
|
end,
|
||||||
|
|
||||||
renderURI = function(ctx)
|
|
||||||
-- TODO:
|
|
||||||
-- janusxr should be an ECS SYSTEM respond to inserted xmls
|
|
||||||
-- gltf should be a ECS SYSTEM respond to inserted objects
|
|
||||||
-- browser should insert url response to entity (respond to 404 + autodelete entity e.g.)
|
|
||||||
--
|
|
||||||
--local URL = ctx.URL
|
|
||||||
--local me = api.ext.gltf
|
|
||||||
--if ctx.succes and URL.extension == 'GLB' or URL.extension == 'GLTF' then
|
|
||||||
|
|
||||||
--end
|
|
||||||
end
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,20 +6,23 @@ return {
|
||||||
enabled = true,
|
enabled = true,
|
||||||
init = function()
|
init = function()
|
||||||
-- create a (ecs) system which detects add/remove entities
|
-- create a (ecs) system which detects add/remove entities
|
||||||
local ecs = api.ecs
|
local ecs = api.ecs
|
||||||
xrfsystem = ecs.system({
|
xrfsystem = ecs.system({
|
||||||
|
|
||||||
filter = ecs.filter('x&y&z'),
|
filter = ecs.filter('x', 'y', 'z', 'model'),
|
||||||
|
|
||||||
onAdd = function(self,obj)
|
onAdd = function(self,obj)
|
||||||
xrf.traverseNodesContaining('href', obj,
|
|
||||||
xrf.makeClickable( api.ecs.worldPhysics,
|
if lovr ~= nil then
|
||||||
function(obj, collider)
|
xrf.traverseNodesContaining('href', obj,
|
||||||
print(obj['name'] .. ".href => " .. obj['extras']['href'] )
|
xrf.makeClickable( api.ecs.worldPhysics,
|
||||||
api.ecs.add( ecs.world, {collider = collider})
|
function(obj, collider)
|
||||||
end
|
print(obj['name'] .. ".href => " .. obj['extras']['href'] )
|
||||||
|
api.world.add({collider = collider})
|
||||||
|
end
|
||||||
|
)
|
||||||
)
|
)
|
||||||
)
|
end
|
||||||
end,
|
end,
|
||||||
|
|
||||||
onRemove = function(self,obj)
|
onRemove = function(self,obj)
|
||||||
|
|
@ -27,7 +30,7 @@ return {
|
||||||
end
|
end
|
||||||
|
|
||||||
})
|
})
|
||||||
ecs.addSystem( ecs.world, xrfsystem )
|
ecs.addSystem( api.world, xrfsystem )
|
||||||
|
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -13,8 +13,11 @@ iui.resourcePath = "lovr/" .. iui.resourcePath
|
||||||
backend.resourcePath = "lovr/" .. backend.resourcePath
|
backend.resourcePath = "lovr/" .. backend.resourcePath
|
||||||
api.iui = iui
|
api.iui = iui
|
||||||
api.backend = backend
|
api.backend = backend
|
||||||
|
-- decorate api
|
||||||
|
api.protocol.http = require('http')
|
||||||
local ecs = api.ecs
|
local ecs = api.ecs
|
||||||
|
|
||||||
|
|
||||||
-- ecs systems
|
-- ecs systems
|
||||||
local interactionsUpdater
|
local interactionsUpdater
|
||||||
local renderer
|
local renderer
|
||||||
|
|
@ -49,7 +52,7 @@ function lovr.update(dt)
|
||||||
lovr.event.quit()
|
lovr.event.quit()
|
||||||
end
|
end
|
||||||
|
|
||||||
ecs.update( ecs.world, dt, ecs.filter('updatesystem') )
|
ecs.update( api.world, dt, ecs.filter('updatesystem') )
|
||||||
|
|
||||||
iui.beginFrame(dt)
|
iui.beginFrame(dt)
|
||||||
|
|
||||||
|
|
@ -89,7 +92,7 @@ function lovr.draw(pass)
|
||||||
end
|
end
|
||||||
|
|
||||||
api.exec(api.ext, "draw",pass)
|
api.exec(api.ext, "draw",pass)
|
||||||
ecs.update( ecs.world, pass, ecs.filter('drawsystem') )
|
ecs.update( api.world, pass, ecs.filter('drawsystem') )
|
||||||
|
|
||||||
-- Dot
|
-- Dot
|
||||||
if selectedBox then
|
if selectedBox then
|
||||||
|
|
@ -129,7 +132,7 @@ if launch.mode == "desktop" then
|
||||||
function lovr.mousereleased(x, y, button)
|
function lovr.mousereleased(x, y, button)
|
||||||
backend.mousereleased(x, y, button)
|
backend.mousereleased(x, y, button)
|
||||||
interactionsUpdater['mouse']['released'] = {x=x, y=y, button=button}
|
interactionsUpdater['mouse']['released'] = {x=x, y=y, button=button}
|
||||||
print("ja")
|
print("mouserelease")
|
||||||
end
|
end
|
||||||
|
|
||||||
function lovr.wheelmoved(x, y)
|
function lovr.wheelmoved(x, y)
|
||||||
|
|
@ -151,8 +154,6 @@ if launch.mode == "desktop" then
|
||||||
end
|
end
|
||||||
|
|
||||||
local initECS = function(ecs)
|
local initECS = function(ecs)
|
||||||
ecs.world = ecs.world()
|
|
||||||
|
|
||||||
-- lovr.draw => ecs.drawsystem (render-logic thread)
|
-- lovr.draw => ecs.drawsystem (render-logic thread)
|
||||||
renderer = ecs.processingSystem()
|
renderer = ecs.processingSystem()
|
||||||
renderer.filter = ecs.requireAny('model')
|
renderer.filter = ecs.requireAny('model')
|
||||||
|
|
@ -169,7 +170,7 @@ local initECS = function(ecs)
|
||||||
1, 0, 1, 0, 0, 1)
|
1, 0, 1, 0, 0, 1)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
ecs.addSystem( ecs.world, renderer )
|
ecs.addSystem( api.world, renderer )
|
||||||
|
|
||||||
-- lovr.update => ecs updatesystem (game-logic thread)
|
-- lovr.update => ecs updatesystem (game-logic thread)
|
||||||
local updater = ecs.processingSystem()
|
local updater = ecs.processingSystem()
|
||||||
|
|
@ -178,7 +179,7 @@ local initECS = function(ecs)
|
||||||
function updater:process(obj, dt)
|
function updater:process(obj, dt)
|
||||||
print_r(obj['data'])
|
print_r(obj['data'])
|
||||||
end
|
end
|
||||||
ecs.addSystem( ecs.world, updater )
|
ecs.addSystem( api.world, updater )
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -217,7 +218,7 @@ function initInteractions(ecs)
|
||||||
mouse['released'] = false
|
mouse['released'] = false
|
||||||
mouse['pressed'] = false
|
mouse['pressed'] = false
|
||||||
end
|
end
|
||||||
ecs.addSystem( ecs.world, interactionsUpdater )
|
ecs.addSystem( api.world, interactionsUpdater )
|
||||||
end
|
end
|
||||||
|
|
||||||
initECS(api.ecs)
|
initECS(api.ecs)
|
||||||
|
|
|
||||||
39
src/main.lua
39
src/main.lua
|
|
@ -1,32 +1,41 @@
|
||||||
local util = require("util")
|
local util = require("util")
|
||||||
|
local ecs = require("tiny-ecs")
|
||||||
|
|
||||||
api = {
|
api = {
|
||||||
parser = {
|
parser = {
|
||||||
json = require("json"),
|
json = require("json"),
|
||||||
xml = require("xmlSimple"),
|
xml = require("xmlSimple"),
|
||||||
},
|
},
|
||||||
browser = require("browser"),
|
|
||||||
url = require("url"),
|
url = require("url"),
|
||||||
util = require("util"),
|
util = require("util"),
|
||||||
ecs = require("tiny-ecs"),
|
ecs = ecs,
|
||||||
|
world = ecs.world(),
|
||||||
|
protocol = {},
|
||||||
ext = {}, -- all extensions loaded from disk at runtime
|
ext = {}, -- all extensions loaded from disk at runtime
|
||||||
exec = function(...) util.exec(...) end -- calls function on each table item
|
exec = function(...) util.exec(...) end -- calls function on each table item
|
||||||
}
|
}
|
||||||
if lovr ~= nil then
|
|
||||||
api = util.merge( api, lovr )
|
-- convenience wrappers
|
||||||
api['protocol'] = {
|
api.world.add = function(...) api.ecs.add( api.world, ...) end
|
||||||
http = require('http')
|
api.world.remove = function(...) api.ecs.remove( api.world, ...) end
|
||||||
},
|
|
||||||
util.loaddir( "ext", api, api.ext )
|
local runtime
|
||||||
util.loaddir( "media", api, api.media )
|
local runtimepath
|
||||||
require("lovr/main")
|
|
||||||
api.exec( api.ext, 'init')
|
if lovr ~= nil then runtime = { path = "lovr", api = lovr } end
|
||||||
end
|
if love ~= nil then runtime = { path = "love", api = love } end
|
||||||
|
|
||||||
|
api = util.merge( api, runtime.api )
|
||||||
|
util.loaddir( "ext", api, api.ext )
|
||||||
|
util.loaddir( "media", api, api.media )
|
||||||
|
|
||||||
|
api.exec( api.ext, 'init')
|
||||||
|
require( runtime.path .. "/main")
|
||||||
|
|
||||||
local url='https://coderofsalvation.codeberg.page/xrfragment-haxe/example/assets/example.glb?bar=1&f=2#foo'
|
local url='https://coderofsalvation.codeberg.page/xrfragment-haxe/example/assets/example.glb?bar=1&f=2#foo'
|
||||||
--local url = 'https://codeberg.org/coderofsalvation/xrfragment/raw/branch/main/assets/template/website/website.glb'
|
--local url = 'https://codeberg.org/coderofsalvation/xrfragment/raw/branch/main/assets/template/website/website.glb'
|
||||||
--local url = 'https://codeberg.org/coderofsalvation/xrfragment/raw/branch/main/assets/simple-a.glb'
|
--local url = 'https://codeberg.org/coderofsalvation/xrfragment/raw/branch/main/assets/simple-a.glb'
|
||||||
|
--local url = 'https://janusxr.org/index.html'
|
||||||
|
|
||||||
if lovr ~= nil then -- TODO: make this work for LOVE2D too
|
|
||||||
api.browser.to(url)
|
api.world.add({ url = url, method = 'GET' })
|
||||||
end
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue