forked from c-frame/aframe-extras
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjump-ability.js
62 lines (54 loc) · 1.78 KB
/
jump-ability.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
const ACCEL_G = -9.8, // m/s^2
EASING = -15; // m/s^2
/**
* Jump ability.
*/
module.exports = AFRAME.registerComponent('jump-ability', {
dependencies: ['velocity'],
/* Schema
——————————————————————————————————————————————*/
schema: {
on: { default: 'keydown:Space gamepadbuttondown:0' },
playerHeight: { default: 1.764 },
maxJumps: { default: 1 },
distance: { default: 5 },
debug: { default: false }
},
init: function () {
this.velocity = 0;
this.numJumps = 0;
const beginJump = this.beginJump.bind(this),
events = this.data.on.split(' ');
this.bindings = {};
for (let i = 0; i < events.length; i++) {
this.bindings[events[i]] = beginJump;
this.el.addEventListener(events[i], beginJump);
}
this.bindings.collide = this.onCollide.bind(this);
this.el.addEventListener('collide', this.bindings.collide);
},
remove: function () {
for (var event in this.bindings) {
if (this.bindings.hasOwnProperty(event)) {
this.el.removeEventListener(event, this.bindings[event]);
delete this.bindings[event];
}
}
this.el.removeEventListener('collide', this.bindings.collide);
delete this.bindings.collide;
},
beginJump: function () {
if (this.numJumps < this.data.maxJumps) {
const data = this.data,
initialVelocity = Math.sqrt(-2 * data.distance * (ACCEL_G + EASING)),
v = this.el.getAttribute('velocity');
this.el.setAttribute('velocity', {x: v.x, y: initialVelocity, z: v.z});
this.numJumps++;
this.el.emit('jumpstart');
}
},
onCollide: function () {
if (this.numJumps > 0) this.el.emit('jumpend');
this.numJumps = 0;
}
});