Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
AroriaNetwork
GitHub Repository: AroriaNetwork/3kho-backup
Path: blob/main/projects/flappy-2048/js/keyboard_input_manager.js
1834 views
1
function KeyboardInputManager() {
2
this.events = {};
3
4
this.listen();
5
}
6
7
KeyboardInputManager.prototype.on = function (event, callback) {
8
if (!this.events[event]) {
9
this.events[event] = [];
10
}
11
this.events[event].push(callback);
12
};
13
14
KeyboardInputManager.prototype.emit = function (event, data) {
15
var callbacks = this.events[event];
16
if (callbacks) {
17
callbacks.forEach(function (callback) {
18
callback(data);
19
});
20
}
21
};
22
23
KeyboardInputManager.prototype.listen = function () {
24
var self = this;
25
26
function dojump(event) {
27
var modifiers = event.altKey || event.ctrlKey || event.metaKey ||
28
event.shiftKey;
29
30
if (!modifiers) {
31
if (event.which >= 8 && event.which < 48) event.preventDefault();
32
self.emit("jump");
33
}
34
}
35
36
function dojump2(event) {
37
event.preventDefault();
38
self.emit("jump");
39
}
40
41
document.addEventListener("keydown", dojump);
42
var gameContainer = document.querySelector(".game-container");
43
gameContainer.addEventListener("click", dojump2);
44
gameContainer.addEventListener("touchstart", dojump2);
45
};
46
47
KeyboardInputManager.prototype.restart = function (event) {
48
event.preventDefault();
49
this.emit("restart");
50
};
51
52
KeyboardInputManager.prototype.keepPlaying = function (event) {
53
event.preventDefault();
54
this.emit("keepPlaying");
55
};
56
57