Skip to content

Commit

Permalink
Update example to use replication
Browse files Browse the repository at this point in the history
  • Loading branch information
evaera committed Jun 22, 2022
1 parent 49ac08e commit 5fd3371
Show file tree
Hide file tree
Showing 15 changed files with 242 additions and 30 deletions.
27 changes: 21 additions & 6 deletions example.project.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,34 @@
"$className": "ServerScriptService",
"Game": {
"$path": "example/server"
},
"ExamplePackages": {
"$path": "example/Packages"
}
},
"ReplicatedStorage": {
"$className": "ReplicatedStorage",
"$path": "Packages",

"Assets": {
"$path": "example/assets.rbxm"
},
"Matter": {
"$path": "lib"
"Packages": {
"$path": "Packages",
"Matter": {
"$path": "lib"
}
},
"ExamplePackages": {
"$path": "example/Packages"
},
"Game": {
"$path": "example/shared"
}
},
"StarterPlayer": {
"$className": "StarterPlayer",
"StarterPlayerScripts": {
"$className": "StarterPlayerScripts",
"Game": {
"$path": "example/client"
}
}
}
}
Expand Down
Binary file modified example/assets.rbxm
Binary file not shown.
89 changes: 89 additions & 0 deletions example/client/init.client.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
local RunService = game:GetService("RunService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Packages = ReplicatedStorage.ExamplePackages
local Matter = require(ReplicatedStorage.Packages.Matter)
local Plasma = require(Packages.plasma)
local Components = require(ReplicatedStorage.Game.components)
local RemoteEvent = ReplicatedStorage:WaitForChild("MatterRemote")

local world = Matter.World.new()
local state = {}
local loop = Matter.Loop.new(world, state)

local systems = {}
for _, child in ipairs(script.systems:GetChildren()) do
if child:IsA("ModuleScript") then
table.insert(systems, require(child))
end
end

loop:scheduleSystems(systems)

local plasmaNode = Plasma.new(workspace)

loop:addMiddleware(function(nextFn)
return function()
Plasma.start(plasmaNode, nextFn)
end
end)

loop:begin({
default = RunService.Heartbeat,
RenderStepped = RunService.RenderStepped,
})

local entityIdMap = {}

RemoteEvent.OnClientEvent:Connect(function(entities)
for serverEntityId, componentMap in entities do
local clientEntityId = entityIdMap[serverEntityId]

if clientEntityId and next(componentMap) == nil then
world:despawn(clientEntityId)
print(string.format("Despawn %ds%d", clientEntityId, serverEntityId))
continue
end

local componentsToInsert = {}
local componentsToRemove = {}

local insertNames = {}
local removeNames = {}

for name, container in componentMap do
if container.data then
table.insert(componentsToInsert, Components[name](container.data))
table.insert(insertNames, name)
else
table.insert(componentsToRemove, Components[name])
table.insert(removeNames, name)
end
end

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

entityIdMap[serverEntityId] = clientEntityId

print(string.format("Spawn %ds%d with %s", clientEntityId, serverEntityId, table.concat(insertNames, ",")))
else
if #componentsToInsert > 0 then
world:insert(clientEntityId, unpack(componentsToInsert))
end

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

print(
string.format(
"Modify %ds%d adding %s, removing %s",
clientEntityId,
serverEntityId,
if #insertNames > 0 then table.concat(insertNames, ", ") else "nothing",
if #removeNames > 0 then table.concat(removeNames, ", ") else "nothing"
)
)
end
end
end)
45 changes: 45 additions & 0 deletions example/client/systems/debugVision.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
local HttpService = game:GetService("HttpService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Components = require(ReplicatedStorage.Game.components)
local template = ReplicatedStorage.Assets.BillboardGui

local function rainbowRoombas(world)
for id, model in world:query(Components.Model) do
local debugLabel = world:get(id, Components.DebugLabel)

if not debugLabel then
local label = template:Clone()
label.Parent = model.model.PrimaryPart
label.Adornee = model.model.PrimaryPart

debugLabel = Components.DebugLabel({
label = label,
})

world:insert(id, debugLabel)
end

local text = ""

for _, component in Components do
local data = world:get(id, component)

if data then
text ..= tostring(component) .. " {"

if next(data) then
for key, value in data do
text ..= "\n " .. key .. ": " .. tostring(value)
end

text ..= "\n"
end
text ..= "}\n"
end
end

debugLabel.label.TextLabel.Text = text
end
end

return rainbowRoombas
7 changes: 3 additions & 4 deletions example/server/init.server.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
local RunService = game:GetService("RunService")
local CollectionService = game:GetService("CollectionService")
local ServerScriptService = game:GetService("ServerScriptService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Packages = ServerScriptService.ExamplePackages
local Matter = require(ReplicatedStorage.Matter)
local Packages = ReplicatedStorage.ExamplePackages
local Matter = require(ReplicatedStorage.Packages.Matter)
local Plasma = require(Packages.plasma)
local Components = require(script.components)
local Components = require(ReplicatedStorage.Game.components)

local world = Matter.World.new()
local state = {}
Expand Down
8 changes: 5 additions & 3 deletions example/server/systems/mothershipsSpawnRoombas.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Components = require(script.Parent.Parent.components)
local Matter = require(ReplicatedStorage.Matter)
local Components = require(ReplicatedStorage.Game.components)
local Matter = require(ReplicatedStorage.Packages.Matter)

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

lasering = lasering:patch({
Expand Down
4 changes: 2 additions & 2 deletions example/server/systems/playersAreTargets.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Components = require(script.Parent.Parent.components)
local Matter = require(ReplicatedStorage.Matter)
local Components = require(ReplicatedStorage.Game.components)
local Matter = require(ReplicatedStorage.Packages.Matter)

local function playersAreTargets(world)
for _, player in ipairs(Players:GetPlayers()) do
Expand Down
4 changes: 2 additions & 2 deletions example/server/systems/removeMissingModels.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Components = require(script.Parent.Parent.components)
local Matter = require(ReplicatedStorage.Matter)
local Components = require(ReplicatedStorage.Game.components)
local Matter = require(ReplicatedStorage.Packages.Matter)

local function removeMissingModels(world)
for id, model in world:query(Components.Model) do
Expand Down
60 changes: 60 additions & 0 deletions example/server/systems/replication.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Components = require(ReplicatedStorage.Game.components)
local useEvent = require(ReplicatedStorage.Packages.Matter).useEvent

local RemoteEvent = Instance.new("RemoteEvent")
RemoteEvent.Name = "MatterRemote"
RemoteEvent.Parent = ReplicatedStorage

local replicatedComponents = {
Components.Roomba,
Components.Model,
Components.Health,
Components.Target,
Components.Mothership,
}

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

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

for component, componentData in entityData do
entityPayload[tostring(component)] = { data = componentData }
end
end

print("Sending initial payload to", player)
RemoteEvent:FireClient(player, payload)
end

local changes = {}

for _, component in replicatedComponents do
for entityId, record in 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
changes[key][name] = { data = record.new }
end
end
end

if next(changes) then
RemoteEvent:FireAllClients(changes)
end
end

return {
system = replication,
priority = math.huge,
}
4 changes: 2 additions & 2 deletions example/server/systems/roombasHurt.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Components = require(script.Parent.Parent.components)
local Matter = require(ReplicatedStorage.Matter)
local Components = require(ReplicatedStorage.Game.components)
local Matter = require(ReplicatedStorage.Packages.Matter)

local function roombasHurt(world)
for id, roomba, model in world:query(Components.Roomba, Components.Model) do
Expand Down
4 changes: 2 additions & 2 deletions example/server/systems/roombasMove.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Components = require(script.Parent.Parent.components)
local Matter = require(ReplicatedStorage.Matter)
local Components = require(ReplicatedStorage.Game.components)
local Matter = require(ReplicatedStorage.Packages.Matter)

local function roombasMove(world)
local targets = {}
Expand Down
9 changes: 5 additions & 4 deletions example/server/systems/spawnMotherships.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
local ServerScriptService = game:GetService("ServerScriptService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Components = require(script.Parent.Parent.components)
local Matter = require(ReplicatedStorage.Matter)
local Plasma = require(ServerScriptService.ExamplePackages.plasma)
local Components = require(ReplicatedStorage.Game.components)
local Matter = require(ReplicatedStorage.Packages.Matter)

local function spawnMotherships(world)
if Matter.useThrottle(10) then
Expand Down Expand Up @@ -38,7 +37,9 @@ local function spawnMotherships(world)
)
end

for id, mothership, transform in world:query(Components.Mothership, Components.Transform):without(Components.Lasering) do
for id, mothership, transform in
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)
Expand Down
4 changes: 2 additions & 2 deletions example/server/systems/spawnRoombas.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Components = require(script.Parent.Parent.components)
local Matter = require(ReplicatedStorage.Matter)
local Components = require(ReplicatedStorage.Game.components)
local Matter = require(ReplicatedStorage.Packages.Matter)

local function spawnRoombas(world)
for id, transform in world:query(Components.Transform, Components.Roomba):without(Components.Model) do
Expand Down
4 changes: 2 additions & 2 deletions example/server/systems/updateTransforms.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Components = require(script.Parent.Parent.components)
local Matter = require(ReplicatedStorage.Matter)
local Components = require(ReplicatedStorage.Game.components)
local Matter = require(ReplicatedStorage.Packages.Matter)
local removeMissingModels = require(script.Parent.removeMissingModels)

local function updateTransforms(world)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Matter = require(ReplicatedStorage.Matter)
local Matter = require(ReplicatedStorage.Packages.Matter)

local COMPONENTS = {
"Roomba",
Expand All @@ -10,6 +10,7 @@ local COMPONENTS = {
"Transform",
"Mothership",
"Lasering",
"DebugLabel",
}

local components = {}
Expand Down

0 comments on commit 5fd3371

Please sign in to comment.