From 702bdae7d6beeafded29f56ec06cfc6cc3fdfa87 Mon Sep 17 00:00:00 2001 From: John Shaughnessy Date: Tue, 2 Aug 2022 21:55:40 -0400 Subject: [PATCH] Move message handling to tick --- src/ammo.worker.js | 125 +++++++++++++-------------------------------- 1 file changed, 36 insertions(+), 89 deletions(-) diff --git a/src/ammo.worker.js b/src/ammo.worker.js index 749e053..b71c479 100644 --- a/src/ammo.worker.js +++ b/src/ammo.worker.js @@ -96,14 +96,49 @@ const tick = () => { case MESSAGE_TYPES.ADD_SHAPES: addShapes(message); break; + case MESSAGE_TYPES.REMOVE_SHAPES: + const bodyUuid = message.bodyUuid; + const shapesUuid = message.shapesUuid; + if (bodies[bodyUuid] && shapes[shapesUuid]) { + for (let i = 0; i < shapes[shapesUuid].length; i++) { + const shape = shapes[shapesUuid][i]; + bodies[bodyUuid].removeShape(shape); + } + } + break; case MESSAGE_TYPES.ADD_CONSTRAINT: addConstraint(message); break; + case MESSAGE_TYPES.REMOVE_CONSTRAINT: + const constraintId = message.constraintId; + if (constraints[constraintId]) { + constraints[constraintId].destroy(); + delete constraints[constraintId]; + } + break; + case MESSAGE_TYPES.ENABLE_DEBUG: + const enable = message.enable; + if (!world.debugDrawer) { + initDebug(message.debugSharedArrayBuffer, world); + } + + if (world.debugDrawer) { + if (enable) { + world.debugDrawer.enable(); + } else { + world.debugDrawer.disable(); + } + } + break; case MESSAGE_TYPES.RESET_DYNAMIC_BODY: resetDynamicBody(message); break; case MESSAGE_TYPES.ACTIVATE_BODY: activateBody(message); + break; + default: + console.error("Unknown message in queue", message); + break; } } @@ -311,95 +346,7 @@ onmessage = async event => { } objectMatricesFloatArray = event.data.objectMatricesFloatArray; objectMatricesIntArray = new Int32Array(objectMatricesFloatArray.buffer); - } else if (world) { - switch (event.data.type) { - case MESSAGE_TYPES.ADD_BODY: { - messageQueue.push(event.data); - break; - } - - case MESSAGE_TYPES.UPDATE_BODY: { - messageQueue.push(event.data); - break; - } - - case MESSAGE_TYPES.REMOVE_BODY: { - const uuid = event.data.uuid; - if (uuids.indexOf(uuid) !== -1) { - messageQueue.push(event.data); - } - break; - } - - case MESSAGE_TYPES.ADD_SHAPES: { - const bodyUuid = event.data.bodyUuid; - if (bodies[bodyUuid]) { - addShapes(event.data); - } else { - messageQueue.push(event.data); - } - break; - } - - case MESSAGE_TYPES.REMOVE_SHAPES: { - const bodyUuid = event.data.bodyUuid; - const shapesUuid = event.data.shapesUuid; - if (bodies[bodyUuid] && shapes[shapesUuid]) { - for (let i = 0; i < shapes[shapesUuid].length; i++) { - const shape = shapes[shapesUuid][i]; - bodies[bodyUuid].removeShape(shape); - } - } - break; - } - - case MESSAGE_TYPES.ADD_CONSTRAINT: { - const bodyUuid = event.data.bodyUuid; - const targetUuid = event.data.targetUuid; - if (bodies[bodyUuid] && bodies[targetUuid]) { - addConstraint(event.data); - } else { - messageQueue.push(event.data); - } - break; - } - - case MESSAGE_TYPES.REMOVE_CONSTRAINT: { - const constraintId = event.data.constraintId; - if (constraints[constraintId]) { - constraints[constraintId].destroy(); - delete constraints[constraintId]; - } - break; - } - - case MESSAGE_TYPES.ENABLE_DEBUG: { - const enable = event.data.enable; - if (!world.debugDrawer) { - initDebug(event.data.debugSharedArrayBuffer, world); - } - - if (world.debugDrawer) { - if (enable) { - world.debugDrawer.enable(); - } else { - world.debugDrawer.disable(); - } - } - break; - } - - case MESSAGE_TYPES.RESET_DYNAMIC_BODY: { - messageQueue.push(event.data); - break; - } - - case MESSAGE_TYPES.ACTIVATE_BODY: { - messageQueue.push(event.data); - break; - } - } } else { - console.error("Error: World Not Initialized", event.data); + messageQueue.push(event.data); } };