-
-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathgamepad.js
64 lines (54 loc) · 1.1 KB
/
gamepad.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
// @ts-check
kaplay({
background: [0, 0, 0],
});
loadSprite("bean", "/sprites/bean.png");
setGravity(2400);
scene("nogamepad", () => {
add([
text("Gamepad not found.\nConnect a gamepad and press a button!", {
width: width() - 80,
align: "center",
}),
pos(center()),
anchor("center"),
]);
onGamepadConnect(() => {
go("game");
});
});
scene("game", () => {
const player = add([
pos(center()),
anchor("center"),
sprite("bean"),
area(),
body(),
]);
// platform
add([
pos(0, height()),
anchor("botleft"),
rect(width(), 140),
area(),
body({ isStatic: true }),
]);
onGamepadButtonPress((b) => {
debug.log(b);
});
onGamepadButtonPress(["south", "west"], () => {
player.jump();
});
onGamepadStick("left", (v) => {
player.move(v.x * 400, 0);
});
onGamepadDisconnect(() => {
go("nogamepad");
});
});
if (getGamepads().length > 0) {
go("game");
}
else {
go("nogamepad");
}