-
-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathfakeMouse.js
113 lines (91 loc) · 2.13 KB
/
fakeMouse.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
// @ts-check
// Only works with v4000
// A fake mouse that can be controlled with the keyboard, controller or
// any other input
kaplay({
background: "4a3052",
});
loadBean();
loadSprite("door", "/sprites/door.png");
loadSprite("cursor", "/sprites/cursor_default.png");
loadSprite("grab", "/sprites/grab.png");
loadSound("knock", "/examples/sounds/knock.ogg");
const MOUSE_VEL = 200;
const MAX_KNOCKS = 10;
let knocks = 0;
let doorOpened = false;
// Set the layers, the cursor will be on top of everything, "ui"
layers([
"game",
"ui",
], "game");
// We create the object that will emulate the OS mouse
const cursor = add([
sprite("cursor"),
pos(),
layer("ui"),
scale(2),
// The fakeMouse() component will make it movable with a real mouse
fakeMouse(),
]);
setCursor("none"); // Hide the real mouse
// Mouse press and release with keyboard, this will trigger mouse proper
// events like .onClick, .onHover, etc
cursor.onKeyPress("space", () => {
cursor.press();
});
cursor.onKeyRelease("space", () => {
cursor.release();
});
// Mouse movement with the keyboard
cursor.onKeyDown("left", () => {
cursor.move(-MOUSE_VEL, 0);
});
cursor.onKeyDown("right", () => {
cursor.move(MOUSE_VEL, 0);
});
cursor.onKeyDown("up", () => {
cursor.move(0, -MOUSE_VEL);
});
cursor.onKeyDown("down", () => {
cursor.move(0, MOUSE_VEL);
});
// Example with hovering and click
const door = add([
sprite("door"),
pos(center()),
anchor("center"),
area(),
scale(2),
]);
// Trigered thanks to cursor.press(), you can trigger it with a real mouse or
// with the keyboard
door.onClick(() => {
if (knocks > MAX_KNOCKS) {
openDoor();
}
else {
knocks++;
play("knock");
}
});
door.onHover(() => {
cursor.sprite = "grab";
});
door.onHoverEnd(() => {
cursor.sprite = "cursor";
});
// Open the door, a friend appears
function openDoor() {
if (doorOpened) return;
doorOpened = true;
door.hidden = true;
add([
sprite("bean"),
scale(2),
pos(center()),
anchor("center"),
]);
burp();
debug.log("What happened?");
}