forked from netpro2k/networked-aframe
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtoggle-ownership.component.js
52 lines (44 loc) · 1.3 KB
/
toggle-ownership.component.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
/**
* Rotate the entity every frame if you are the owner.
* When you press enter take ownership of the entity,
* spin it in the opposite direction and change its color.
*/
AFRAME.registerComponent('toggle-ownership', {
schema: {
speed: { default: 0.01 },
direction: { default: 1 }
},
init() {
this.onKeyUp = this.onKeyUp.bind(this);
document.addEventListener("keyup", this.onKeyUp);
if (NAF.utils.isMine(this.el)) {
this.updateColor();
}
},
onKeyUp(e) {
if (e.keyCode !== 13 /* enter */) {
return;
}
if(NAF.utils.takeOwnership(this.el)) {
this.el.setAttribute("toggle-ownership", { direction: this.data.direction * -1 });
this.updateColor();
}
},
updateColor() {
const headColor = document.querySelector("#player .head").getAttribute("material").color;
this.el.setAttribute('material', 'color', headColor);
},
tick() {
// Only update the component if you are the owner.
if (!NAF.utils.isMine(this.el)) {
return;
}
this.el.object3D.rotateY(this.data.speed * this.data.direction);
const rotation = this.el.object3D.rotation;
this.el.setAttribute("rotation", {
x: THREE.Math.radToDeg(rotation.x),
y: THREE.Math.radToDeg(rotation.y),
z: THREE.Math.radToDeg(rotation.z),
});
}
});