Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move message handling to tick #1

Merged
merged 1 commit into from
Aug 3, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 36 additions & 89 deletions src/ammo.worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -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);
}
};