Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
mamayaya1
GitHub Repository: mamayaya1/game
Path: blob/main/projects/doge2048/js/keyboard_input_manager.js
4626 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
var map = {
27
38: 0, // Up
28
39: 1, // Right
29
40: 2, // Down
30
37: 3, // Left
31
75: 0, // vim keybindings
32
76: 1,
33
74: 2,
34
72: 3,
35
87: 0, // W
36
68: 1, // D
37
83: 2, // S
38
65: 3 // A
39
};
40
41
document.addEventListener("keydown", function (event) {
42
var modifiers = event.altKey || event.ctrlKey || event.metaKey ||
43
event.shiftKey;
44
var mapped = map[event.which];
45
46
if (!modifiers) {
47
if (mapped !== undefined) {
48
event.preventDefault();
49
self.emit("move", mapped);
50
}
51
52
if (event.which === 32) self.restart.bind(self)(event);
53
}
54
});
55
56
var retry = document.querySelector(".retry-button");
57
retry.addEventListener("click", this.restart.bind(this));
58
retry.addEventListener("touchend", this.restart.bind(this));
59
60
var keepPlaying = document.querySelector(".keep-playing-button");
61
keepPlaying.addEventListener("click", this.keepPlaying.bind(this));
62
keepPlaying.addEventListener("touchend", this.keepPlaying.bind(this));
63
64
var showInfo = document.querySelector(".info-container");
65
showInfo.addEventListener("click", this.showInfo.bind(this));
66
showInfo.addEventListener("touchend", this.showInfo.bind(this));
67
68
// var hideInfo = document.querySelector(".hide-info");
69
// hideInfo.addEventListener("click", this.hideInfo.bind(this));
70
// hideInfo.addEventListener("touchend", this.hideInfo.bind(this));
71
72
73
// Listen to swipe events
74
var touchStartClientX, touchStartClientY;
75
var gameContainer = document.getElementsByClassName("game-container")[0];
76
77
gameContainer.addEventListener("touchstart", function (event) {
78
if (event.touches.length > 1) return;
79
80
touchStartClientX = event.touches[0].clientX;
81
touchStartClientY = event.touches[0].clientY;
82
event.preventDefault();
83
});
84
85
gameContainer.addEventListener("touchmove", function (event) {
86
event.preventDefault();
87
});
88
89
gameContainer.addEventListener("touchend", function (event) {
90
if (event.touches.length > 0) return;
91
var dx = event.changedTouches[0].clientX - touchStartClientX;
92
var absDx = Math.abs(dx);
93
var dy = event.changedTouches[0].clientY - touchStartClientY;
94
var absDy = Math.abs(dy);
95
if (Math.max(absDx, absDy) > 10) {
96
// (right : left) : (down : up)
97
self.emit("move", absDx > absDy ? (dx > 0 ? 1 : 3) : (dy > 0 ? 2 : 0));
98
}
99
});
100
};
101
KeyboardInputManager.prototype.restart = function (event) {
102
event.preventDefault();
103
this.emit("restart");
104
};
105
KeyboardInputManager.prototype.keepPlaying = function (event) {
106
event.preventDefault();
107
this.emit("keepPlaying");
108
};
109
KeyboardInputManager.prototype.showInfo = function (event) {
110
event.preventDefault();
111
this.emit("showInfo");
112
};
113
KeyboardInputManager.prototype.hideInfo = function (event) {
114
event.preventDefault();
115
this.emit("hideInfo");
116
};
117
118