Skip to content

Commit

Permalink
Move bitecs canMove to its own function
Browse files Browse the repository at this point in the history
  • Loading branch information
keianhzo committed Sep 15, 2023
1 parent 5569aee commit dc0d510
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 27 deletions.
7 changes: 6 additions & 1 deletion src/systems/hold-system.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
AEntity
} from "../bit-components";
import { canMove } from "../utils/permissions-utils";
import { canMove as canMoveEntity } from "../utils/bit-permissions-utils";
import { isPinned } from "../bit-systems/networking";

const GRAB_REMOTE_RIGHT = paths.actions.cursor.right.grab;
Expand All @@ -27,7 +28,11 @@ const GRAB_HAND_LEFT = paths.actions.leftHand.grab;
const DROP_HAND_LEFT = paths.actions.leftHand.drop;

function hasPermissionToGrab(world, eid) {
return canMove(hasComponent(world, AEntity, eid) ? world.eid2obj.get(eid).el : eid);
if (hasComponent(world, AEntity, eid)) {
return canMove(world.eid2obj.get(eid).el);
} else {
return canMoveEntity(eid);
}
}

function isAEntityPinned(world, eid) {
Expand Down
10 changes: 10 additions & 0 deletions src/utils/bit-permissions-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { hasComponent } from "bitecs";
import { HoldableButton } from "../bit-components";
import { isPinned } from "../bit-systems/networking";

export function canMove(eid) {
return (
hasComponent(APP.world, HoldableButton, eid) ||
(APP.hubChannel.can("spawn_and_move_media") && (!isPinned(eid) || APP.hubChannel.can("pin_objects")))
);
}
42 changes: 16 additions & 26 deletions src/utils/permissions-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import { hasComponent } from "bitecs";
import { HoldableButton } from "../bit-components";
import { isPinned } from "../bit-systems/networking";

// https://github.com/mozilla/hubs/wiki/Hubs-authorization
export function showHoverEffect(el) {
Expand All @@ -19,31 +18,22 @@ export function showHoverEffect(el) {
return (isSpawner || !isPinned || isFrozen) && canMove;
}

export function canMove(elOrEid) {
if (!elOrEid.isEntity) {
return (
hasComponent(APP.world, HoldableButton, elOrEid) ||
(window.APP.hubChannel.can("spawn_and_move_media") &&
(!isPinned(elOrEid) || window.APP.hubChannel.can("pin_objects")))
);
} else {
const isPinned = elOrEid.components.pinnable && elOrEid.components.pinnable.data.pinned;
// TODO Move pen/emojis to bitECS when it's migrated
const networkedTemplate = elOrEid && elOrEid.components.networked && elOrEid.components.networked.data.template;
const isPen = networkedTemplate === "#interactable-pen";
const spawnerTemplate =
elOrEid && elOrEid.components["super-spawner"] && elOrEid.components["super-spawner"].data.template;
const isEmojiSpawner = spawnerTemplate === "#interactable-emoji";
const isEmoji = !!elOrEid.components.emoji;
return (
hasComponent(APP.world, HoldableButton, elOrEid.eid) ||
((isEmoji || isEmojiSpawner
? window.APP.hubChannel.can("spawn_emoji")
: window.APP.hubChannel.can("spawn_and_move_media")) &&
(!isPinned || window.APP.hubChannel.can("pin_objects")) &&
(!isPen || window.APP.hubChannel.can("spawn_drawing")))
);
}
export function canMove(entity) {
const isPinned = entity.components.pinnable && entity.components.pinnable.data.pinned;
const networkedTemplate = entity && entity.components.networked && entity.components.networked.data.template;
const isPen = networkedTemplate === "#interactable-pen";
const spawnerTemplate =
entity && entity.components["super-spawner"] && entity.components["super-spawner"].data.template;
const isEmojiSpawner = spawnerTemplate === "#interactable-emoji";
const isEmoji = !!entity.components.emoji;
return (
hasComponent(APP.world, HoldableButton, entity.eid) ||
((isEmoji || isEmojiSpawner
? window.APP.hubChannel.can("spawn_emoji")
: window.APP.hubChannel.can("spawn_and_move_media")) &&
(!isPinned || window.APP.hubChannel.can("pin_objects")) &&
(!isPen || window.APP.hubChannel.can("spawn_drawing")))
);
}

function indexForComponent(component, schema) {
Expand Down

0 comments on commit dc0d510

Please sign in to comment.