Skip to content

Commit e65fc5a

Browse files
committed
feat: intaking and ejecting game pieces across multiplayer
1 parent f70da18 commit e65fc5a

File tree

5 files changed

+87
-46
lines changed

5 files changed

+87
-46
lines changed

fission/src/mirabuf/EjectableSceneObject.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import MirabufSceneObject from "./MirabufSceneObject"
1414
import ScoringZoneSceneObject from "./ScoringZoneSceneObject"
1515

1616
class EjectableSceneObject extends SceneObject {
17-
private _parentAssembly: MirabufSceneObject
18-
private _gamePieceBodyId?: Jolt.BodyID
17+
private _parentSceneObject: MirabufSceneObject
18+
private _gamePieceBodyId: Jolt.BodyID
1919

2020
private _parentBodyId?: Jolt.BodyID
2121
private _deltaTransformation?: THREE.Matrix4
@@ -44,25 +44,29 @@ class EjectableSceneObject extends SceneObject {
4444
return this._parentBodyId
4545
}
4646

47+
public get parentSceneObject(): MirabufSceneObject {
48+
return this._parentSceneObject
49+
}
50+
4751
public constructor(parentAssembly: MirabufSceneObject, gamePieceBody: Jolt.BodyID) {
4852
super()
4953

5054
console.debug("Trying to create ejectable...")
5155

52-
this._parentAssembly = parentAssembly
56+
this._parentSceneObject = parentAssembly
5357
this._gamePieceBodyId = gamePieceBody
5458
}
5559

5660
public setup(): void {
57-
if (this._parentAssembly.ejectorPreferences && this._gamePieceBodyId) {
58-
this._parentBodyId = this._parentAssembly.mechanism.nodeToBody.get(
59-
this._parentAssembly.ejectorPreferences.parentNode ?? this._parentAssembly.rootNodeId
61+
if (this._parentSceneObject.ejectorPreferences && this._gamePieceBodyId) {
62+
this._parentBodyId = this._parentSceneObject.mechanism.nodeToBody.get(
63+
this._parentSceneObject.ejectorPreferences.parentNode ?? this._parentSceneObject.rootNodeId
6064
)
6165

6266
this._deltaTransformation = convertArrayToThreeMatrix4(
63-
this._parentAssembly.ejectorPreferences.deltaTransformation
67+
this._parentSceneObject.ejectorPreferences.deltaTransformation
6468
)
65-
this._ejectVelocity = this._parentAssembly.ejectorPreferences.ejectorVelocity
69+
this._ejectVelocity = this._parentSceneObject.ejectorPreferences.ejectorVelocity
6670

6771
// Record start transform at the game piece center of mass
6872
const gpBody = World.physicsSystem.getBody(this._gamePieceBodyId)

fission/src/mirabuf/MirabufSceneObject.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -579,9 +579,7 @@ class MirabufSceneObject extends SceneObject implements ContextSupplier {
579579

580580
// 3) avoid duplicates
581581
const key = bodyId.GetIndexAndSequenceNumber()
582-
if (this._ejectables.some(e => e.gamePieceBodyId!.GetIndexAndSequenceNumber() === key)) {
583-
return false
584-
}
582+
if (this._ejectables.some(e => e.gamePieceBodyId!.GetIndexAndSequenceNumber() === key)) return false
585583

586584
const ejectable = new EjectableSceneObject(this, bodyId)
587585
this._ejectables.push(ejectable)

fission/src/systems/multiplayer/MultiplayerSystem.ts

Lines changed: 66 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -241,43 +241,74 @@ class MultiplayerSystem {
241241
}
242242

243243
handlePeerUpdate(data: UpdateObjectData[]) {
244-
data.forEach(({ sceneObjectKey, linearVelocityStr, angularVelocityStr, positionStr, rotationStr }) => {
245-
// const rootBody: Jolt.Body = JSON.parse(encodedRootBody)
246-
const lin: { x: number; y: number; z: number } = JSON.parse(linearVelocityStr)
247-
const ang: { x: number; y: number; z: number } = JSON.parse(angularVelocityStr)
248-
const pos: { x: number; y: number; z: number } = JSON.parse(positionStr)
249-
const rot: { x: number; y: number; z: number; w: number } = JSON.parse(rotationStr)
250-
251-
const linearVelocity = new JOLT.Vec3(lin.x, lin.y, lin.z)
252-
const angularVelocity = new JOLT.Vec3(ang.x, ang.y, ang.z)
253-
const position = new JOLT.RVec3(pos.x, pos.y, pos.z)
254-
const rotation = new JOLT.Quat(rot.x, rot.y, rot.z, rot.w)
255-
256-
const sceneObject = World.sceneRenderer.sceneObjects.get(sceneObjectKey)
257-
if (sceneObject == null) {
258-
console.error(
259-
`Multiplayer SceneObject: ${sceneObjectKey} not found in sceneObjects map. Multiplayer SceneObjects must be initialized before being updated.`
260-
)
261-
return
262-
} else if (!(sceneObject instanceof MirabufSceneObject)) {
263-
console.error(`Multiplayer SceneObject: ${sceneObjectKey} not MirabufSceneObject`)
264-
console.log(sceneObject)
265-
return
266-
}
244+
data.forEach(
245+
({
246+
sceneObjectKey,
247+
gamePiecesControlled,
248+
linearVelocityStr,
249+
angularVelocityStr,
250+
positionStr,
251+
rotationStr,
252+
}) => {
253+
// const rootBody: Jolt.Body = JSON.parse(encodedRootBody)
254+
const lin: { x: number; y: number; z: number } = JSON.parse(linearVelocityStr)
255+
const ang: { x: number; y: number; z: number } = JSON.parse(angularVelocityStr)
256+
const pos: { x: number; y: number; z: number } = JSON.parse(positionStr)
257+
const rot: { x: number; y: number; z: number; w: number } = JSON.parse(rotationStr)
258+
259+
const linearVelocity = new JOLT.Vec3(lin.x, lin.y, lin.z)
260+
const angularVelocity = new JOLT.Vec3(ang.x, ang.y, ang.z)
261+
const position = new JOLT.RVec3(pos.x, pos.y, pos.z)
262+
const rotation = new JOLT.Quat(rot.x, rot.y, rot.z, rot.w)
263+
264+
const sceneObject = World.sceneRenderer.sceneObjects.get(sceneObjectKey)
265+
if (sceneObject == null) {
266+
console.error(
267+
`Multiplayer SceneObject: ${sceneObjectKey} not found in sceneObjects map. Multiplayer SceneObjects must be initialized before being updated.`
268+
)
269+
return
270+
} else if (!(sceneObject instanceof MirabufSceneObject)) {
271+
console.error(`Multiplayer SceneObject: ${sceneObjectKey} not MirabufSceneObject`)
272+
console.log(sceneObject)
273+
return
274+
}
267275

268-
const clientMechanism = sceneObject.mechanism
269-
const clientBodyId = clientMechanism.nodeToBody.get(clientMechanism.rootBody)
270-
if (!clientBodyId) {
271-
console.error(`Body not found`)
272-
return
273-
}
276+
// Set all the ejectables that are in activeEjectables but not gamePiecesControlled
277+
sceneObject.activeEjectables
278+
.filter(id => !gamePiecesControlled.includes(id.GetIndexAndSequenceNumber()))
279+
// We're not ejecting the actual game piece here, but the robots should be configured to eject in the same order so it's fine
280+
.forEach(_ => sceneObject.eject())
281+
282+
// Set all the ejectables that are in gamePiecesControlled but not activeEjectables
283+
gamePiecesControlled
284+
.filter(id => !sceneObject.activeEjectables.map(n => n.GetIndexAndSequenceNumber()).includes(id))
285+
.forEach(id => {
286+
const bodyId = new JOLT.BodyID(id)
287+
return sceneObject.setEjectable(bodyId)
288+
})
274289

275-
const clientBody = World.physicsSystem.getBody(clientBodyId)!
276-
clientBody.SetLinearVelocity(linearVelocity)
277-
clientBody.SetAngularVelocity(angularVelocity)
278-
World.physicsSystem.setBodyPosition(clientBodyId, position)
279-
World.physicsSystem.setBodyRotation(clientBodyId, rotation)
280-
})
290+
// const piecesSet = gamePiecesControlled.map(id => {
291+
// const bodyId = new JOLT.BodyID(id)
292+
// return sceneObject.setEjectable(bodyId)
293+
// })
294+
// if (piecesSet.some(success => !success)) {
295+
// console.error(`Failed to set object(s) as ejectables`)
296+
// }
297+
298+
const clientMechanism = sceneObject.mechanism
299+
const clientBodyId = clientMechanism.nodeToBody.get(clientMechanism.rootBody)
300+
if (!clientBodyId) {
301+
console.error(`Body not found`)
302+
return
303+
}
304+
305+
const clientBody = World.physicsSystem.getBody(clientBodyId)!
306+
clientBody.SetLinearVelocity(linearVelocity)
307+
clientBody.SetAngularVelocity(angularVelocity)
308+
World.physicsSystem.setBodyPosition(clientBodyId, position)
309+
World.physicsSystem.setBodyRotation(clientBodyId, rotation)
310+
}
311+
)
281312
}
282313

283314
handleCollision(data: CollisionData) {

fission/src/systems/multiplayer/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ export type InitData = {
6363

6464
export type UpdateObjectData = {
6565
sceneObjectKey: number
66+
gamePiecesControlled: number[] // BodyID
6667
// {x, y, z, w?}
6768
linearVelocityStr: string
6869
angularVelocityStr: string

fission/src/systems/physics/PhysicsSystem.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,13 +1325,20 @@ class PhysicsSystem extends WorldSystem {
13251325
if (!rootBodyId) return
13261326
const rootBody = World.physicsSystem.getBody(rootBodyId)
13271327

1328+
const sceneObject = World.sceneRenderer.sceneObjects.get(
1329+
sceneObjectKey
1330+
) as MirabufSceneObject
1331+
const gamePiecesControlled: number[] = sceneObject.activeEjectables.map(bodyId =>
1332+
bodyId.GetIndexAndSequenceNumber()
1333+
)
13281334
const linearVelocity = rootBody.GetLinearVelocity()
13291335
const angularVelocity = rootBody.GetAngularVelocity()
13301336
const position = rootBody.GetPosition()
13311337
const rotation = rootBody.GetRotation()
13321338

13331339
return {
13341340
sceneObjectKey,
1341+
gamePiecesControlled,
13351342
linearVelocityStr: `{"x": ${linearVelocity.GetX()}, "y": ${linearVelocity.GetY()}, "z": ${linearVelocity.GetZ()}}`,
13361343
angularVelocityStr: `{"x": ${angularVelocity.GetX()}, "y": ${angularVelocity.GetY()}, "z": ${angularVelocity.GetZ()}}`,
13371344
positionStr: `{"x": ${position.GetX()}, "y": ${position.GetY()}, "z": ${position.GetZ()}}`,

0 commit comments

Comments
 (0)