-
-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathout.js
55 lines (46 loc) · 1.05 KB
/
out.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
// @ts-check
// detect if obj is out of screen
kaplay();
loadSprite("bean", "/sprites/bean.png");
// custom comp
function handleout() {
return {
id: "handleout",
require: ["pos"],
update() {
const spos = this.screenPos();
if (
spos.x < 0
|| spos.x > width()
|| spos.y < 0
|| spos.y > height()
) {
// triggers a custom event when out
this.trigger("out");
}
},
};
}
const SPEED = 640;
function shoot() {
const center = vec2(width() / 2, height() / 2);
const mpos = mousePos();
add([
pos(center),
sprite("bean"),
anchor("center"),
handleout(),
"bean",
{ dir: mpos.sub(center).unit() },
]);
}
onKeyPress("space", shoot);
onClick(shoot);
onUpdate("bean", (m) => {
m.move(m.dir.scale(SPEED));
});
// binds a custom event "out" to tag group "bean"
on("out", "bean", (m) => {
addKaboom(m.pos);
destroy(m);
});