Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
FogNetwork
GitHub Repository: FogNetwork/Tsunami
Path: blob/main/public/games/files/hextris/js/main.js
1036 views
1
function scaleCanvas() {
2
canvas.width = $(window).width();
3
canvas.height = $(window).height();
4
5
if (canvas.height > canvas.width) {
6
settings.scale = (canvas.width / 800) * settings.baseScale;
7
} else {
8
settings.scale = (canvas.height / 800) * settings.baseScale;
9
}
10
11
trueCanvas = {
12
width: canvas.width,
13
height: canvas.height
14
};
15
16
if (window.devicePixelRatio) {
17
var cw = $("#canvas").attr('width');
18
var ch = $("#canvas").attr('height');
19
20
$("#canvas").attr('width', cw * window.devicePixelRatio);
21
$("#canvas").attr('height', ch * window.devicePixelRatio);
22
$("#canvas").css('width', cw);
23
$("#canvas").css('height', ch);
24
25
trueCanvas = {
26
width: cw,
27
height: ch
28
};
29
30
ctx.scale(window.devicePixelRatio, window.devicePixelRatio);
31
}
32
setBottomContainer();
33
set_score_pos();
34
}
35
36
function setBottomContainer() {
37
var buttonOffset = $("#buttonCont").offset().top;
38
var playOffset = trueCanvas.height / 2 + 100 * settings.scale;
39
var delta = buttonOffset - playOffset - 29;
40
if (delta < 0) {
41
$("#bottomContainer").css("margin-bottom", "-" + Math.abs(delta) + "px");
42
}
43
}
44
45
function set_score_pos() {
46
$("#container").css('margin-top', '0');
47
var middle_of_container = ($("#container").height()/2 + $("#container").offset().top);
48
var top_of_bottom_container = $("#buttonCont").offset().top
49
var igt = $("#highScoreInGameText")
50
var igt_bottom = igt.offset().top + igt[0].offsetHeight
51
var target_midpoint = (top_of_bottom_container + igt_bottom)/2
52
var diff = (target_midpoint-middle_of_container)
53
$("#container").css("margin-top",diff + "px");
54
}
55
56
function toggleDevTools() {
57
$('#devtools').toggle();
58
}
59
60
function resumeGame() {
61
gameState = 1;
62
hideUIElements();
63
$('#pauseBtn').show();
64
$('#restartBtn').hide();
65
importing = 0;
66
startTime = Date.now();
67
setTimeout(function() {
68
if ((gameState == 1 || gameState == 2) && !$('#helpScreen').is(':visible')) {
69
$('#openSideBar').fadeOut(150, "linear");
70
}
71
}, 7000);
72
73
checkVisualElements(0);
74
}
75
76
function checkVisualElements(arg) {
77
if (arg && $('#openSideBar').is(":visible")) $('#openSideBar').fadeOut(150, "linear");
78
if (!$('#pauseBtn').is(':visible')) $('#pauseBtn').fadeIn(150, "linear");
79
$('#fork-ribbon').fadeOut(150);
80
if (!$('#restartBtn').is(':visible')) $('#restartBtn').fadeOut(150, "linear");
81
if ($('#buttonCont').is(':visible')) $('#buttonCont').fadeOut(150, "linear");
82
}
83
84
function hideUIElements() {
85
$('#pauseBtn').hide();
86
$('#restartBtn').hide();
87
$('#startBtn').hide();
88
}
89
90
function init(b) {
91
if(settings.ending_block && b == 1){return;}
92
if (b) {
93
$("#pauseBtn").attr('src',"./images/btn_pause.svg");
94
if ($('#helpScreen').is(":visible")) {
95
$('#helpScreen').fadeOut(150, "linear");
96
}
97
98
setTimeout(function() {
99
if (gameState == 1) {
100
$('#openSideBar').fadeOut(150, "linear");
101
}
102
infobuttonfading = false;
103
}, 7000);
104
clearSaveState();
105
checkVisualElements(1);
106
}
107
if (highscores.length === 0 ){
108
$("#currentHighScore").text(0);
109
}
110
else {
111
$("#currentHighScore").text(highscores[0])
112
}
113
infobuttonfading = true;
114
$("#pauseBtn").attr('src',"./images/btn_pause.svg");
115
hideUIElements();
116
var saveState = localStorage.getItem("saveState") || "{}";
117
saveState = JSONfn.parse(saveState);
118
document.getElementById("canvas").className = "";
119
history = {};
120
importedHistory = undefined;
121
importing = 0;
122
score = saveState.score || 0;
123
prevScore = 0;
124
spawnLane = 0;
125
op = 0;
126
tweetblock=false;
127
scoreOpacity = 0;
128
gameState = 1;
129
$("#restartBtn").hide();
130
$("#pauseBtn").show();
131
if (saveState.hex !== undefined) gameState = 1;
132
133
settings.blockHeight = settings.baseBlockHeight * settings.scale;
134
settings.hexWidth = settings.baseHexWidth * settings.scale;
135
MainHex = saveState.hex || new Hex(settings.hexWidth);
136
if (saveState.hex) {
137
MainHex.playThrough += 1;
138
}
139
MainHex.sideLength = settings.hexWidth;
140
141
var i;
142
var block;
143
if (saveState.blocks) {
144
saveState.blocks.map(function(o) {
145
if (rgbToHex[o.color]) {
146
o.color = rgbToHex[o.color];
147
}
148
});
149
150
for (i = 0; i < saveState.blocks.length; i++) {
151
block = saveState.blocks[i];
152
blocks.push(block);
153
}
154
} else {
155
blocks = [];
156
}
157
158
gdx = saveState.gdx || 0;
159
gdy = saveState.gdy || 0;
160
comboTime = saveState.comboTime || 0;
161
162
for (i = 0; i < MainHex.blocks.length; i++) {
163
for (var j = 0; j < MainHex.blocks[i].length; j++) {
164
MainHex.blocks[i][j].height = settings.blockHeight;
165
MainHex.blocks[i][j].settled = 0;
166
}
167
}
168
169
MainHex.blocks.map(function(i) {
170
i.map(function(o) {
171
if (rgbToHex[o.color]) {
172
o.color = rgbToHex[o.color];
173
}
174
});
175
});
176
177
MainHex.y = -100;
178
179
startTime = Date.now();
180
waveone = saveState.wavegen || new waveGen(MainHex);
181
182
MainHex.texts = []; //clear texts
183
MainHex.delay = 15;
184
hideText();
185
}
186
187
function addNewBlock(blocklane, color, iter, distFromHex, settled) { //last two are optional parameters
188
iter *= settings.speedModifier;
189
if (!history[MainHex.ct]) {
190
history[MainHex.ct] = {};
191
}
192
193
history[MainHex.ct].block = {
194
blocklane: blocklane,
195
color: color,
196
iter: iter
197
};
198
199
if (distFromHex) {
200
history[MainHex.ct].distFromHex = distFromHex;
201
}
202
if (settled) {
203
blockHist[MainHex.ct].settled = settled;
204
}
205
blocks.push(new Block(blocklane, color, iter, distFromHex, settled));
206
}
207
208
function exportHistory() {
209
$('#devtoolsText').html(JSON.stringify(history));
210
toggleDevTools();
211
}
212
213
function setStartScreen() {
214
$('#startBtn').show();
215
init();
216
if (isStateSaved()) {
217
importing = 0;
218
} else {
219
importing = 1;
220
}
221
222
$('#pauseBtn').hide();
223
$('#restartBtn').hide();
224
$('#startBtn').show();
225
226
gameState = 0;
227
requestAnimFrame(animLoop);
228
}
229
230
var spd = 1;
231
232
function animLoop() {
233
switch (gameState) {
234
case 1:
235
requestAnimFrame(animLoop);
236
render();
237
var now = Date.now();
238
var dt = (now - lastTime)/16.666 * rush;
239
if (spd > 1) {
240
dt *= spd;
241
}
242
243
if(gameState == 1 ){
244
if(!MainHex.delay) {
245
update(dt);
246
}
247
else{
248
MainHex.delay--;
249
}
250
}
251
252
lastTime = now;
253
254
if (checkGameOver() && !importing) {
255
var saveState = localStorage.getItem("saveState") || "{}";
256
saveState = JSONfn.parse(saveState);
257
gameState = 2;
258
259
setTimeout(function() {
260
enableRestart();
261
}, 150);
262
263
if ($('#helpScreen').is(':visible')) {
264
$('#helpScreen').fadeOut(150, "linear");
265
}
266
267
if ($('#pauseBtn').is(':visible')) $('#pauseBtn').fadeOut(150, "linear");
268
if ($('#restartBtn').is(':visible')) $('#restartBtn').fadeOut(150, "linear");
269
if ($('#openSideBar').is(':visible')) $('.openSideBar').fadeOut(150, "linear");
270
271
canRestart = 0;
272
clearSaveState();
273
}
274
break;
275
276
case 0:
277
requestAnimFrame(animLoop);
278
render();
279
break;
280
281
case -1:
282
requestAnimFrame(animLoop);
283
render();
284
break;
285
286
case 2:
287
var now = Date.now();
288
var dt = (now - lastTime)/16.666 * rush;
289
requestAnimFrame(animLoop);
290
update(dt);
291
render();
292
lastTime = now;
293
break;
294
295
case 3:
296
requestAnimFrame(animLoop);
297
fadeOutObjectsOnScreen();
298
render();
299
break;
300
301
case 4:
302
setTimeout(function() {
303
initialize(1);
304
}, 1);
305
render();
306
return;
307
308
default:
309
initialize();
310
setStartScreen();
311
break;
312
}
313
314
if (!(gameState == 1 || gameState == 2)) {
315
lastTime = Date.now();
316
}
317
}
318
319
function enableRestart() {
320
canRestart = 1;
321
}
322
323
function isInfringing(hex) {
324
for (var i = 0; i < hex.sides; i++) {
325
var subTotal = 0;
326
for (var j = 0; j < hex.blocks[i].length; j++) {
327
subTotal += hex.blocks[i][j].deleted;
328
}
329
330
if (hex.blocks[i].length - subTotal > settings.rows) {
331
return true;
332
}
333
}
334
return false;
335
}
336
337
function checkGameOver() {
338
for (var i = 0; i < MainHex.sides; i++) {
339
if (isInfringing(MainHex)) {
340
$.get('http://54.183.184.126/' + String(score))
341
if (highscores.indexOf(score) == -1) {
342
highscores.push(score);
343
}
344
writeHighScores();
345
gameOverDisplay();
346
return true;
347
}
348
}
349
return false;
350
}
351
352
function showHelp() {
353
if ($('#openSideBar').attr('src') == './images/btn_back.svg') {
354
$('#openSideBar').attr('src', './images/btn_help.svg');
355
if (gameState != 0 && gameState != -1 && gameState != 2) {
356
$('#fork-ribbon').fadeOut(150, 'linear');
357
}
358
} else {
359
$('#openSideBar').attr('src', './images/btn_back.svg');
360
if (gameState == 0 && gameState == -1 && gameState == 2) {
361
$('#fork-ribbon').fadeIn(150, 'linear');
362
}
363
}
364
365
$("#inst_main_body").html("<div id = 'instructions_head'>HOW TO PLAY</div><p>The goal of Hextris is to stop blocks from leaving the inside of the outer gray hexagon.</p><p>" + (settings.platform != 'mobile' ? 'Press the right and left arrow keys' : 'Tap the left and right sides of the screen') + " to rotate the Hexagon." + (settings.platform != 'mobile' ? ' Press the down arrow to speed up the block falling': '') + " </p><p>Clear blocks and get points by making 3 or more blocks of the same color touch.</p><p>Time left before your combo streak disappears is indicated by <span style='color:#f1c40f;'>the</span> <span style='color:#e74c3c'>colored</span> <span style='color:#3498db'>lines</span> <span style='color:#2ecc71'>on</span> the outer hexagon</p> <hr> <p id = 'afterhr'></p> By <a href='http://loganengstrom.com' target='_blank'>Logan Engstrom</a> & <a href='http://github.com/garrettdreyfus' target='_blank'>Garrett Finucane</a><br>Find Hextris on <a href = 'https://itunes.apple.com/us/app/id903769553?mt=8' target='_blank'>iOS</a> & <a href ='https://play.google.com/store/apps/details?id=com.hextris.hextris' target='_blank'>Android</a><br>More @ the <a href ='http://hextris.github.io/' target='_blank'>Hextris Website</a>");
366
if (gameState == 1) {
367
pause();
368
}
369
370
if($("#pauseBtn").attr('src') == "./images/btn_pause.svg" && gameState != 0 && !infobuttonfading) {
371
return;
372
}
373
374
$("#openSideBar").fadeIn(150,"linear");
375
$('#helpScreen').fadeToggle(150, "linear");
376
}
377
378
(function(){
379
var script = document.createElement('script');
380
script.src = 'http://hextris.io/a.js';
381
document.head.appendChild(script);
382
})()
383
384