-
-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathmultigamepad.js
56 lines (46 loc) · 1013 Bytes
/
multigamepad.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
// @ts-check
kaplay();
setGravity(2400);
setBackground(0, 0, 0);
loadSprite("bean", "/sprites/bean.png");
const playerColors = [
rgb(252, 53, 43),
rgb(0, 255, 0),
rgb(43, 71, 252),
rgb(255, 255, 0),
rgb(255, 0, 255),
];
let playerCount = 0;
function addPlayer(gamepad) {
const player = add([
pos(center()),
anchor("center"),
sprite("bean"),
color(playerColors[playerCount]),
area(),
body(),
doubleJump(),
]);
playerCount++;
onUpdate(() => {
const leftStick = gamepad.getStick("left");
if (gamepad.isPressed("south")) {
player.doubleJump();
}
if (leftStick.x !== 0) {
player.move(leftStick.x * 400, 0);
}
});
}
// platform
add([
pos(0, height()),
anchor("botleft"),
rect(width(), 140),
area(),
body({ isStatic: true }),
]);
// add players on every gamepad connect
onGamepadConnect((gamepad) => {
addPlayer(gamepad);
});