-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdynamics.js
103 lines (92 loc) · 3.28 KB
/
dynamics.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
;(function() {
function getAccelX(x0v0) {
let v0 = x0v0[1];
if (window.INPUT.keys.downKey) {
ax = (v0 >= 0) ? -0.5*window.LEVEL.constants.accelX : 0.5*window.LEVEL.constants.accelX ;
} else if (window.INPUT.keys.rightKey && (window.INPUT.keys.xKeyReset)) {
ax = (v0 <= window.LEVEL.constants.maxVeloX) ? window.LEVEL.constants.accelX : 0;
} else if (window.INPUT.keys.leftKey && (window.INPUT.keys.xKeyReset)) {
ax = (v0 >= -window.LEVEL.constants.maxVeloX) ? -window.LEVEL.constants.accelX : 0;
} else if (v0 > 0 && (window.INPUT.keys.xKeyReset)) {
ax = -window.LEVEL.constants.decelX;
} else if (v0 < 0 && (window.INPUT.keys.xKeyReset)) {
ax = window.LEVEL.constants.decelX;
} else if (window.INPUT.keys.rightKey) {
ax = (v0 <= window.LEVEL.constants.maxVeloX) ? 0.3*window.LEVEL.constants.accelX : 0;
} else if (window.INPUT.keys.leftKey) {
ax = (v0 >= -window.LEVEL.constants.maxVeloX) ? -0.3*window.LEVEL.constants.accelX : 0;
} else {
ax = 0;
}
return ax;
}
function getVeloX(x0v0) {
let ax = getAccelX(x0v0);
let vx = x0v0[1] + ax;
if (vx * x0v0[1] < 0) {
vx = 0;
}
return vx;
}
function getPosX(x0v0) {
let levelWidth = window.LEVEL.constants.levelWidth;
let x0 = x0v0[0];
let ax = getAccelX(x0v0);
let vx = getVeloX(x0v0);
let posX = x0 + vx + ax;
if (posX <= 0) {
posX = x0v0[0];
vx = 0;
} else if ((posX) >= (levelWidth-64)) {
posX = x0v0[0];
vx = 0;
}
return [Math.round(posX), vx] // x0v0
}
function getAccelY(y0v0) {
if (y0v0[1] <= -window.LEVEL.constants.maxVeloY) {
ay = 0;
} else if (LEVEL.variables.onBlock) {
ay = 0;
} else {
ay = -window.LEVEL.constants.accelY;
}
return ay;
}
function getVeloY(y0v0) {
let ay = getAccelY(y0v0);
let vy = y0v0[1] + ay;
if (window.INPUT.keys.upKey && (window.INPUT.keys.upKeyReset < 2)) {
LEVEL.variables.onBlock = false;
vy += (window.INPUT.keys.upKeyReset == 0) ? window.LEVEL.constants.jumpVelocity : window.LEVEL.constants.jumpVelocity / 3;
window.INPUT.keys.upKey = false;
window.INPUT.keys.upKeyReset++;
}
return vy;
}
function getPosY(y0v0) {
let y0 = y0v0[0];
let vy = getVeloY(y0v0);
let ay = getAccelY(y0v0);
let posY = y0 + vy + ay;
if (posY <= 0 || LEVEL.variables.onBlock) {
window.INPUT.keys.upKeyReset = 0;
window.INPUT.keys.xKeyReset = true;
} else {
window.INPUT.keys.xKeyReset = false;
}
return([posY, vy]) // y0v0
}
// ******** EXPORTS ************
window.XY = window.XY || {}
XY.x0v0 = window.LEVEL.constants.x0v0, //Default values
XY.y0v0 = window.LEVEL.constants.y0v0
XY.getPosY = getPosY;
XY.getPosX = getPosX;
XY.updatePosX = function() {
XY.x0v0 = XY.getPosX(XY.x0v0);
}
XY.updatePosY = function() {
XY.y0v0 = XY.getPosY(XY.y0v0);
}
})()