-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathhold-system.js
85 lines (74 loc) · 3.08 KB
/
hold-system.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { paths } from "./userinput/paths";
import { addComponent, removeComponent, defineQuery, hasComponent } from "bitecs";
import {
Held,
Holdable,
Pinned,
HoveredRemoteRight,
HeldRemoteRight,
HoveredRemoteLeft,
HeldRemoteLeft,
HoveredHandRight,
HeldHandRight,
HoveredHandLeft,
HeldHandLeft,
AEntity
} from "../bit-components";
import { canMove } from "../utils/permissions-utils";
const GRAB_REMOTE_RIGHT = paths.actions.cursor.right.grab;
const DROP_REMOTE_RIGHT = paths.actions.cursor.right.drop;
const GRAB_REMOTE_LEFT = paths.actions.cursor.left.grab;
const DROP_REMOTE_LEFT = paths.actions.cursor.left.drop;
const GRAB_HAND_RIGHT = paths.actions.rightHand.grab;
const DROP_HAND_RIGHT = paths.actions.rightHand.drop;
const GRAB_HAND_LEFT = paths.actions.leftHand.grab;
const DROP_HAND_LEFT = paths.actions.leftHand.drop;
function hasPermissionToGrab(world, eid) {
if (!hasComponent(world, AEntity, eid)) return true;
return canMove(world.eid2obj.get(eid).el);
}
function grab(world, userinput, queryHovered, held, grabPath) {
const hovered = queryHovered(world)[0];
if (
hovered &&
userinput.get(grabPath) &&
(!hasComponent(world, Pinned, hovered) || AFRAME.scenes[0].is("frozen")) &&
hasPermissionToGrab(world, hovered)
) {
addComponent(world, held, hovered);
addComponent(world, Held, hovered);
}
}
function drop(world, userinput, queryHeld, held, dropPath) {
const heldEid = queryHeld(world)[0];
if (heldEid && userinput.get(dropPath)) {
// TODO: Drop on ownership lost
removeComponent(world, held, heldEid);
if (
!hasComponent(world, HeldRemoteRight, heldEid) &&
!hasComponent(world, HeldRemoteLeft, heldEid) &&
!hasComponent(world, HeldHandRight, heldEid) &&
!hasComponent(world, HeldHandLeft, heldEid)
) {
removeComponent(world, Held, heldEid);
}
}
}
const queryHeldRemoteRight = defineQuery([Holdable, HeldRemoteRight]);
const queryHoveredRemoteRight = defineQuery([Holdable, HoveredRemoteRight]);
const queryHeldRemoteLeft = defineQuery([Holdable, HeldRemoteLeft]);
const queryHoveredRemoteLeft = defineQuery([Holdable, HoveredRemoteLeft]);
const queryHeldHandRight = defineQuery([Holdable, HeldHandRight]);
const queryHoveredHandRight = defineQuery([Holdable, HoveredHandRight]);
const queryHeldHandLeft = defineQuery([Holdable, HeldHandLeft]);
const queryHoveredHandLeft = defineQuery([Holdable, HoveredHandLeft]);
export function holdSystem(world, userinput) {
grab(world, userinput, queryHoveredRemoteRight, HeldRemoteRight, GRAB_REMOTE_RIGHT);
grab(world, userinput, queryHoveredRemoteLeft, HeldRemoteLeft, GRAB_REMOTE_LEFT);
grab(world, userinput, queryHoveredHandRight, HeldHandRight, GRAB_HAND_RIGHT);
grab(world, userinput, queryHoveredHandLeft, HeldHandLeft, GRAB_HAND_LEFT);
drop(world, userinput, queryHeldRemoteRight, HeldRemoteRight, DROP_REMOTE_RIGHT);
drop(world, userinput, queryHeldRemoteLeft, HeldRemoteLeft, DROP_REMOTE_LEFT);
drop(world, userinput, queryHeldHandRight, HeldHandRight, DROP_HAND_RIGHT);
drop(world, userinput, queryHeldHandLeft, HeldHandLeft, DROP_HAND_LEFT);
}