mirror of
https://github.com/Ukendio/jecs.git
synced 2025-04-24 17:10:03 +00:00
Provide better error messages (#7)
* Provide better error messages * Inline getSmallestMap
This commit is contained in:
parent
6adc3670ee
commit
2ace031fa2
2 changed files with 19 additions and 43 deletions
|
@ -4,11 +4,8 @@
|
||||||
local ReplicatedStorage = game:GetService("ReplicatedStorage")
|
local ReplicatedStorage = game:GetService("ReplicatedStorage")
|
||||||
local rgb = require(ReplicatedStorage.rgb)
|
local rgb = require(ReplicatedStorage.rgb)
|
||||||
local Matter = require(ReplicatedStorage.DevPackages.Matter)
|
local Matter = require(ReplicatedStorage.DevPackages.Matter)
|
||||||
local Rewrite = require(ReplicatedStorage.rewrite)
|
|
||||||
local ecr = require(ReplicatedStorage.DevPackages.ecr)
|
local ecr = require(ReplicatedStorage.DevPackages.ecr)
|
||||||
local newWorld = Matter.World.new()
|
local newWorld = Matter.World.new()
|
||||||
local world = Rewrite.World.new()
|
|
||||||
local component = Rewrite.component
|
|
||||||
|
|
||||||
local jecs = require(ReplicatedStorage.Lib)
|
local jecs = require(ReplicatedStorage.Lib)
|
||||||
local mirror = require(ReplicatedStorage.mirror)
|
local mirror = require(ReplicatedStorage.mirror)
|
||||||
|
@ -33,15 +30,6 @@ local B6 = ecr.component()
|
||||||
local B7 = ecr.component()
|
local B7 = ecr.component()
|
||||||
local B8 = ecr.component()
|
local B8 = ecr.component()
|
||||||
|
|
||||||
local C1 = component()
|
|
||||||
local C2 = component()
|
|
||||||
local C3 = component()
|
|
||||||
local C4 = component()
|
|
||||||
local C5 = component()
|
|
||||||
local C6 = component()
|
|
||||||
local C7 = component()
|
|
||||||
local C8 = component()
|
|
||||||
|
|
||||||
local D1 = ecs:entity()
|
local D1 = ecs:entity()
|
||||||
local D2 = ecs:entity()
|
local D2 = ecs:entity()
|
||||||
local D3 = ecs:entity()
|
local D3 = ecs:entity()
|
||||||
|
@ -78,13 +66,11 @@ for i = 1, N do
|
||||||
local combination = ""
|
local combination = ""
|
||||||
local n = newWorld:spawn()
|
local n = newWorld:spawn()
|
||||||
local entity = ecs:entity()
|
local entity = ecs:entity()
|
||||||
local e = world:spawn()
|
|
||||||
local m = mcs:entity()
|
local m = mcs:entity()
|
||||||
|
|
||||||
if flip() then
|
if flip() then
|
||||||
combination ..= "B"
|
combination ..= "B"
|
||||||
registry2:set(id, B2, {value = true})
|
registry2:set(id, B2, {value = true})
|
||||||
world:insert(e, C2({ value = true}))
|
|
||||||
ecs:set(entity, D2, { value = true})
|
ecs:set(entity, D2, { value = true})
|
||||||
mcs:set(m, E2, { value = 2})
|
mcs:set(m, E2, { value = 2})
|
||||||
newWorld:insert(n, A2({value = true}))
|
newWorld:insert(n, A2({value = true}))
|
||||||
|
@ -92,7 +78,6 @@ for i = 1, N do
|
||||||
if flip() then
|
if flip() then
|
||||||
combination ..= "C"
|
combination ..= "C"
|
||||||
registry2:set(id, B3, {value = true})
|
registry2:set(id, B3, {value = true})
|
||||||
world:insert(e, C3({ value = true}))
|
|
||||||
ecs:set(entity, D3, { value = true})
|
ecs:set(entity, D3, { value = true})
|
||||||
mcs:set(m, E3, { value = 2})
|
mcs:set(m, E3, { value = 2})
|
||||||
newWorld:insert(n, A3({value = true}))
|
newWorld:insert(n, A3({value = true}))
|
||||||
|
@ -100,7 +85,6 @@ for i = 1, N do
|
||||||
if flip() then
|
if flip() then
|
||||||
combination ..= "D"
|
combination ..= "D"
|
||||||
registry2:set(id, B4, {value = true})
|
registry2:set(id, B4, {value = true})
|
||||||
world:insert(e, C4({ value = true}))
|
|
||||||
ecs:set(entity, D4, { value = true})
|
ecs:set(entity, D4, { value = true})
|
||||||
mcs:set(m, E4, { value = 2})
|
mcs:set(m, E4, { value = 2})
|
||||||
|
|
||||||
|
@ -109,7 +93,6 @@ for i = 1, N do
|
||||||
if flip() then
|
if flip() then
|
||||||
combination ..= "E"
|
combination ..= "E"
|
||||||
registry2:set(id, B5, {value = true})
|
registry2:set(id, B5, {value = true})
|
||||||
world:insert(e, C5({value = true}))
|
|
||||||
ecs:set(entity, D5, { value = true})
|
ecs:set(entity, D5, { value = true})
|
||||||
mcs:set(m, E5, { value = 2})
|
mcs:set(m, E5, { value = 2})
|
||||||
|
|
||||||
|
@ -118,26 +101,20 @@ for i = 1, N do
|
||||||
if flip() then
|
if flip() then
|
||||||
combination ..= "F"
|
combination ..= "F"
|
||||||
registry2:set(id, B6, {value = true})
|
registry2:set(id, B6, {value = true})
|
||||||
world:insert(e, C6({value = true}))
|
|
||||||
ecs:set(entity, D6, { value = true})
|
ecs:set(entity, D6, { value = true})
|
||||||
mcs:set(m, E6, { value = 2})
|
mcs:set(m, E6, { value = 2})
|
||||||
|
|
||||||
newWorld:insert(n, A6({value = true}))
|
newWorld:insert(n, A6({value = true}))
|
||||||
end
|
end
|
||||||
if flip() then
|
if flip() then
|
||||||
combination ..= "G"
|
combination ..= "G"
|
||||||
registry2:set(id, B7, {value = true})
|
registry2:set(id, B7, {value = true})
|
||||||
world:insert(e, C7{ value = true})
|
|
||||||
ecs:set(entity, D7, { value = true})
|
ecs:set(entity, D7, { value = true})
|
||||||
mcs:set(m, E7, { value = 2})
|
mcs:set(m, E7, { value = 2})
|
||||||
|
|
||||||
|
|
||||||
newWorld:insert(n, A7({value = true}))
|
newWorld:insert(n, A7({value = true}))
|
||||||
end
|
end
|
||||||
if flip() then
|
if flip() then
|
||||||
combination ..= "H"
|
combination ..= "H"
|
||||||
registry2:set(id, B8, {value = true})
|
registry2:set(id, B8, {value = true})
|
||||||
world:insert(e, C8{ value = true})
|
|
||||||
newWorld:insert(n, A8({value = true}))
|
newWorld:insert(n, A8({value = true}))
|
||||||
ecs:set(entity, D8, { value = true})
|
ecs:set(entity, D8, { value = true})
|
||||||
mcs:set(m, E8, { value = 2})
|
mcs:set(m, E8, { value = 2})
|
||||||
|
@ -148,7 +125,6 @@ for i = 1, N do
|
||||||
combination = "A" .. combination
|
combination = "A" .. combination
|
||||||
common += 1
|
common += 1
|
||||||
registry2:set(id, B1, {value = true})
|
registry2:set(id, B1, {value = true})
|
||||||
world:insert(e, C1{ value = true})
|
|
||||||
ecs:set(entity, D1, { value = true})
|
ecs:set(entity, D1, { value = true})
|
||||||
newWorld:insert(n, A1({value = true}))
|
newWorld:insert(n, A1({value = true}))
|
||||||
mcs:set(m, E1, { value = 2})
|
mcs:set(m, E1, { value = 2})
|
||||||
|
@ -194,7 +170,7 @@ return {
|
||||||
end,
|
end,
|
||||||
|
|
||||||
Functions = {
|
Functions = {
|
||||||
Mater = function()
|
Matter = function()
|
||||||
local matched = 0
|
local matched = 0
|
||||||
for entityId, firstComponent in newWorld:query(A1, A4, A6, A8) do
|
for entityId, firstComponent in newWorld:query(A1, A4, A6, A8) do
|
||||||
matched += 1
|
matched += 1
|
||||||
|
|
36
lib/init.lua
36
lib/init.lua
|
@ -198,7 +198,7 @@ export type World = typeof(World.new())
|
||||||
|
|
||||||
local function ensureArchetype(world: World, types, prev)
|
local function ensureArchetype(world: World, types, prev)
|
||||||
if #types < 1 then
|
if #types < 1 then
|
||||||
|
return world.ROOT_ARCHETYPE
|
||||||
end
|
end
|
||||||
local ty = hash(types)
|
local ty = hash(types)
|
||||||
local archetype = world.archetypeIndex[ty]
|
local archetype = world.archetypeIndex[ty]
|
||||||
|
@ -350,31 +350,31 @@ local function noop(): any
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function getSmallestMap(componentIndex, components)
|
|
||||||
local s: any
|
|
||||||
|
|
||||||
for i, componentId in components do
|
|
||||||
local map = componentIndex[componentId]
|
|
||||||
if s == nil or map.size < s.size then
|
|
||||||
s = map
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
return s.sparse
|
|
||||||
end
|
|
||||||
|
|
||||||
function World.query(world: World, ...: i53): any
|
function World.query(world: World, ...: i53): any
|
||||||
local compatibleArchetypes = {}
|
local compatibleArchetypes = {}
|
||||||
local components = { ... }
|
local components = { ... }
|
||||||
local archetypes = world.archetypes
|
local archetypes = world.archetypes
|
||||||
local queryLength = #components
|
local queryLength = #components
|
||||||
local firstArchetypeMap = getSmallestMap(world.componentIndex, components)
|
|
||||||
|
|
||||||
if not firstArchetypeMap then
|
if queryLength == 0 then
|
||||||
return noop()
|
error("Missing components")
|
||||||
end
|
end
|
||||||
|
|
||||||
for id in firstArchetypeMap do
|
local firstArchetypeMap
|
||||||
|
local componentIndex = world.componentIndex
|
||||||
|
|
||||||
|
for i, componentId in components do
|
||||||
|
local map = componentIndex[componentId]
|
||||||
|
if not map then
|
||||||
|
error(tostring(componentId).." has not been added to an entity")
|
||||||
|
end
|
||||||
|
|
||||||
|
if firstArchetypeMap == nil or map.size < firstArchetypeMap.size then
|
||||||
|
firstArchetypeMap = map
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
for id in firstArchetypeMap.sparse do
|
||||||
local archetype = archetypes[id]
|
local archetype = archetypes[id]
|
||||||
local columns = archetype.columns
|
local columns = archetype.columns
|
||||||
local archetypeRecords = archetype.records
|
local archetypeRecords = archetype.records
|
||||||
|
|
Loading…
Reference in a new issue