-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
152 lines (127 loc) · 4.44 KB
/
index.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Set states based on thumbstick positions.
// controller: selector for the controller with the thumbstick
// bindings: stats to set for each of up/down/left/right.
// sensitivity: 0 to 1- how far off center thumbstick must be to count as movement.
AFRAME.registerComponent('thumbstick-states', {
schema: {
controller: {type: 'selector', default: "#lhand"},
bindings: {type: 'array', default: ["none", "none", "none", "none"]},
tBindings: {type: 'array', default: []},
gBindings: {type: 'array', default: []},
tgBindings: {type: 'array', default: []},
sensitivity: {type: 'number', default: 0.5}
},
multiple: true,
init() {
this.controller = this.data.controller;
this.listeners = {
thumbstickMoved: this.thumbstickMoved.bind(this),
triggerUp: this.triggerUp.bind(this),
triggerDown: this.triggerDown.bind(this),
gripUp: this.gripUp.bind(this),
gripDown: this.gripDown.bind(this),
}
this.states = {
gripDown: false,
triggerDown: false,
}
},
update() {
this.controller.addEventListener('thumbstickmoved',
this.listeners.thumbstickMoved);
this.controller.addEventListener('triggerup',
this.listeners.triggerUp);
this.controller.addEventListener('triggerdown',
this.listeners.triggerDown);
this.controller.addEventListener('gripup',
this.listeners.gripUp);
this.controller.addEventListener('gripdown',
this.listeners.gripDown);
this.updateBindings()
},
updateBindings() {
// clear all pre-existing state
const removeStates = (set) => set.forEach((item) => this.el.removeState(item) )
removeStates(this.data.bindings)
removeStates(this.data.tBindings)
removeStates(this.data.gBindings)
removeStates(this.data.tgBindings)
// now update bindings
var binding;
if (!this.states.triggerDown && !this.states.gripDown) {
binding = (x) => this.data.bindings[x]
}
else if (this.states.triggerDown && !this.states.gripDown) {
// trigger down. If tBinding not specified, fall back to regular bindings
binding = (x) => this.data.tBindings[x] ||
this.data.bindings[x]
}
else if (!this.states.triggerDown && this.states.gripDown) {
// grip down. If gBinding not specified, fall back to regular bindings
binding = (x) => this.data.gBindings[x] ||
this.data.bindings[x]
}
else {
// trigger and grip down. If tgBinding not specified, fall back to t, g, or regular bindings
binding = (x) => this.data.tgBindings[x] ||
this.data.gBindings[x] ||
this.data.tBindings[x] ||
this.data.bindings[x]
}
this.yplus = binding(0)
this.yminus = binding(1)
this.xplus = binding(2)
this.xminus = binding(3)
console.log(this)
},
gripDown(event) {
this.states.gripDown = true;
this.updateBindings()
},
gripUp(event) {
this.states.gripDown = false;
this.updateBindings()
},
triggerDown(event) {
this.states.triggerDown = true;
this.updateBindings()
},
triggerUp(event) {
this.states.triggerDown = false;
this.updateBindings()
},
thumbstickMoved(event) {
const x = event.detail.x
const y = event.detail.y
if (Math.abs(x) > this.data.sensitivity) {
if (x > 0) {
this.el.addState(this.xplus)
this.el.removeState(this.xminus)
}
else {
this.el.addState(this.xminus)
this.el.removeState(this.xplus)
}
}
else
{
this.el.removeState(this.xplus)
this.el.removeState(this.xminus)
}
if (Math.abs(y) > this.data.sensitivity) {
if (y > 0) {
this.el.addState(this.yplus)
this.el.removeState(this.yminus)
}
else {
this.el.addState(this.yminus)
this.el.removeState(this.yplus)
}
}
else
{
this.el.removeState(this.yplus)
this.el.removeState(this.yminus)
}
}
});