在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):cloudwu/luaecs开源软件地址(OpenSource Url):https://github.com/cloudwu/luaecs开源编程语言(OpenSource Language):C 74.5%开源软件介绍(OpenSource Introduction):Lua ECSA simple Entity-Componet library for Lua. -- Create a world
local w = ecs.world()
-- Register a new type "vector( x:float, y:float )" into world.
-- It's a C component (The data is in C memory).
w:register {
name = "vector",
"x:float",
"y:float",
}
-- Register a new type "name" into world.
-- It's a Lua component (The value can be any lua object)
w:register {
name = "name",
type = "lua",
}
-- Create a new entity with components vector and name.
local eid = w:new {
vector = { 1.0, 2.0 },
name = "point",
}
-- Iterate every entity with component vector and name
for v in w:select "vector:in name:in" do
-- v is the iterator, we can read compionent from it.
-- DO NOT store v outside of the iteration
print(v.name) -- output : point
print(v.vector.x, v.vector.y) -- output: 1.0, 2.0
end Component Types
The component is stored in C side, it's compat and can be accessed from C side easily. The supported buildin types are:
w:register {
name = "vector",
"x:float",
"y:float",
} The equivalent C struct is struct vector {
float x;
float y;
}; A single buildin C type can be register as w:register {
name = "visible",
type = "bool",
}
The component is stored in Lua side , and can be any lua types, such as string, table, userdata, etc. w:register {
name = "name",
type = "lua",
}
The tag is a special component , it can be read as a boolean type, and the value must be true. It used for selection. w:register {
name = "visible"
}
Each entity has a build-in readonly component Select PatternUse -- print all the entities' eid
for v in w:select "eid:in" do
print(v.eid)
end The pattern is a space-separated combination of
-- clear temp component from all entities
w:clear "temp"
-- Iterate the entity with visible/value/output components and without readonly component
-- Create temp component for each.
for v in w:select "visible readonly:absent value:in output:out temp:new" do
v.output = v.value + 1
v.temp = v.value
end NOTICE: If you use action Create Entity-- create an empty entity
local eid = w:new()
-- Import component "name" into entity (eid)
w:import(eid, { name = "foobar" })
-- Or you can use
local eid = w:new { name = "foobar" } You can also create an entity from a template : -- Create a template first
local t = w:template {
name = "foobar"
}
-- instance the template into an entity, and add visible tag.
-- The additional components ( { visible = true } ) is optional.
local eid = w:template_instance(w:new(), t, { visible = true }) You can offer
NOTICE: C component can be initialize by a lua string, you can use Remove Entity-- Remove an entity with eid
w:remove(eid)
-- Or remove an entity with an iterator
for v in w:select "value:in" do
if v.value == 42 then
w:remove(v)
end
end
for v in w:select "REMOVED value:in do
assert(v.value == 42)
end
-- delete all the entities removed
w:update() The entities removed are not going to disppear immediately, they are tagged with GroupYou can add an entity into a group after creation. Each entity can belongs one or more groups (or no group). -- Add entity (eid) into a group with groupid (32bit integer)
w:group_add(groupid, eid) You can tags entities in groups with PersistanceOnly C components can be persistance. -- Save
-- Create a writer to file "saves.bin"
local writer = ecs.writer "saves.bin"
writer:write(w, w:component_id "eid") -- write component eid
writer:write(w, w:component_id "value") -- write component value
writer:write(w, w:component_id "tag") -- write component tag
local meta = writer:close()
local function print_section(s)
print("offset =", s.offset)
print("stride =", s.stride)
print("n = ", s.n)
end
print_section(meta[1]) -- meta information of eid
print_section(meta[2]) -- meta information of value
print_section(meta[3]) -- meta information of tag -- Load
local reader = ecs.reader "saves.bin"
local maxid = w:read_component(reader, "eid", meta[1].offset, meta[1].stride, meta[1].n)
local value_n = w:read_component(reader, "value", meta[2].offset, meta[2].stride, meta[2].n)
local tag_n = w:read_component(reader, "tag", meta[3].offset, meta[3].stride, meta[3].n)
reader:close() You can also use Other APIs
Access Components from C side
struct component *v;
int i;
// iterate all the components with COMPONENT_ID. COMPONENT_ID is the index of context (base 0) or component id from lua MAKE_COMPONENT_ID(cid)
for (i = 0; (v = (struct component *)entity_iter(ctx, COMPONENT_ID, i)); i++) {
// Read the component2 associate with component (the same entity)
struct component2 * c2 = (struct component2 *)entity_sibling(ctx, COMPONENT_ID, i, COMPONENT_ID2);
}
The same with entity_sibling, but returns an id (base 1). 0 : not exist.
Create an entity with one component
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论