Skip to content

Commit

Permalink
Use a dictionary for loop parameters (revert later)
Browse files Browse the repository at this point in the history
  • Loading branch information
jackTabsCode committed Sep 11, 2024
1 parent e67b83e commit 4440052
Show file tree
Hide file tree
Showing 16 changed files with 68 additions and 66 deletions.
12 changes: 6 additions & 6 deletions example/src/client/receiveReplication.luau
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Components = require(ReplicatedStorage.Shared.components)
local RemoteEvent = ReplicatedStorage:WaitForChild("MatterRemote")

local function setupReplication(world, state)
local function setupReplication(cx)
local function debugPrint(...)
if state.debugEnabled then
if cx.state.debugEnabled then
print("Replication>", ...)
end
end
Expand All @@ -16,7 +16,7 @@ local function setupReplication(world, state)
local clientEntityId = entityIdMap[serverEntityId]

if clientEntityId and next(componentMap) == nil then
world:despawn(clientEntityId)
cx.world:despawn(clientEntityId)
entityIdMap[serverEntityId] = nil
debugPrint(string.format("Despawn %ds%d", clientEntityId, serverEntityId))
continue
Expand All @@ -39,7 +39,7 @@ local function setupReplication(world, state)
end

if clientEntityId == nil then
clientEntityId = world:spawn(unpack(componentsToInsert))
clientEntityId = cx.world:spawn(unpack(componentsToInsert))

entityIdMap[serverEntityId] = clientEntityId

Expand All @@ -48,11 +48,11 @@ local function setupReplication(world, state)
)
else
if #componentsToInsert > 0 then
world:insert(clientEntityId, unpack(componentsToInsert))
cx.world:insert(clientEntityId, unpack(componentsToInsert))
end

if #componentsToRemove > 0 then
world:remove(clientEntityId, unpack(componentsToRemove))
cx.world:remove(clientEntityId, unpack(componentsToRemove))
end

debugPrint(
Expand Down
4 changes: 2 additions & 2 deletions example/src/client/systems/roombasHurt.luau
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Components = require(ReplicatedStorage.Shared.components)
local Matter = require(ReplicatedStorage.Lib.Matter)

local function roombasHurt(world)
for _, _, model in world:query(Components.Roomba, Components.Model) do
local function roombasHurt(cx)
for _, _, model in cx.world:query(Components.Roomba, Components.Model) do
for _, part in Matter.useEvent(model.model.PrimaryPart, "Touched") do
local touchedModel = part:FindFirstAncestorWhichIsA("Model")
if not touchedModel then
Expand Down
10 changes: 5 additions & 5 deletions example/src/client/systems/spinSpinners.luau
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Components = require(ReplicatedStorage.Shared.components)

local function spinSpinners(world, _, ui)
if ui.checkbox("Disable Spinning"):checked() then
local function spinSpinners(cx)
if cx.widgets.checkbox("Disable Spinning"):checked() then
return
end

local transparency = ui.slider(1)
local transparency = cx.widgets.slider(1)

local randomize = ui.button("Randomize colors!"):clicked()
local randomize = cx.widgets.button("Randomize colors!"):clicked()

for _, model in world:query(Components.Model, Components.Spinner) do
for _, model in cx.world:query(Components.Model, Components.Spinner) do
model.model.PrimaryPart.CFrame = model.model.PrimaryPart.CFrame * CFrame.Angles(0, math.rad(5), 0)
model.model.PrimaryPart.Transparency = transparency

Expand Down
4 changes: 2 additions & 2 deletions example/src/game.client.luau
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ local ReplicatedStorage = game:GetService("ReplicatedStorage")
local start = require(ReplicatedStorage.Shared.start)
local receiveReplication = require(ReplicatedStorage.Client.receiveReplication)

local world, state = start({
local cx = start({
ReplicatedStorage.Shared.systems,
ReplicatedStorage.Client.systems,
})

receiveReplication(world, state)
receiveReplication(cx)
4 changes: 2 additions & 2 deletions example/src/server/init.server.luau
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ local ReplicatedStorage = game:GetService("ReplicatedStorage")
local start = require(ReplicatedStorage.Shared.start)
local setupTags = require(ReplicatedStorage.Shared.setupTags)

local world = start({
local cx = start({
script.systems,
ReplicatedStorage.Shared.systems,
})

setupTags(world)
setupTags(cx)
10 changes: 5 additions & 5 deletions example/src/server/systems/mothershipsSpawnRoombas.luau
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Components = require(ReplicatedStorage.Shared.components)
local Matter = require(ReplicatedStorage.Lib.Matter)

local function mothershipsSpawnRoombas(world)
local function mothershipsSpawnRoombas(cx)
for id, model, lasering, transform in
world:query(Components.Model, Components.Lasering, Components.Transform, Components.Mothership)
cx.world:query(Components.Model, Components.Lasering, Components.Transform, Components.Mothership)
do
model.model.Beam.Transparency = 1 - lasering.remainingTime

Expand All @@ -15,7 +15,7 @@ local function mothershipsSpawnRoombas(world)
if not lasering.spawned then
local spawnPosition = Vector3.new(transform.cframe.p.X, 11, transform.cframe.p.Z)

world:spawn(
cx.world:spawn(
Components.Roomba(),
Components.Charge({
charge = 100,
Expand All @@ -29,9 +29,9 @@ local function mothershipsSpawnRoombas(world)
end

if lasering.remainingTime <= 0 then
world:remove(id, Components.Lasering)
cx.world:remove(id, Components.Lasering)
else
world:insert(id, lasering)
cx.world:insert(id, lasering)
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions example/src/server/systems/playersAreTargets.luau
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Components = require(ReplicatedStorage.Shared.components)
local Matter = require(ReplicatedStorage.Lib.Matter)

local function playersAreTargets(world)
local function playersAreTargets(cx)
for _, player in ipairs(Players:GetPlayers()) do
for _, character in Matter.useEvent(player, "CharacterAdded") do
world:spawn(
cx.world:spawn(
Components.Target(),
Components.Model({
model = character,
Expand All @@ -16,8 +16,8 @@ local function playersAreTargets(world)
end

-- players can die
for id in world:query(Components.Target):without(Components.Model) do
world:despawn(id)
for id in cx.world:query(Components.Target):without(Components.Model) do
cx.world:despawn(id)
end
end

Expand Down
10 changes: 5 additions & 5 deletions example/src/server/systems/removeMissingModels.luau
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Components = require(ReplicatedStorage.Shared.components)
local Matter = require(ReplicatedStorage.Lib.Matter)

local function removeMissingModels(world)
for id, model in world:query(Components.Model) do
local function removeMissingModels(cx)
for id, model in cx.world:query(Components.Model) do
for _ in Matter.useEvent(model.model, "AncestryChanged") do
if model.model:IsDescendantOf(game) == false then
world:remove(id, Components.Model)
cx.world:remove(id, Components.Model)
break
end
end
if not model.model.PrimaryPart then
world:remove(id, Components.Model)
cx.world:remove(id, Components.Model)
end
end

for _id, modelRecord in world:queryChanged(Components.Model) do
for _id, modelRecord in cx.world:queryChanged(Components.Model) do
if modelRecord.new == nil then
if modelRecord.old and modelRecord.old.model then
modelRecord.old.model:Destroy()
Expand Down
8 changes: 4 additions & 4 deletions example/src/server/systems/replication.luau
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ for _, name in REPLICATED_COMPONENTS do
replicatedComponents[Components[name]] = true
end

local function replication(world)
local function replication(cx)
for _, player in useEvent(Players, "PlayerAdded") do
local payload = {}

for entityId, entityData in world do
for entityId, entityData in cx.world do
local entityPayload = {}
payload[tostring(entityId)] = entityPayload

Expand All @@ -44,15 +44,15 @@ local function replication(world)
local changes = {}

for component in replicatedComponents do
for entityId, record in world:queryChanged(component) do
for entityId, record in cx.world:queryChanged(component) do
local key = tostring(entityId)
local name = tostring(component)

if changes[key] == nil then
changes[key] = {}
end

if world:contains(entityId) then
if cx.world:contains(entityId) then
changes[key][name] = { data = record.new }
end
end
Expand Down
6 changes: 3 additions & 3 deletions example/src/server/systems/roombasMove.luau
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Components = require(ReplicatedStorage.Shared.components)

local function roombasMove(world)
local function roombasMove(cx)
local targets = {}
for _, model in world:query(Components.Model, Components.Target) do
for _, model in cx.world:query(Components.Model, Components.Target) do
table.insert(targets, model.model.PrimaryPart.CFrame.p)
end

for _, _, charge, model in world:query(Components.Roomba, Components.Charge, Components.Model) do
for _, _, charge, model in cx.world:query(Components.Roomba, Components.Charge, Components.Model) do
if charge.charge <= 0 then
continue
end
Expand Down
16 changes: 8 additions & 8 deletions example/src/server/systems/spawnMotherships.luau
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Components = require(ReplicatedStorage.Shared.components)
local Matter = require(ReplicatedStorage.Lib.Matter)

local function spawnMotherships(world)
local function spawnMotherships(cx)
if Matter.useThrottle(10) then
local spawnPosition = Vector3.new(500, 500, 500)
* Vector3.new(math.random(1, 2) == 1 and 1 or -1, 1, math.random(1, 2) == 1 and 1 or -1)
Expand All @@ -12,7 +12,7 @@ local function spawnMotherships(world)

local goalPosition = Vector3.new(math.random(-100, 100), 100, math.random(-100, 100))

world:spawn(
cx.world:spawn(
Components.Mothership({
goal = goalPosition,
nextGoal = despawnPosition,
Expand All @@ -23,12 +23,12 @@ local function spawnMotherships(world)
)
end

for id in world:query(Components.Transform, Components.Mothership):without(Components.Model) do
for id in cx.world:query(Components.Transform, Components.Mothership):without(Components.Model) do
local model = ReplicatedStorage.Assets.Mothership:Clone()
model.Parent = workspace
model.PrimaryPart:SetNetworkOwner(nil)

world:insert(
cx.world:insert(
id,
Components.Model({
model = model,
Expand All @@ -37,13 +37,13 @@ local function spawnMotherships(world)
end

for id, mothership, transform in
world:query(Components.Mothership, Components.Transform):without(Components.Lasering)
cx.world:query(Components.Mothership, Components.Transform):without(Components.Lasering)
do
if (transform.cframe.p - mothership.goal).magnitude < 10 then
if mothership.lasered then
world:despawn(id)
cx.world:despawn(id)
else
world:insert(
cx.world:insert(
id,
mothership:patch({
goal = mothership.nextGoal,
Expand All @@ -57,7 +57,7 @@ local function spawnMotherships(world)
end
end

for _, mothership, model in world:query(Components.Mothership, Components.Model):without(Components.Lasering) do
for _, mothership, model in cx.world:query(Components.Mothership, Components.Model):without(Components.Lasering) do
model.model.Roomba.AlignPosition.Position = mothership.goal
end
end
Expand Down
6 changes: 3 additions & 3 deletions example/src/server/systems/spawnRoombas.luau
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Components = require(ReplicatedStorage.Shared.components)

local function spawnRoombas(world)
for id, _ in world:query(Components.Transform, Components.Roomba):without(Components.Model) do
local function spawnRoombas(cx)
for id, _ in cx.world:query(Components.Transform, Components.Roomba):without(Components.Model) do
local model = ReplicatedStorage.Assets.KillerRoomba:Clone()
model.Parent = workspace
model.PrimaryPart:SetNetworkOwner(nil)

world:insert(
cx.world:insert(
id,
Components.Model({
model = model,
Expand Down
20 changes: 10 additions & 10 deletions example/src/server/systems/updateTransforms.luau
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Components = require(ReplicatedStorage.Shared.components)
local removeMissingModels = require(script.Parent.removeMissingModels)

local function updateTransforms(world)
local function updateTransforms(cx)
-- Handle Transform added/changed to existing entity with Model
for id, transformRecord in world:queryChanged(Components.Transform) do
if not world:contains(id) then
for id, transformRecord in cx.world:queryChanged(Components.Transform) do
if not cx.world:contains(id) then
continue
end

local model = world:get(id, Components.Model)
local model = cx.world:get(id, Components.Model)

if not model then
continue
Expand All @@ -21,12 +21,12 @@ local function updateTransforms(world)
end

-- Handle Model added/changed on existing entity with Transform
for id, modelRecord in world:queryChanged(Components.Model) do
if not world:contains(id) then
for id, modelRecord in cx.world:queryChanged(Components.Model) do
if not cx.world:contains(id) then
continue
end

local transform = world:get(id, Components.Transform)
local transform = cx.world:get(id, Components.Transform)

if not transform then
continue
Expand All @@ -38,7 +38,7 @@ local function updateTransforms(world)
end

-- Update Transform on unanchored Models
for id, model, transform in world:query(Components.Model, Components.Transform) do
for id, model, transform in cx.world:query(Components.Model, Components.Transform) do
if model.model.PrimaryPart.Anchored then
continue
end
Expand All @@ -48,12 +48,12 @@ local function updateTransforms(world)

-- Despawn models that fall into the void
if currentCFrame.Y < -400 then
world:despawn(id)
cx.world:despawn(id)
continue
end

if currentCFrame ~= existingCFrame then
world:insert(
cx.world:insert(
id,
Components.Transform({
cframe = currentCFrame,
Expand Down
6 changes: 3 additions & 3 deletions example/src/shared/setupTags.luau
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ local boundTags = {
Spinner = Components.Spinner,
}

local function setupTags(world)
local function setupTags(cx)
local function spawnBound(instance, component)
local id = world:spawn(
local id = cx.world:spawn(
component(),
Components.Model({
model = instance,
Expand All @@ -33,7 +33,7 @@ local function setupTags(world)
CollectionService:GetInstanceRemovedSignal(tagName):Connect(function(instance)
local id = instance:GetAttribute("serverEntityId")
if id then
world:despawn(id)
cx.world:despawn(id)
end
end)
end
Expand Down
Loading

0 comments on commit 4440052

Please sign in to comment.