Skip to content

Commit edf9151

Browse files
committed
fix: construct newObjects on connect correctly
1 parent bb771d6 commit edf9151

File tree

1 file changed

+31
-23
lines changed

1 file changed

+31
-23
lines changed

fission/src/systems/multiplayer/MultiplayerSystem.ts

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import PreferencesSystem from "@/systems/preferences/PreferencesSystem.ts"
99
import type PhysicsSystem from "../physics/PhysicsSystem"
1010
import World from "../World"
1111
import { peerMessageHandlers } from "./MessageHandlers"
12-
import type { ClientInfo, EncodedAssembly, Message, MessageType } from "./types"
12+
import type { ClientInfo, InitObjectData, Message, MessageType } from "./types"
1313
import Jolt from "@azaleacolburn/jolt-physics"
1414

1515
export const COLLISION_TIMEOUT = 500
@@ -157,11 +157,17 @@ class MultiplayerSystem {
157157

158158
// Called by the host, initializes the world with some defined set of objects, robots can be spawned in later
159159
async initWorld(physicsSystem: PhysicsSystem) {
160-
const sceneObjects = World.sceneRenderer.mirabufSceneObjects
161-
.getAll()
162-
.map(sceneObject =>
163-
mirabuf.Assembly.encode(sceneObject.mirabufInstance.parser.assembly).finish()
164-
) as EncodedAssembly[]
160+
const sceneObjects: InitObjectData[] = await Promise.all(
161+
World.sceneRenderer.mirabufSceneObjects.getAll().map(async sceneObject => ({
162+
sceneObjectKey: sceneObject.id,
163+
assemblyHash: await MirabufCachingService.hashBuffer(
164+
mirabuf.Assembly.encode(sceneObject.mirabufInstance.parser.assembly).finish().buffer as ArrayBuffer
165+
),
166+
miraType: sceneObject.miraType,
167+
initialPreferences: sceneObject.getPreferenceData(),
168+
bodyIds: sceneObject.getAllBodyIds().map(id => id.GetIndexAndSequenceNumber()),
169+
}))
170+
)
165171

166172
await this.broadcast({
167173
type: "init",
@@ -179,23 +185,25 @@ class MultiplayerSystem {
179185
this._connections.set(conn.peer, conn)
180186
MultiplayerStateEvent.dispatch(MultiplayerStateEventType.PEER_CHANGE)
181187
await this.send(conn.peer, { type: "info", data: this.info })
182-
for (const objectId of this.getOwnSceneObjectIDs()) {
183-
const obj = World.sceneRenderer.sceneObjects.get(objectId)
184-
if (!(obj instanceof MirabufSceneObject)) return
185-
const hash = await MirabufCachingService.hashBuffer(
186-
mirabuf.Assembly.encode(obj.mirabufInstance.parser.assembly).finish()
187-
)
188-
await this.send(conn.peer, {
189-
type: "newObject",
190-
data: {
191-
sceneObjectKey: objectId,
192-
assemblyHash: hash,
193-
miraType: obj.miraType,
194-
initialPreferences: obj.getPreferenceData(),
195-
},
196-
})
197-
await this.send(conn.peer, { type: "metadataUpdate", data: obj.multiplayerInfo })
198-
}
188+
// I don't think this is necessary if we just move the call to initWorld
189+
// for (const objectId of this.getOwnSceneObjectIDs()) {
190+
// const obj = World.sceneRenderer.sceneObjects.get(objectId)
191+
// if (!(obj instanceof MirabufSceneObject)) return
192+
// const hash = await MirabufCachingService.hashBuffer(
193+
// mirabuf.Assembly.encode(obj.mirabufInstance.parser.assembly).finish().buffer as ArrayBuffer
194+
// )
195+
// await this.send(conn.peer, {
196+
// type: "newObject",
197+
// data: {
198+
// sceneObjectKey: objectId,
199+
// assemblyHash: hash,
200+
// miraType: obj.miraType,
201+
// initialPreferences: obj.getPreferenceData(),
202+
// bodyIds: obj.getAllBodies(),
203+
// },
204+
// })
205+
// await this.send(conn.peer, { type: "metadataUpdate", data: obj.multiplayerInfo })
206+
// }
199207
})
200208

201209
conn.on("data", async (data: unknown) => {

0 commit comments

Comments
 (0)