🐳 chore(工具): 更新 lua ecs
parent
674f932099
commit
8d44c1e8b4
@ -0,0 +1,234 @@
|
|||||||
|
local ecs = require "ecs"
|
||||||
|
|
||||||
|
local N = 1
|
||||||
|
|
||||||
|
local w = ecs.world()
|
||||||
|
print("memory:", w:memory())
|
||||||
|
|
||||||
|
w:register {
|
||||||
|
name = "vector",
|
||||||
|
"x:float",
|
||||||
|
"y:float",
|
||||||
|
}
|
||||||
|
|
||||||
|
w:register {
|
||||||
|
name = "mark"
|
||||||
|
}
|
||||||
|
|
||||||
|
w:register {
|
||||||
|
name = "id",
|
||||||
|
type = "int"
|
||||||
|
}
|
||||||
|
|
||||||
|
w:register {
|
||||||
|
name = "object",
|
||||||
|
type = "lua",
|
||||||
|
}
|
||||||
|
|
||||||
|
local t = {}
|
||||||
|
for i = 1, N do
|
||||||
|
w:new {
|
||||||
|
vector = {
|
||||||
|
x = 1,
|
||||||
|
y = 2,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
t[i] = { x = 1, y = 2 }
|
||||||
|
end
|
||||||
|
|
||||||
|
w:update()
|
||||||
|
|
||||||
|
local function swap_c()
|
||||||
|
for v in w:select "vector:update" do
|
||||||
|
local vec = v.vector
|
||||||
|
vec.x, vec.y = vec.y, vec.x
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function swap_lua()
|
||||||
|
for _, v in ipairs(t) do
|
||||||
|
v.x, v.y = v.y, v.x
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function timing(f)
|
||||||
|
local c = os.clock()
|
||||||
|
for i = 1, 100 do
|
||||||
|
f()
|
||||||
|
end
|
||||||
|
return os.clock() - c
|
||||||
|
end
|
||||||
|
|
||||||
|
print("memory:", w:memory())
|
||||||
|
|
||||||
|
print("CSWAP", timing(swap_c))
|
||||||
|
print("LUASWAP", timing(swap_lua))
|
||||||
|
|
||||||
|
w:new {
|
||||||
|
vector = {
|
||||||
|
x = 3,
|
||||||
|
y = 4,
|
||||||
|
},
|
||||||
|
id = 100,
|
||||||
|
}
|
||||||
|
|
||||||
|
table.insert(t, { x = 3, y = 4 })
|
||||||
|
|
||||||
|
w:new {
|
||||||
|
vector = {
|
||||||
|
x = 5,
|
||||||
|
y = 6,
|
||||||
|
},
|
||||||
|
mark = true,
|
||||||
|
}
|
||||||
|
|
||||||
|
table.insert(t, { x = 5, y = 6 })
|
||||||
|
|
||||||
|
w:update()
|
||||||
|
|
||||||
|
w:register {
|
||||||
|
name = "singleton",
|
||||||
|
type = "lua"
|
||||||
|
}
|
||||||
|
|
||||||
|
local context = w:context {
|
||||||
|
"vector",
|
||||||
|
"mark",
|
||||||
|
"id",
|
||||||
|
"singleton",
|
||||||
|
}
|
||||||
|
|
||||||
|
w:new { singleton = "Hello World" }
|
||||||
|
|
||||||
|
w:update()
|
||||||
|
|
||||||
|
local test = require "ecs.ctest"
|
||||||
|
|
||||||
|
print(test.get(context))
|
||||||
|
|
||||||
|
local function csum()
|
||||||
|
return test.sum(context)
|
||||||
|
end
|
||||||
|
print("csum = ", csum())
|
||||||
|
|
||||||
|
local function luasum()
|
||||||
|
local s = 0
|
||||||
|
for v in w:select "vector:in" do
|
||||||
|
s = s + v.vector.x + v.vector.y
|
||||||
|
end
|
||||||
|
return s
|
||||||
|
end
|
||||||
|
|
||||||
|
print("luasum = ", luasum())
|
||||||
|
|
||||||
|
local function luanativesum()
|
||||||
|
local s = 0
|
||||||
|
for _, v in ipairs(t) do
|
||||||
|
s = s + v.x + v.y
|
||||||
|
end
|
||||||
|
return s
|
||||||
|
end
|
||||||
|
|
||||||
|
print("lnative sum = ", luanativesum())
|
||||||
|
|
||||||
|
print("CSUM", timing(csum))
|
||||||
|
print("LUASUM", timing(luasum))
|
||||||
|
print("LNATIVESUM", timing(luanativesum))
|
||||||
|
|
||||||
|
print "vector:update"
|
||||||
|
for v in w:select "vector:update" do
|
||||||
|
local vec = v.vector
|
||||||
|
print(vec.x, vec.y)
|
||||||
|
vec.x, vec.y = vec.y , vec.x
|
||||||
|
end
|
||||||
|
|
||||||
|
print "vector:in id?out"
|
||||||
|
for v in w:select "vector:in id?temp" do
|
||||||
|
print(v.vector.x, v.vector.y, v.id)
|
||||||
|
if v.id then
|
||||||
|
v.id = 200
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
print "vector:in id:in"
|
||||||
|
|
||||||
|
for v in w:select "vector:in id:in" do
|
||||||
|
print(v.vector.x, v.vector.y, v.id)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
w:new { object = "Hello" , mark = true }
|
||||||
|
w:new { object = "World" , mark = true }
|
||||||
|
|
||||||
|
w:update()
|
||||||
|
|
||||||
|
print "mark:update object:in"
|
||||||
|
|
||||||
|
for v in w:select "mark:update object:in" do
|
||||||
|
print(v.object)
|
||||||
|
if v.object == "World" then
|
||||||
|
print "Disable mark where object == World"
|
||||||
|
v.mark = false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
print "mark:exist object:in"
|
||||||
|
|
||||||
|
for v in w:select "mark:exist object:in" do
|
||||||
|
print(v.object)
|
||||||
|
end
|
||||||
|
|
||||||
|
for v in w:select "object:exist mark:out" do
|
||||||
|
v.mark = false
|
||||||
|
end
|
||||||
|
|
||||||
|
for v in w:select "mark:exist" do
|
||||||
|
print("Remove")
|
||||||
|
w:remove(v)
|
||||||
|
end
|
||||||
|
|
||||||
|
for v in w:select "REMOVED:exist vector:in" do
|
||||||
|
print(v.vector.x, v.vector.y, "removed")
|
||||||
|
end
|
||||||
|
|
||||||
|
w:update() -- remove all
|
||||||
|
|
||||||
|
local n = 0
|
||||||
|
for v in w:select "mark:in" do
|
||||||
|
n = n + 1
|
||||||
|
end
|
||||||
|
print("Marked", n)
|
||||||
|
|
||||||
|
|
||||||
|
print "object:update"
|
||||||
|
|
||||||
|
for v in w:select "object:update" do
|
||||||
|
print(v.object)
|
||||||
|
v.object = v.object .. " world"
|
||||||
|
end
|
||||||
|
|
||||||
|
print "object:in"
|
||||||
|
|
||||||
|
for v in w:select "object:in" do
|
||||||
|
print(v.object)
|
||||||
|
end
|
||||||
|
|
||||||
|
w:register {
|
||||||
|
name = "sum",
|
||||||
|
type = "float",
|
||||||
|
}
|
||||||
|
|
||||||
|
for v in w:select "vector:in sum:temp" do
|
||||||
|
print(v.vector.x, "+", v.vector.y)
|
||||||
|
v.sum = v.vector.x + v.vector.y
|
||||||
|
end
|
||||||
|
|
||||||
|
for v in w:select "sum:in" do
|
||||||
|
print(v.sum)
|
||||||
|
end
|
||||||
|
|
||||||
|
w:clear "sum"
|
||||||
|
|
||||||
|
for v in w:select "sum:exist" do
|
||||||
|
error "Not empty"
|
||||||
|
end
|
||||||
@ -0,0 +1,58 @@
|
|||||||
|
-- test sort
|
||||||
|
|
||||||
|
local ecs = require "ecs"
|
||||||
|
|
||||||
|
local w = ecs.world()
|
||||||
|
|
||||||
|
w:register {
|
||||||
|
name = "data",
|
||||||
|
type = "float",
|
||||||
|
}
|
||||||
|
|
||||||
|
w:register {
|
||||||
|
name = "index",
|
||||||
|
type = "int",
|
||||||
|
}
|
||||||
|
|
||||||
|
local tmp = { 10,9,8,7,6,5,4,3,2,1 }
|
||||||
|
|
||||||
|
for i = 1, 10, 2 do
|
||||||
|
w:new { data = tmp[i], index = tmp[i] }
|
||||||
|
w:new { index = tmp[i+1] }
|
||||||
|
end
|
||||||
|
|
||||||
|
w:update()
|
||||||
|
|
||||||
|
for v in w:select "index data?in" do
|
||||||
|
print(v.data)
|
||||||
|
end
|
||||||
|
|
||||||
|
w:sort("sort", "index")
|
||||||
|
|
||||||
|
print "sorted"
|
||||||
|
|
||||||
|
for v in w:select "sort data:in index:in" do
|
||||||
|
print(v.data, v.index)
|
||||||
|
end
|
||||||
|
|
||||||
|
local iter = w:bsearch("sort", "index", 5)
|
||||||
|
w:sync("data:in", iter)
|
||||||
|
print("Found", iter.data)
|
||||||
|
|
||||||
|
w:register {
|
||||||
|
name = "sorted_index",
|
||||||
|
type = "int",
|
||||||
|
}
|
||||||
|
|
||||||
|
for i = 1, 10 do
|
||||||
|
w:new { data = i * 0.5 , sorted_index = i * 2 }
|
||||||
|
end
|
||||||
|
|
||||||
|
for v in w:select "sorted_index:in data?in" do
|
||||||
|
print(v.sorted_index, "=>", v.data)
|
||||||
|
end
|
||||||
|
|
||||||
|
local iter = w:bsearch("sorted_index", "sorted_index", 4)
|
||||||
|
w:sync("data:in", iter)
|
||||||
|
print("Found", iter.data)
|
||||||
|
--w:remove(iter)
|
||||||
@ -0,0 +1,126 @@
|
|||||||
|
-- test object
|
||||||
|
local ecs = require "ecs"
|
||||||
|
|
||||||
|
local w = ecs.world()
|
||||||
|
|
||||||
|
w:register {
|
||||||
|
name = "refobject",
|
||||||
|
type = "int",
|
||||||
|
ref = true,
|
||||||
|
}
|
||||||
|
|
||||||
|
local id1 = w:ref("refobject", { refobject = 42 })
|
||||||
|
local id2 = w:ref("refobject", { refobject = 0 })
|
||||||
|
local id3 = w:ref("refobject", { refobject = 100 })
|
||||||
|
print ("New", id1, id2, id3)
|
||||||
|
print(w:object("refobject", id1))
|
||||||
|
|
||||||
|
print("Release", id1)
|
||||||
|
|
||||||
|
w:release("refobject", id1)
|
||||||
|
|
||||||
|
for v in w:select "refobject:in" do
|
||||||
|
print(v.refobject)
|
||||||
|
end
|
||||||
|
|
||||||
|
local id4 = w:ref("refobject", { refobject = -42 })
|
||||||
|
print ("New", id4)
|
||||||
|
|
||||||
|
print ("Release", id2)
|
||||||
|
|
||||||
|
w:release("refobject", id2)
|
||||||
|
|
||||||
|
print ("Release", id3)
|
||||||
|
|
||||||
|
w:release("refobject", id3)
|
||||||
|
|
||||||
|
print "List refobject"
|
||||||
|
|
||||||
|
for v in w:select "refobject:in" do
|
||||||
|
print(v.refobject)
|
||||||
|
end
|
||||||
|
|
||||||
|
w:register {
|
||||||
|
name = "index",
|
||||||
|
type = "int",
|
||||||
|
}
|
||||||
|
|
||||||
|
w:new {
|
||||||
|
index = id4
|
||||||
|
}
|
||||||
|
|
||||||
|
for v in w:select "index:in" do
|
||||||
|
print(v.index)
|
||||||
|
end
|
||||||
|
|
||||||
|
print "Index refobject"
|
||||||
|
|
||||||
|
for v in w:select "refobject(index):in" do
|
||||||
|
print(v.refobject)
|
||||||
|
end
|
||||||
|
|
||||||
|
w:register {
|
||||||
|
name = "name",
|
||||||
|
type = "lua",
|
||||||
|
}
|
||||||
|
|
||||||
|
w:new {
|
||||||
|
name = "Hello"
|
||||||
|
}
|
||||||
|
|
||||||
|
for v in w:select "index:in" do
|
||||||
|
print(v.index)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
for v in w:select "name refobject(index):temp" do
|
||||||
|
v.refobject = 42
|
||||||
|
end
|
||||||
|
|
||||||
|
for v in w:select "refobject:in" do
|
||||||
|
print(v.refobject)
|
||||||
|
end
|
||||||
|
|
||||||
|
for v in w:select "refobject(index):update" do
|
||||||
|
v.refobject = v.refobject + 1
|
||||||
|
end
|
||||||
|
|
||||||
|
for v in w:select "refobject:in" do
|
||||||
|
print(v.refobject)
|
||||||
|
end
|
||||||
|
|
||||||
|
w:register {
|
||||||
|
name = "mark"
|
||||||
|
}
|
||||||
|
|
||||||
|
local ref = w:object_ref("refobject", id4)
|
||||||
|
ref.mark = true
|
||||||
|
w:sync("mark?out", ref)
|
||||||
|
|
||||||
|
|
||||||
|
w:ref ("refobject", {
|
||||||
|
refobject = 42,
|
||||||
|
mark = true
|
||||||
|
})
|
||||||
|
|
||||||
|
print "Marked refobject"
|
||||||
|
|
||||||
|
for v in w:select "mark refobject?in" do
|
||||||
|
print(v.refobject)
|
||||||
|
end
|
||||||
|
|
||||||
|
w:register {
|
||||||
|
name = "refobject2",
|
||||||
|
type = "int",
|
||||||
|
ref = true,
|
||||||
|
}
|
||||||
|
|
||||||
|
w:register {
|
||||||
|
name = "mark2"
|
||||||
|
}
|
||||||
|
|
||||||
|
w:ref ("refobject2", {
|
||||||
|
refobject2 = 42,
|
||||||
|
mark = true,
|
||||||
|
mark2 = false,
|
||||||
|
})
|
||||||
@ -0,0 +1,51 @@
|
|||||||
|
local ecs = require "ecs"
|
||||||
|
|
||||||
|
local w = ecs.world()
|
||||||
|
|
||||||
|
w:register {
|
||||||
|
name = "a",
|
||||||
|
type = "int",
|
||||||
|
}
|
||||||
|
|
||||||
|
w:register {
|
||||||
|
name = "b",
|
||||||
|
type = "float",
|
||||||
|
}
|
||||||
|
|
||||||
|
for i = 1, 20 do
|
||||||
|
w:new { a = i }
|
||||||
|
w:new { b = i }
|
||||||
|
end
|
||||||
|
|
||||||
|
for i = 20, 40 do
|
||||||
|
w:new { a = i , b = i }
|
||||||
|
end
|
||||||
|
|
||||||
|
w:update()
|
||||||
|
|
||||||
|
for v in w:select "a:in" do
|
||||||
|
if v.a % 2 == 1 then
|
||||||
|
w:remove(v)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
for v in w:select "b:in" do
|
||||||
|
if v.b < 10 then
|
||||||
|
w:remove(v)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
for v in w:select "REMOVED a?in b?in" do
|
||||||
|
print(v.a, v.b, "Removed")
|
||||||
|
end
|
||||||
|
|
||||||
|
w:update()
|
||||||
|
|
||||||
|
for v in w:select "a:in" do
|
||||||
|
print(v.a)
|
||||||
|
end
|
||||||
|
|
||||||
|
for v in w:select "b:in" do
|
||||||
|
print(v.b)
|
||||||
|
end
|
||||||
|
|
||||||
@ -0,0 +1,29 @@
|
|||||||
|
local ecs = require "ecs"
|
||||||
|
|
||||||
|
local w = ecs.world()
|
||||||
|
|
||||||
|
w:register {
|
||||||
|
name = "a",
|
||||||
|
type = "int",
|
||||||
|
}
|
||||||
|
|
||||||
|
w:register {
|
||||||
|
name = "temp",
|
||||||
|
type = "int",
|
||||||
|
}
|
||||||
|
|
||||||
|
for i = 1, 10 do
|
||||||
|
w:new { a = i }
|
||||||
|
end
|
||||||
|
|
||||||
|
for v in w:select "a:in" do
|
||||||
|
if v.a %2 == 0 then
|
||||||
|
v.a = -v.a
|
||||||
|
v.temp = 42
|
||||||
|
w:sync("a:out temp:temp", v)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
for v in w:select "a:in temp?in" do
|
||||||
|
print(v.a, v.temp)
|
||||||
|
end
|
||||||
@ -0,0 +1,35 @@
|
|||||||
|
local ecs = require "ecs"
|
||||||
|
|
||||||
|
local w = ecs.world()
|
||||||
|
|
||||||
|
w:register {
|
||||||
|
name = "t",
|
||||||
|
"a:bool",
|
||||||
|
"b:userdata",
|
||||||
|
}
|
||||||
|
|
||||||
|
w:new {
|
||||||
|
t = {
|
||||||
|
a = false,
|
||||||
|
b = ecs.NULL,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
local function print_v()
|
||||||
|
local v = w:singleton "t"
|
||||||
|
|
||||||
|
print(".a = ",v.a)
|
||||||
|
print(".b = ",v.b)
|
||||||
|
end
|
||||||
|
|
||||||
|
local ctx = w:context { "t" }
|
||||||
|
|
||||||
|
print("ctx = ", ctx)
|
||||||
|
|
||||||
|
local test = require "ecs.ctest"
|
||||||
|
|
||||||
|
print_v()
|
||||||
|
|
||||||
|
test.testuserdata(ctx)
|
||||||
|
|
||||||
|
print_v()
|
||||||
@ -0,0 +1,19 @@
|
|||||||
|
local ecs = require "ecs"
|
||||||
|
|
||||||
|
local w = ecs.world()
|
||||||
|
|
||||||
|
w:register {
|
||||||
|
name = "object",
|
||||||
|
type = "int",
|
||||||
|
ref = true,
|
||||||
|
}
|
||||||
|
|
||||||
|
for i = 1, 10 do
|
||||||
|
w:ref("object", { object = i * 10 })
|
||||||
|
end
|
||||||
|
|
||||||
|
w:order("order", "object", { 9,7,5,3,1 })
|
||||||
|
|
||||||
|
for v in w:select "order object:in" do
|
||||||
|
print(v.object)
|
||||||
|
end
|
||||||
@ -0,0 +1,33 @@
|
|||||||
|
local ecs = require "ecs"
|
||||||
|
|
||||||
|
local w = ecs.world()
|
||||||
|
|
||||||
|
w:register {
|
||||||
|
name = "v",
|
||||||
|
type = "int",
|
||||||
|
}
|
||||||
|
|
||||||
|
w:register {
|
||||||
|
name = "marked"
|
||||||
|
}
|
||||||
|
|
||||||
|
for i = 1, 10 do
|
||||||
|
w:new {
|
||||||
|
v = i,
|
||||||
|
marked = i % 2 == 1,
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
for v in w:select "v:in marked?in" do
|
||||||
|
print(v.v, v.marked)
|
||||||
|
end
|
||||||
|
|
||||||
|
print "Marked"
|
||||||
|
for v in w:select "v:in marked" do
|
||||||
|
print(v.v)
|
||||||
|
end
|
||||||
|
|
||||||
|
print "Not Marked"
|
||||||
|
for v in w:select "v:in marked:absent" do
|
||||||
|
print(v.v)
|
||||||
|
end
|
||||||
Loading…
Reference in New Issue