forked from hackclub/sprig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBuilder.js
224 lines (216 loc) · 4.33 KB
/
Builder.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
@title: Builder
@author: Leonard (Omay)
@tags: ['strategy']
@addedOn: 2022-11-06
WAD to move
S to reset
IJKL to make boxes
Get a box to the goal
*/
const player = "p";
const wall = "w";
const box = "b";
const goal = "g";
setLegend(
[player, bitmap`
0......00......0
.0....LLLL....0.
..0..111111..0..
...0000000000...
...0........0...
..10.00..00.01..
.L10.00..00.01L.
0L10........01L0
0L10........01L0
.L10.0....0.01L.
..10..0000..01..
...0........0...
...0000000000...
..0..111111..0..
.0....LLLL....0.
0......00......0`],
[wall, bitmap`
0000000000000000
0LLLLLLLLLLLLLL0
0L000000000000L0
0L000000000000L0
0L000000000000L0
0L000000000000L0
0L000000000000L0
0L000000000000L0
0L000000000000L0
0L000000000000L0
0L000000000000L0
0L000000000000L0
0L000000000000L0
0L000000000000L0
0LLLLLLLLLLLLLL0
0000000000000000`],
[box, bitmap`
L111111111111111
LL1111111111111L
LLL11111111111LL
LLL0000000000LLL
LLL0000000000LLL
LLL0000000000LLL
LLL0000000000LLL
LLL0000000000LLL
LLL0000000000LLL
LLL0000000000LLL
LLL0000000000LLL
LLL0000000000LLL
LLL0000000000LLL
LL11111111111LLL
L1111111111111LL
111111111111111L`],
[goal, bitmap`
1111111111111111
1LLLLLLLLLLLLLL1
1L000000000000L1
1L000000000000L1
1L000000000000L1
1L000000000000L1
1L000000000000L1
1L000000000000L1
1L000000000000L1
1L000000000000L1
1L000000000000L1
1L000000000000L1
1L000000000000L1
1L000000000000L1
1LLLLLLLLLLLLLL1
1111111111111111`]
);
setSolids([player, box, wall]);
let level = 0;
const levels = [
map`
..wg
..w.
....
p...`,
map`
..ww
...g
..ww
p.ww`,
map`
w...
wgw.
www.
p...`,
map`
...wg
.w.w.
.w.w.
pw...`
];
setMap(levels[level]);
setPushables({
[player]: [box],
[box]: [box]
});
var velocity = 0;
var canGoDown = false;
function isOnGround(obj, ground){
var below = getTile(obj.x, obj.y+1).map(x => x.type);
for(var i = 0; i < below.length; i++){
if(ground.includes(below[i])){
return true;
}
}
if(obj.y === height()-1){
return true;
}else{
return false;
}
}
onInput("w", () => {
if(isOnGround(getFirst(player), [box, wall])){
velocity = 1;
canGoDown = false;
var p = getFirst(player);
var y = p.y;
p.y -= velocity;
if(p.y === y && velocity !== 0){
velocity = 0;
}else{
velocity = -1;
}
}
});
onInput("a", () => {
getFirst(player).x -= 1;
});
onInput("d", () => {
getFirst(player).x += 1;
});
onInput("s", () => {
setMap(levels[level]);
});
onInput("i", () => {
var p = getFirst(player);
if(getTile(p.x, p.y-1).map(x => x.type).includes(box)){
getTile(p.x, p.y-1)[getTile(p.x, p.y-1).map(x => x.type).indexOf(box)].y--;
}
if((getTile(p.x, p.y-1).map(x => x.type).includes(goal) || getTile(p.x, p.y-1).length === 0) && p.y > 0){
addSprite(p.x, p.y-1, box);
}
});
onInput("j", () => {
var p = getFirst(player);
if(getTile(p.x-1, p.y).map(x => x.type).includes(box)){
getTile(p.x-1, p.y)[getTile(p.x-1, p.y).map(x => x.type).indexOf(box)].x--;
}
if((getTile(p.x-1, p.y).map(x => x.type).includes(goal) || getTile(p.x-1, p.y).length === 0) && p.x > 0){
addSprite(p.x-1, p.y, box);
}
});
onInput("k", () => {
var p = getFirst(player);
if(getTile(p.x, p.y+1).map(x => x.type).includes(box)){
getTile(p.x, p.y+1)[getTile(p.x, p.y+1).map(x => x.type).indexOf(box)].y++;
}
if((getTile(p.x, p.y+1).map(x => x.type).includes(goal) || getTile(p.x, p.y+1).length === 0) && p.y < height()-1){
addSprite(p.x, p.y+1, box);
}
});
onInput("l", () => {
var p = getFirst(player);
if(getTile(p.x+1, p.y).map(x => x.type).includes(box)){
getTile(p.x+1, p.y)[getTile(p.x+1, p.y).map(x => x.type).indexOf(box)].x++;
}
if((getTile(p.x+1, p.y).map(x => x.type).includes(goal) || getTile(p.x+1, p.y).length === 0) && p.x < width()-1){
addSprite(p.x+1, p.y, box);
}
});
setInterval(() => {
var b = getAll(box);
for(var i = 0; i < b.length; i++){
b[i].y++;
}
if(canGoDown || velocity > 0){
var p = getFirst(player);
var y = p.y;
if((velocity < 0 && !isOnGround(p, [box, wall])) || velocity > 0){
p.y -= velocity;
}
if(p.y === y && velocity !== 0){
velocity = 0;
}else{
velocity--;
}
}
canGoDown = true;
}, 500);
afterInput(() => {
if(tilesWith(goal).length === tilesWith(goal, box).length){
if(level < levels.length-1){
level++;
setMap(levels[level]);
}else{
addText("You Win!", {color: color`4`});
}
}
});