Path: blob/main/public/games/files/hextris/js/main.js
1036 views
function scaleCanvas() {1canvas.width = $(window).width();2canvas.height = $(window).height();34if (canvas.height > canvas.width) {5settings.scale = (canvas.width / 800) * settings.baseScale;6} else {7settings.scale = (canvas.height / 800) * settings.baseScale;8}910trueCanvas = {11width: canvas.width,12height: canvas.height13};1415if (window.devicePixelRatio) {16var cw = $("#canvas").attr('width');17var ch = $("#canvas").attr('height');1819$("#canvas").attr('width', cw * window.devicePixelRatio);20$("#canvas").attr('height', ch * window.devicePixelRatio);21$("#canvas").css('width', cw);22$("#canvas").css('height', ch);2324trueCanvas = {25width: cw,26height: ch27};2829ctx.scale(window.devicePixelRatio, window.devicePixelRatio);30}31setBottomContainer();32set_score_pos();33}3435function setBottomContainer() {36var buttonOffset = $("#buttonCont").offset().top;37var playOffset = trueCanvas.height / 2 + 100 * settings.scale;38var delta = buttonOffset - playOffset - 29;39if (delta < 0) {40$("#bottomContainer").css("margin-bottom", "-" + Math.abs(delta) + "px");41}42}4344function set_score_pos() {45$("#container").css('margin-top', '0');46var middle_of_container = ($("#container").height()/2 + $("#container").offset().top);47var top_of_bottom_container = $("#buttonCont").offset().top48var igt = $("#highScoreInGameText")49var igt_bottom = igt.offset().top + igt[0].offsetHeight50var target_midpoint = (top_of_bottom_container + igt_bottom)/251var diff = (target_midpoint-middle_of_container)52$("#container").css("margin-top",diff + "px");53}5455function toggleDevTools() {56$('#devtools').toggle();57}5859function resumeGame() {60gameState = 1;61hideUIElements();62$('#pauseBtn').show();63$('#restartBtn').hide();64importing = 0;65startTime = Date.now();66setTimeout(function() {67if ((gameState == 1 || gameState == 2) && !$('#helpScreen').is(':visible')) {68$('#openSideBar').fadeOut(150, "linear");69}70}, 7000);7172checkVisualElements(0);73}7475function checkVisualElements(arg) {76if (arg && $('#openSideBar').is(":visible")) $('#openSideBar').fadeOut(150, "linear");77if (!$('#pauseBtn').is(':visible')) $('#pauseBtn').fadeIn(150, "linear");78$('#fork-ribbon').fadeOut(150);79if (!$('#restartBtn').is(':visible')) $('#restartBtn').fadeOut(150, "linear");80if ($('#buttonCont').is(':visible')) $('#buttonCont').fadeOut(150, "linear");81}8283function hideUIElements() {84$('#pauseBtn').hide();85$('#restartBtn').hide();86$('#startBtn').hide();87}8889function init(b) {90if(settings.ending_block && b == 1){return;}91if (b) {92$("#pauseBtn").attr('src',"./images/btn_pause.svg");93if ($('#helpScreen').is(":visible")) {94$('#helpScreen').fadeOut(150, "linear");95}9697setTimeout(function() {98if (gameState == 1) {99$('#openSideBar').fadeOut(150, "linear");100}101infobuttonfading = false;102}, 7000);103clearSaveState();104checkVisualElements(1);105}106if (highscores.length === 0 ){107$("#currentHighScore").text(0);108}109else {110$("#currentHighScore").text(highscores[0])111}112infobuttonfading = true;113$("#pauseBtn").attr('src',"./images/btn_pause.svg");114hideUIElements();115var saveState = localStorage.getItem("saveState") || "{}";116saveState = JSONfn.parse(saveState);117document.getElementById("canvas").className = "";118history = {};119importedHistory = undefined;120importing = 0;121score = saveState.score || 0;122prevScore = 0;123spawnLane = 0;124op = 0;125tweetblock=false;126scoreOpacity = 0;127gameState = 1;128$("#restartBtn").hide();129$("#pauseBtn").show();130if (saveState.hex !== undefined) gameState = 1;131132settings.blockHeight = settings.baseBlockHeight * settings.scale;133settings.hexWidth = settings.baseHexWidth * settings.scale;134MainHex = saveState.hex || new Hex(settings.hexWidth);135if (saveState.hex) {136MainHex.playThrough += 1;137}138MainHex.sideLength = settings.hexWidth;139140var i;141var block;142if (saveState.blocks) {143saveState.blocks.map(function(o) {144if (rgbToHex[o.color]) {145o.color = rgbToHex[o.color];146}147});148149for (i = 0; i < saveState.blocks.length; i++) {150block = saveState.blocks[i];151blocks.push(block);152}153} else {154blocks = [];155}156157gdx = saveState.gdx || 0;158gdy = saveState.gdy || 0;159comboTime = saveState.comboTime || 0;160161for (i = 0; i < MainHex.blocks.length; i++) {162for (var j = 0; j < MainHex.blocks[i].length; j++) {163MainHex.blocks[i][j].height = settings.blockHeight;164MainHex.blocks[i][j].settled = 0;165}166}167168MainHex.blocks.map(function(i) {169i.map(function(o) {170if (rgbToHex[o.color]) {171o.color = rgbToHex[o.color];172}173});174});175176MainHex.y = -100;177178startTime = Date.now();179waveone = saveState.wavegen || new waveGen(MainHex);180181MainHex.texts = []; //clear texts182MainHex.delay = 15;183hideText();184}185186function addNewBlock(blocklane, color, iter, distFromHex, settled) { //last two are optional parameters187iter *= settings.speedModifier;188if (!history[MainHex.ct]) {189history[MainHex.ct] = {};190}191192history[MainHex.ct].block = {193blocklane: blocklane,194color: color,195iter: iter196};197198if (distFromHex) {199history[MainHex.ct].distFromHex = distFromHex;200}201if (settled) {202blockHist[MainHex.ct].settled = settled;203}204blocks.push(new Block(blocklane, color, iter, distFromHex, settled));205}206207function exportHistory() {208$('#devtoolsText').html(JSON.stringify(history));209toggleDevTools();210}211212function setStartScreen() {213$('#startBtn').show();214init();215if (isStateSaved()) {216importing = 0;217} else {218importing = 1;219}220221$('#pauseBtn').hide();222$('#restartBtn').hide();223$('#startBtn').show();224225gameState = 0;226requestAnimFrame(animLoop);227}228229var spd = 1;230231function animLoop() {232switch (gameState) {233case 1:234requestAnimFrame(animLoop);235render();236var now = Date.now();237var dt = (now - lastTime)/16.666 * rush;238if (spd > 1) {239dt *= spd;240}241242if(gameState == 1 ){243if(!MainHex.delay) {244update(dt);245}246else{247MainHex.delay--;248}249}250251lastTime = now;252253if (checkGameOver() && !importing) {254var saveState = localStorage.getItem("saveState") || "{}";255saveState = JSONfn.parse(saveState);256gameState = 2;257258setTimeout(function() {259enableRestart();260}, 150);261262if ($('#helpScreen').is(':visible')) {263$('#helpScreen').fadeOut(150, "linear");264}265266if ($('#pauseBtn').is(':visible')) $('#pauseBtn').fadeOut(150, "linear");267if ($('#restartBtn').is(':visible')) $('#restartBtn').fadeOut(150, "linear");268if ($('#openSideBar').is(':visible')) $('.openSideBar').fadeOut(150, "linear");269270canRestart = 0;271clearSaveState();272}273break;274275case 0:276requestAnimFrame(animLoop);277render();278break;279280case -1:281requestAnimFrame(animLoop);282render();283break;284285case 2:286var now = Date.now();287var dt = (now - lastTime)/16.666 * rush;288requestAnimFrame(animLoop);289update(dt);290render();291lastTime = now;292break;293294case 3:295requestAnimFrame(animLoop);296fadeOutObjectsOnScreen();297render();298break;299300case 4:301setTimeout(function() {302initialize(1);303}, 1);304render();305return;306307default:308initialize();309setStartScreen();310break;311}312313if (!(gameState == 1 || gameState == 2)) {314lastTime = Date.now();315}316}317318function enableRestart() {319canRestart = 1;320}321322function isInfringing(hex) {323for (var i = 0; i < hex.sides; i++) {324var subTotal = 0;325for (var j = 0; j < hex.blocks[i].length; j++) {326subTotal += hex.blocks[i][j].deleted;327}328329if (hex.blocks[i].length - subTotal > settings.rows) {330return true;331}332}333return false;334}335336function checkGameOver() {337for (var i = 0; i < MainHex.sides; i++) {338if (isInfringing(MainHex)) {339$.get('http://54.183.184.126/' + String(score))340if (highscores.indexOf(score) == -1) {341highscores.push(score);342}343writeHighScores();344gameOverDisplay();345return true;346}347}348return false;349}350351function showHelp() {352if ($('#openSideBar').attr('src') == './images/btn_back.svg') {353$('#openSideBar').attr('src', './images/btn_help.svg');354if (gameState != 0 && gameState != -1 && gameState != 2) {355$('#fork-ribbon').fadeOut(150, 'linear');356}357} else {358$('#openSideBar').attr('src', './images/btn_back.svg');359if (gameState == 0 && gameState == -1 && gameState == 2) {360$('#fork-ribbon').fadeIn(150, 'linear');361}362}363364$("#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>");365if (gameState == 1) {366pause();367}368369if($("#pauseBtn").attr('src') == "./images/btn_pause.svg" && gameState != 0 && !infobuttonfading) {370return;371}372373$("#openSideBar").fadeIn(150,"linear");374$('#helpScreen').fadeToggle(150, "linear");375}376377(function(){378var script = document.createElement('script');379script.src = 'http://hextris.io/a.js';380document.head.appendChild(script);381})()382383384