Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
FogNetwork
GitHub Repository: FogNetwork/Tsunami
Path: blob/main/public/games/files/hextris/js/input.js
1036 views
1
function addKeyListeners() {
2
keypress.register_combo({
3
keys: "left",
4
on_keydown: function() {
5
if (MainHex && gameState !== 0) {
6
MainHex.rotate(1);
7
}
8
}
9
});
10
11
keypress.register_combo({
12
keys: "right",
13
on_keydown: function() {
14
if (MainHex && gameState !== 0){
15
MainHex.rotate(-1);
16
}
17
}
18
});
19
keypress.register_combo({
20
keys: "down",
21
on_keydown: function() {
22
var tempSpeed = settings.speedModifier;
23
if (MainHex && gameState !== 0){
24
//speed up block temporarily
25
if(settings.speedUpKeyHeld == false){
26
settings.speedUpKeyHeld = true;
27
window.rush *=4;
28
}
29
}
30
//settings.speedModifier = tempSpeed;
31
},
32
on_keyup:function(){
33
if (MainHex && gameState !== 0){
34
//speed up block temporarily
35
36
window.rush /=4;
37
settings.speedUpKeyHeld = false;
38
}
39
}
40
});
41
42
keypress.register_combo({
43
keys: "a",
44
on_keydown: function() {
45
if (MainHex && gameState !== 0) {
46
MainHex.rotate(1);
47
}
48
}
49
});
50
51
keypress.register_combo({
52
keys: "d",
53
on_keydown: function() {
54
if (MainHex && gameState !== 0){
55
MainHex.rotate(-1);
56
}
57
}
58
});
59
60
keypress.register_combo({
61
keys: "s",
62
on_keydown: function() {
63
var tempSpeed = settings.speedModifier;
64
if (MainHex && gameState !== 0){
65
//speed up block temporarily
66
if(settings.speedUpKeyHeld == false){
67
settings.speedUpKeyHeld = true;
68
window.rush *=4;
69
}
70
}
71
//settings.speedModifier = tempSpeed;
72
},
73
on_keyup:function(){
74
if (MainHex && gameState !== 0){
75
//speed up block temporarily
76
77
window.rush /=4;
78
settings.speedUpKeyHeld = false;
79
}
80
}
81
});
82
keypress.register_combo({
83
keys: "p",
84
on_keydown: function(){pause();}
85
});
86
87
keypress.register_combo({
88
keys: "space",
89
on_keydown: function(){pause();}
90
});
91
92
keypress.register_combo({
93
keys: "q",
94
on_keydown: function() {
95
if (devMode) toggleDevTools();
96
}
97
});
98
99
keypress.register_combo({
100
keys: "enter",
101
on_keydown: function() {
102
if (gameState==1 || importing == 1) {
103
init(1);
104
}
105
if (gameState == 2) {
106
init();
107
$("#gameoverscreen").fadeOut();
108
}
109
if (gameState===0) {
110
resumeGame();
111
}
112
}
113
});
114
115
$("#pauseBtn").on('touchstart mousedown', function() {
116
if (gameState != 1 && gameState != -1) {
117
return;
118
}
119
120
if ($('#helpScreen').is(":visible")) {
121
$('#helpScreen').fadeOut(150, "linear");
122
}
123
pause();
124
return false;
125
});
126
127
$("#colorBlindBtn").on('touchstart mousedown', function() {
128
window.colors = ["#8e44ad", "#f1c40f", "#3498db", "#d35400"];
129
130
window.hexColorsToTintedColors = {
131
"#8e44ad": "rgb(229,152,102)",
132
"#f1c40f": "rgb(246,223,133)",
133
"#3498db": "rgb(151,201,235)",
134
"#d35400": "rgb(210,180,222)"
135
};
136
137
window.rgbToHex = {
138
"rgb(142,68,173)": "#8e44ad",
139
"rgb(241,196,15)": "#f1c40f",
140
"rgb(52,152,219)": "#3498db",
141
"rgb(211,84,0)": "#d35400"
142
};
143
144
window.rgbColorsToTintedColors = {
145
"rgb(142,68,173)": "rgb(229,152,102)",
146
"rgb(241,196,15)": "rgb(246,223,133)",
147
"rgb(52,152,219)": "rgb(151,201,235)",
148
"rgb(46,204,113)": "rgb(210,180,222)"
149
};
150
});
151
152
153
if(/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {
154
$("#restart").on('touchstart', function() {
155
init();
156
canRestart = false;
157
$("#gameoverscreen").fadeOut();
158
});
159
160
}
161
else {
162
$("#restart").on('mousedown', function() {
163
init();
164
canRestart = false;
165
$("#gameoverscreen").fadeOut();
166
});
167
168
}
169
if(/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {
170
$("#restartBtn").on('touchstart', function() {
171
init(1);
172
canRestart = false;
173
$("#gameoverscreen").fadeOut();
174
});
175
176
}
177
else {
178
$("#restartBtn").on('mousedown', function() {
179
init(1);
180
canRestart = false;
181
$("#gameoverscreen").fadeOut();
182
});
183
184
185
}
186
187
}
188
function inside (point, vs) {
189
// ray-casting algorithm based on
190
// http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html
191
192
var x = point[0], y = point[1];
193
194
var inside = false;
195
for (var i = 0, j = vs.length - 1; i < vs.length; j = i++) {
196
var xi = vs[i][0], yi = vs[i][1];
197
var xj = vs[j][0], yj = vs[j][1];
198
199
var intersect = ((yi > y) != (yj > y))
200
&& (x < (xj - xi) * (y - yi) / (yj - yi) + xi);
201
if (intersect) inside = !inside;
202
}
203
204
return inside;
205
};
206
207
function handleClickTap(x,y) {
208
if (x < 120 && y < 83 && $('.helpText').is(':visible')) {
209
showHelp();
210
return;
211
}
212
var radius = settings.hexWidth ;
213
var halfRadius = radius/2;
214
var triHeight = radius *(Math.sqrt(3)/2);
215
var Vertexes =[
216
[radius,0],
217
[halfRadius,-triHeight],
218
[-halfRadius,-triHeight],
219
[-radius,0],
220
[-halfRadius,triHeight],
221
[halfRadius,triHeight]];
222
Vertexes = Vertexes.map(function(coord){
223
return [coord[0] + trueCanvas.width/2, coord[1] + trueCanvas.height/2]});
224
225
if (!MainHex || gameState === 0 || gameState==-1) {
226
return;
227
}
228
229
if (x < window.innerWidth/2) {
230
MainHex.rotate(1);
231
}
232
if (x > window.innerWidth/2) {
233
MainHex.rotate(-1);
234
}
235
}
236
237
238