Compare commits

..

2 commits

Author SHA1 Message Date
Ukendio
4153a7cdfe Monitors and observers need to able to accept pair terms
Some checks failed
analysis / Run Luau Analyze (push) Has been cancelled
deploy-docs / build (push) Has been cancelled
publish-npm / publish (push) Has been cancelled
unit-testing / Run Luau Tests (push) Has been cancelled
deploy-docs / Deploy (push) Has been cancelled
2025-07-29 21:08:23 +02:00
Ukendio
4230a0a797 Version 2025-07-29 21:07:59 +02:00
7 changed files with 95 additions and 8 deletions

View file

@ -69,6 +69,9 @@ local function observers_new<T...>(
end end
for _, term in terms do for _, term in terms do
if jecs.IS_PAIR(term) then
term = jecs.ECS_PAIR_FIRST(term)
end
world:added(term, emplaced) world:added(term, emplaced)
world:changed(term, emplaced) world:changed(term, emplaced)
end end
@ -151,7 +154,7 @@ local function monitors_new<T...>(
i += 1 i += 1
entities[i] = entity entities[i] = entity
if callback ~= nil then if callback ~= nil then
callback(entity, id, value) callback(entity, jecs.OnAdd)
end end
end end
end end
@ -164,6 +167,9 @@ local function monitors_new<T...>(
end end
for _, term in terms do for _, term in terms do
if jecs.IS_PAIR(term) then
term = jecs.ECS_PAIR_FIRST(term)
end
world:added(term, emplaced) world:added(term, emplaced)
world:removed(term, removed) world:removed(term, removed)
end end

View file

@ -1,12 +1,11 @@
local ReplicatedStorage = game:GetService("ReplicatedStorage") local ReplicatedStorage = game:GetService("ReplicatedStorage")
local jecs = require(ReplicatedStorage.ecs) local jecs = require(ReplicatedStorage.ecs)
local schedule = require(ReplicatedStorage.schedule) local schedule = require(ReplicatedStorage.schedule)
local observers_add = require(ReplicatedStorage.observers_add)
local SYSTEM = schedule.SYSTEM local SYSTEM = schedule.SYSTEM
local RUN = schedule.RUN local RUN = schedule.RUN
require(ReplicatedStorage.components) require(ReplicatedStorage.components)
local world = observers_add(jecs.world()) local world = jecs.world()
local systems = ReplicatedStorage.systems local systems = ReplicatedStorage.systems
SYSTEM(world, systems.receive_replication) SYSTEM(world, systems.receive_replication)

View file

@ -1,7 +1,6 @@
local jecs = require(game:GetService("ReplicatedStorage").ecs) local jecs = require(game:GetService("ReplicatedStorage").ecs)
local observers_add = require("../ReplicatedStorage/observers_add")
export type World = typeof(observers_add(jecs.world())) export type World = typeof(jecs.world())
export type Entity = jecs.Entity export type Entity = jecs.Entity
export type Id<T> = jecs.Id<T> export type Id<T> = jecs.Id<T>
export type Snapshot = { export type Snapshot = {

View file

@ -1,6 +1,6 @@
{ {
"name": "@rbxts/jecs", "name": "@rbxts/jecs",
"version": "0.9.0-rc.6", "version": "0.9.0-rc.7",
"description": "Stupidly fast Entity Component System", "description": "Stupidly fast Entity Component System",
"main": "jecs.luau", "main": "jecs.luau",
"repository": { "repository": {

View file

@ -2,11 +2,28 @@ local jecs = require("@jecs")
local testkit = require("@testkit") local testkit = require("@testkit")
local test = testkit.test() local test = testkit.test()
local CASE, TEST, FINISH, CHECK = test.CASE, test.TEST, test.FINISH, test.CHECK local CASE, TEST, FINISH, CHECK = test.CASE, test.TEST, test.FINISH, test.CHECK
local FOCUS = test.FOCUS
local ob = require("@addons/ob") local ob = require("@addons/ob")
TEST("addons/observers", function() TEST("addons/observers", function()
local world = jecs.world()
local world = jecs.world()
do CASE "monitors should accept pairs"
local A = world:component()
local B = world:component()
local c = 1
ob.monitor(world:query(jecs.pair(A, B)), function (_, event)
c += 1
end)
local child = world:entity()
world:add(child, jecs.pair(A, B))
CHECK(c == 2)
world:remove(child, jecs.pair(A, B))
CHECK(c == 3)
end
do CASE "Ensure ordering between signals and observers" do CASE "Ensure ordering between signals and observers"
local A = world:component() local A = world:component()
local B = world:component() local B = world:component()

View file

@ -1,6 +1,6 @@
[package] [package]
name = "ukendio/jecs" name = "ukendio/jecs"
version = "0.9.0-rc.6" version = "0.9.0-rc.7"
registry = "https://github.com/UpliftGames/wally-index" registry = "https://github.com/UpliftGames/wally-index"
realm = "shared" realm = "shared"
license = "MIT" license = "MIT"

66
werewitch.luau Executable file
View file

@ -0,0 +1,66 @@
local jecs = require("@jecs")
local world = jecs.world()
local pair = jecs.pair
local IsA = world:entity()
local traits = {
IsA = IsA
}
world:set(IsA, jecs.OnAdd, function(component, id)
local second = jecs.pair_second(world, id)
assert(second ~= component, "circular")
local is_tag = jecs.is_tag(world, second)
world:added(component, function(entity, _, value)
if is_tag then
world:add(entity, second)
else
world:set(entity, second, value)
end
end)
world:removed(component, function(entity)
world:remove(entity, second)
end)
if not is_tag then
world:changed(component, function(entity, _, value)
world:set(entity, second, value)
end)
end
local r = jecs.record(world, second) :: jecs.Record
local archetype = r.archetype
if not archetype then
return
end
local types = archetype.types
for _, id in types do
if jecs.is_tag(world, id) then
world:add(component, id)
else
local metadata = world:get(second, id)
if not world:has(component, id) then
world:set(component, id, metadata)
end
end
end
-- jecs.bulk_insert(world, component, ids, values)
end)
local Witch = world:entity()
local Werewolf = world:entity()
local WereWitch = world:entity()
world:add(WereWitch, pair(IsA, Witch))
world:add(WereWitch, pair(IsA, Werewolf))
local e = world:entity()
world:add(e, WereWitch)
print(world:has(e, pair(IsA, Witch))) -- false
print(world:has(e, Witch)) -- true