forked from hackclub/sprig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoding_demo_5_gravity.js
120 lines (111 loc) · 1.96 KB
/
coding_demo_5_gravity.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
/*
@title: Coding Demo 5: Gravity
@author: Leonard (Omay)
@tags: ['simulation']
@addedOn: 2022-11-12
Comments labeled include on their own line mean copy the code from the include to end include
Try and put the code in the same places
*/
//INCLUDE
let vy = 0;
const jumpHeight = -3;
const gravity = 2;
const terminalVelocity = 10;
const minTime = 50;
const maxTime = 250;
//END INCLUDE
const player = "p";
const wall = "w";
setLegend(
[player, bitmap`
.....00000......
.....0...0..0...
...0.00000..0...
...00..00..00...
....00.0..00....
.....000000.....
......000.......
.......0........
.......0........
.......0........
.....00000......
....00....0.....
...00.....0.....
...0.......0....
..00.......0....
..0........0....`],
[wall, bitmap`
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000`]
);
setSolids([player, wall]);
let level = 0;
const levels = [
map`
.ww.......
..........
www..www..
w....www..
...wwwww..
.....www..
.www.www..
pwww......`,
];
setMap(levels[level]);
setPushables({
[player]: [],
});
//INCLUDE
function checkGrounded(obj){
var py = obj.y;
obj.y++;
if(py === obj.y){
return true;
}else{
obj.y--;
return false;
}
}
//END INCLUDE
onInput("w", () => {
//INCLUDE
if(checkGrounded(getFirst(player))){
vy = jumpHeight;
}
//END INCLUDE
});
onInput("a", () => {
getFirst(player).x--;
});
onInput("d", () => {
getFirst(player).x++;
});
//INCLUDE
function lerp(a, b, f){
return (a * (1 - f)) + (b * f);
}
function constrain(n, mi, ma){
return (n < mi) ? mi : ((n > ma) ? ma : n);
}
function tick(){
getFirst(player).y += Math.sign(vy);
vy += gravity;
vy = constrain(vy, -terminalVelocity, terminalVelocity);
setTimeout(tick, lerp(minTime, maxTime, Math.abs(vy)/terminalVelocity));
}
tick();
//END INCLUDE