Add wildcards

This commit is contained in:
Ukendio 2024-08-03 06:21:56 +02:00
parent e5634b10b2
commit ffb0fae952
2 changed files with 38 additions and 2 deletions

View file

@ -42,8 +42,7 @@ local entity = world:entity()
const entity = world.entity(); const entity = world.entity();
``` ```
:: :::
:
### component() ### component()
```luau ```luau

View 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