var textShown = false; $(document).ready(function(){ scaleCanvas(); $('#startBtn').on('touchstart mousedown', function(){ if (importing == 1) { init(1); } else { resumeGame(); } setTimeout(function(){ document.body.addEventListener('touchstart', function(e) { handleClickTap(e.changedTouches[0].clientX); }, false); }, 1); }); }); $(window).resize(scaleCanvas); $(window).unload(function(){ localStorage.setItem("saveState", exportSaveState()); }); function scaleCanvas() { canvas.width = $(window).width(); canvas.height = $(window).height(); if (canvas.height > canvas.width) { settings.scale = (canvas.width/800) * settings.baseScale; } else { settings.scale = (canvas.height/800) * settings.baseScale; } trueCanvas = { width:canvas.width, height:canvas.height }; if (window.devicePixelRatio) { //from https://gist.github.com/joubertnel/870190 var cw = $("#canvas").attr('width'); var ch = $("#canvas").attr('height'); $("#canvas").attr('width', cw * window.devicePixelRatio); $("#canvas").attr('height', ch * window.devicePixelRatio); $("#canvas").css('width', cw); $("#canvas").css('height', ch); trueCanvas = { width:cw, height:ch }; ctx.scale(window.devicePixelRatio, window.devicePixelRatio); } } var canvas = document.getElementById('canvas'); var ctx = canvas.getContext('2d'); var count = 0; var trueCanvas = {width:canvas.width,height:canvas.height}; window.requestAnimFrame = (function() { return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || function(callback) { window.setTimeout(callback, 1000 / framerate); }; })(); $('#clickToExit').bind('click', toggleDevTools); function toggleDevTools() { $('#devtools').toggle(); } var settings; if(/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) { settings = { platform:"mobile", startDist:227, creationDt:40, baseScale:1.4, scale:1, prevScale:1, baseHexWidth:87, hexWidth:87, baseBlockHeight:20, blockHeight:20, rows:6, speedModifier:0.7, creationSpeedModifier:0.7, comboTime:240 }; } else { settings = { platform:"nonmobile", baseScale:1, startDist:340, creationDt:9, scale:1, prevScale:1, hexWidth:65, baseHexWidth:87, baseBlockHeight:20, blockHeight:15, rows:8, speedModifier:0.65, creationSpeedModifier:0.55, comboTime:240 }; } var framerate = 60; var history = {}; var score = 0; var isGameOver = 3; var scoreAdditionCoeff = 1; var prevScore = 0; var numHighScores = 3; var highscores = [0, 0, 0]; if(localStorage.getItem('highscores')) highscores = localStorage.getItem('highscores').split(',').map(Number); localStorage.setItem('highscores', highscores); var blocks = []; var MainHex; var gdx = 0; var gdy = 0; var lastGen; var prevTimeScored; var nextGen; var spawnLane = 0; var importing = 0; var importedHistory; var startTime; var gameState; setStartScreen(); function resumeGame() { gameState = 1; hideUIElements(); $('#pauseBtn').show(); $('#restartBtn').show(); importing = 0; startTime = Date.now(); } function hideUIElements() { $('#pauseBtn').hide(); $('#restartBtn').hide(); $('#startBtn').hide(); } function init(b) { if (b) { clearSaveState(); } hideUIElements(); var saveState = localStorage.getItem("saveState") || "{}"; saveState = JSONfn.parse(saveState); document.getElementById("canvas").className = ""; history = {}; importedHistory = undefined; importing = 0; isGameOver = 2; score = saveState.score || 0; prevScore = 0; spawnLane = 0; op = 0; scoreOpacity = 0; gameState = 1; $("#restartBtn").show(); $("#pauseBtn").show(); if(saveState.hex !== undefined) gameState = 1; scaleCanvas(); settings.blockHeight = settings.baseBlockHeight * settings.scale; settings.hexWidth = settings.baseHexWidth * settings.scale; MainHex = saveState.hex || new Hex(settings.hexWidth); if (saveState.hex) { MainHex.playThrough += 1; } MainHex.sideLength = settings.hexWidth; count = 0; var i; var block; if(saveState.blocks) { saveState.blocks.map(function(o){ if (rgbToHex[o.color]) { o.color = rgbToHex[o.color]; } }); for(i=0; isettings.rows){ return true; } } return false; } function checkGameOver() { for (var i = 0; i < MainHex.sides; i++) { if (isInfringing(MainHex)) { updateHighScore(); gameOverDisplay(); return true; } } return false; } window.onblur = function (e) { if (gameState==1) { pause(); } }; function showHelp(){ if(gameState != 0){ pause(); } $('#helpScreen').fadeToggle(150, "linear"); }