mirror of
https://github.com/Ukendio/jecs.git
synced 2025-09-23 08:39:16 +00:00
Fix line endings (#280)
Some checks are pending
Some checks are pending
This commit is contained in:
parent
e2ab3be3e5
commit
9514fb758a
2 changed files with 104 additions and 104 deletions
|
@ -1,60 +1,60 @@
|
||||||
-- Using world:target is the recommended way to grab the target in a wildcard
|
-- Using world:target is the recommended way to grab the target in a wildcard
|
||||||
-- query. However the random access can add up in very hot paths. Accessing its
|
-- query. However the random access can add up in very hot paths. Accessing its
|
||||||
-- column can drastically improve performance, especially when there are
|
-- column can drastically improve performance, especially when there are
|
||||||
-- multiple adjacent pairs.
|
-- multiple adjacent pairs.
|
||||||
|
|
||||||
local jecs = require("@jecs")
|
local jecs = require("@jecs")
|
||||||
local pair = jecs.pair
|
local pair = jecs.pair
|
||||||
local __ = jecs.Wildcard
|
local __ = jecs.Wildcard
|
||||||
|
|
||||||
local world = jecs.world()
|
local world = jecs.world()
|
||||||
|
|
||||||
local Likes = world:entity()
|
local Likes = world:entity()
|
||||||
local function name(e, name: string): string
|
local function name(e, name: string): string
|
||||||
if name then
|
if name then
|
||||||
world:set(e, jecs.Name, name)
|
world:set(e, jecs.Name, name)
|
||||||
return name
|
return name
|
||||||
end
|
end
|
||||||
return assert(world:get(e, jecs.Name))
|
return assert(world:get(e, jecs.Name))
|
||||||
end
|
end
|
||||||
|
|
||||||
local e1 = world:entity()
|
local e1 = world:entity()
|
||||||
name(e1, "e1")
|
name(e1, "e1")
|
||||||
local e2 = world:entity()
|
local e2 = world:entity()
|
||||||
name(e2, "e2")
|
name(e2, "e2")
|
||||||
local e3 = world:entity()
|
local e3 = world:entity()
|
||||||
name(e3, "e3")
|
name(e3, "e3")
|
||||||
|
|
||||||
world:add(e1, pair(Likes, e2))
|
world:add(e1, pair(Likes, e2))
|
||||||
world:add(e1, pair(Likes, e3))
|
world:add(e1, pair(Likes, e3))
|
||||||
|
|
||||||
local likes = jecs.component_record(world, pair(Likes, __))
|
local likes = jecs.component_record(world, pair(Likes, __))
|
||||||
assert(likes)
|
assert(likes)
|
||||||
|
|
||||||
local likes_cr = likes.records
|
local likes_cr = likes.records
|
||||||
local likes_counts = likes.counts
|
local likes_counts = likes.counts
|
||||||
|
|
||||||
local archetypes = world:query(pair(Likes, __)):archetypes()
|
local archetypes = world:query(pair(Likes, __)):archetypes()
|
||||||
|
|
||||||
for _, archetype in archetypes do
|
for _, archetype in archetypes do
|
||||||
local types = archetype.types
|
local types = archetype.types
|
||||||
|
|
||||||
-- Get the starting index which is what the (R, *) alias is at
|
-- Get the starting index which is what the (R, *) alias is at
|
||||||
local wc = likes_cr[archetype.id]
|
local wc = likes_cr[archetype.id]
|
||||||
local count = likes_counts[archetype.id]
|
local count = likes_counts[archetype.id]
|
||||||
|
|
||||||
local entities = archetype.entities
|
local entities = archetype.entities
|
||||||
for i = #entities, 1, -1 do
|
for i = #entities, 1, -1 do
|
||||||
-- It is generally a good idea to iterate backwards on arrays if you
|
-- It is generally a good idea to iterate backwards on arrays if you
|
||||||
-- need to delete the iterated entity to prevent iterator invalidation
|
-- need to delete the iterated entity to prevent iterator invalidation
|
||||||
local entity = entities[i]
|
local entity = entities[i]
|
||||||
for cr = wc, wc + count - 1 do
|
for cr = wc, wc + count - 1 do
|
||||||
local person = jecs.pair_second(world, types[cr])
|
local person = jecs.pair_second(world, types[cr])
|
||||||
print(`entity ${entity} likes ${person}`)
|
print(`entity ${entity} likes ${person}`)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Output:
|
-- Output:
|
||||||
-- entity $273 likes $275
|
-- entity $273 likes $275
|
||||||
-- entity $273 likes $274
|
-- entity $273 likes $274
|
||||||
|
|
|
@ -1,44 +1,44 @@
|
||||||
-- To get the most out of performance, you can lift the inner loop of queries to
|
-- To get the most out of performance, you can lift the inner loop of queries to
|
||||||
-- the system in which you can do archetype-specific optimizations like finding
|
-- the system in which you can do archetype-specific optimizations like finding
|
||||||
-- the parent once per archetype rather than per entity.
|
-- the parent once per archetype rather than per entity.
|
||||||
|
|
||||||
local jecs = require("@jecs")
|
local jecs = require("@jecs")
|
||||||
local pair = jecs.pair
|
local pair = jecs.pair
|
||||||
local ChildOf = jecs.ChildOf
|
local ChildOf = jecs.ChildOf
|
||||||
local __ = jecs.Wildcard
|
local __ = jecs.Wildcard
|
||||||
|
|
||||||
local world = jecs.world()
|
local world = jecs.world()
|
||||||
|
|
||||||
local Position = world:component() :: jecs.Id<vector>
|
local Position = world:component() :: jecs.Id<vector>
|
||||||
local Visible = world:entity()
|
local Visible = world:entity()
|
||||||
|
|
||||||
local parent = world:entity()
|
local parent = world:entity()
|
||||||
world:set(parent, Position, vector.zero)
|
world:set(parent, Position, vector.zero)
|
||||||
world:add(parent, Visible)
|
world:add(parent, Visible)
|
||||||
|
|
||||||
local child = world:entity()
|
local child = world:entity()
|
||||||
world:set(child, Position, vector.one)
|
world:set(child, Position, vector.one)
|
||||||
world:add(child, pair(ChildOf, parent))
|
world:add(child, pair(ChildOf, parent))
|
||||||
|
|
||||||
local parents = jecs.component_record(world, pair(ChildOf, __))
|
local parents = jecs.component_record(world, pair(ChildOf, __))
|
||||||
assert(parents)
|
assert(parents)
|
||||||
|
|
||||||
local parent_cr = parents.records
|
local parent_cr = parents.records
|
||||||
|
|
||||||
local archetypes = world:query(Position, pair(ChildOf, __)):archetypes()
|
local archetypes = world:query(Position, pair(ChildOf, __)):archetypes()
|
||||||
|
|
||||||
for _, archetype in archetypes do
|
for _, archetype in archetypes do
|
||||||
local types = archetype.types
|
local types = archetype.types
|
||||||
local p = jecs.pair_second(world, types[parent_cr[archetype.id]])
|
local p = jecs.pair_second(world, types[parent_cr[archetype.id]])
|
||||||
if world:has(p, Visible) then
|
if world:has(p, Visible) then
|
||||||
local columns = archetype.columns_map
|
local columns = archetype.columns_map
|
||||||
local positions = columns[Position]
|
local positions = columns[Position]
|
||||||
for row, entity in archetype.entities do
|
for row, entity in archetype.entities do
|
||||||
local pos = positions[row]
|
local pos = positions[row]
|
||||||
print(`Child ${entity} of ${p} is visible at {pos}`)
|
print(`Child ${entity} of ${p} is visible at {pos}`)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Output:
|
-- Output:
|
||||||
-- Child $274 of $273 is visibile at 1,1,1
|
-- Child $274 of $273 is visibile at 1,1,1
|
||||||
|
|
Loading…
Reference in a new issue