mirror of
https://github.com/Ukendio/jecs.git
synced 2025-04-24 17:10:03 +00:00
Add wildcards
This commit is contained in:
parent
e5634b10b2
commit
ffb0fae952
2 changed files with 38 additions and 2 deletions
|
@ -42,8 +42,7 @@ local entity = world:entity()
|
|||
const entity = world.entity();
|
||||
```
|
||||
|
||||
::
|
||||
:
|
||||
:::
|
||||
|
||||
### component()
|
||||
```luau
|
||||
|
|
37
examples/luau/queries/wildcards.luau
Normal file
37
examples/luau/queries/wildcards.luau
Normal file
|
@ -0,0 +1,37 @@
|
|||
local jecs = require("@jecs")
|
||||
local pair = jecs.pair
|
||||
local world = jecs.World.new()
|
||||
local Name = world:component()
|
||||
|
||||
local function named(ctr, name)
|
||||
local e = ctr(world)
|
||||
world:set(e, Name, name)
|
||||
return e
|
||||
end
|
||||
local function name(e)
|
||||
return world:get(e, Name)
|
||||
end
|
||||
|
||||
local Eats = world:component()
|
||||
local Apples = named(world.entity, "Apples")
|
||||
local Oranges = named(world.entity, "Oranges")
|
||||
|
||||
local bob = named(world.entity, "Bob")
|
||||
world:set(bob, pair(Eats, Apples), 10)
|
||||
|
||||
local alice = named(world.entity, "Alice")
|
||||
world:set(alice, pair(Eats, Oranges), 5)
|
||||
|
||||
-- Aliasing the wildcard to symbols improves readability and ease of writing
|
||||
local __ = jecs.Wildcard
|
||||
|
||||
-- Create a query that matches edible components
|
||||
for entity, amount in world:query(pair(Eats, __)) do
|
||||
-- Iterate the query
|
||||
local food = world:target(entity, Eats)
|
||||
print(`{name(entity)} eats {amount} {name(food)}`)
|
||||
end
|
||||
|
||||
-- Output:
|
||||
-- Alice eats 5 Oranges
|
||||
-- Bob eats 10 Apples
|
Loading…
Reference in a new issue