hextris/js/initialization.js

293 lines
6.7 KiB
JavaScript
Raw Normal View History

2014-08-23 02:10:55 +00:00
$(document).ready(function() {
2015-05-24 14:54:12 +00:00
initialize();
2014-08-23 02:10:55 +00:00
});
function initialize(a) {
2015-05-24 14:54:12 +00:00
window.rush = 1;
window.lastTime = Date.now();
window.iframHasLoaded = false;
window.colors = ["#e74c3c", "#f1c40f", "#3498db", "#2ecc71"];
window.hexColorsToTintedColors = {
"#e74c3c": "rgb(241,163,155)",
"#f1c40f": "rgb(246,223,133)",
"#3498db": "rgb(151,201,235)",
"#2ecc71": "rgb(150,227,183)"
};
window.rgbToHex = {
"rgb(231,76,60)": "#e74c3c",
"rgb(241,196,15)": "#f1c40f",
"rgb(52,152,219)": "#3498db",
"rgb(46,204,113)": "#2ecc71"
};
window.rgbColorsToTintedColors = {
"rgb(231,76,60)": "rgb(241,163,155)",
"rgb(241,196,15)": "rgb(246,223,133)",
"rgb(52,152,219)": "rgb(151,201,235)",
"rgb(46,204,113)": "rgb(150,227,183)"
};
window.hexagonBackgroundColor = 'rgb(236, 240, 241)';
window.hexagonBackgroundColorClear = 'rgba(236, 240, 241, 0.5)';
window.centerBlue = 'rgb(44,62,80)';
window.angularVelocityConst = 4;
window.scoreOpacity = 0;
window.textOpacity = 0;
window.prevGameState = undefined;
window.op = 0;
window.saveState = localStorage.getItem("saveState") || "{}";
if (saveState !== "{}") {
op = 1;
}
window.textShown = false;
window.requestAnimFrame = (function() {
return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || function(callback) {
window.setTimeout(callback, 1000 / framerate);
};
})();
$('#clickToExit').bind('click', toggleDevTools);
window.settings;
if (/Android|webOS|iPhone|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {
$('.rrssb-email').remove();
2015-05-24 14:54:12 +00:00
settings = {
os: "other",
platform: "mobile",
startDist: 227,
creationDt: 60,
baseScale: 1.4,
scale: 1,
prevScale: 1,
baseHexWidth: 87,
hexWidth: 87,
baseBlockHeight: 20,
blockHeight: 20,
rows: 7,
speedModifier: 0.73,
speedUpKeyHeld: false,
2015-05-24 14:54:12 +00:00
creationSpeedModifier: 0.73,
2015-09-26 00:33:36 +00:00
comboTime: 310
2015-05-24 14:54:12 +00:00
};
} else {
settings = {
os: "other",
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,
speedUpKeyHeld: false,
2015-05-24 14:54:12 +00:00
creationSpeedModifier: 0.65,
2015-09-26 00:33:36 +00:00
comboTime: 310
2015-05-24 14:54:12 +00:00
};
}
if(/Android/i.test(navigator.userAgent)) {
settings.os = "android";
}
if(navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPad/i) || navigator.userAgent.match(/iPod/i)){
settings.os="ios";
}
window.canvas = document.getElementById('canvas');
window.ctx = canvas.getContext('2d');
window.trueCanvas = {
width: canvas.width,
height: canvas.height
};
scaleCanvas();
window.framerate = 60;
window.history = {};
window.score = 0;
window.scoreAdditionCoeff = 1;
window.prevScore = 0;
window.numHighScores = 3;
highscores = [];
if (localStorage.getItem('highscores')) {
try {
highscores = JSON.parse(localStorage.getItem('highscores'));
} catch (e) {
highscores = [];
}
}
window.blocks = [];
window.MainHex;
window.gdx = 0;
window.gdy = 0;
window.devMode = 0;
window.lastGen = undefined;
window.prevTimeScored = undefined;
window.nextGen = undefined;
window.spawnLane = 0;
window.importing = 0;
window.importedHistory = undefined;
window.startTime = undefined;
window.gameState;
setStartScreen();
if (a != 1) {
window.canRestart = 1;
window.onblur = function(e) {
if (gameState == 1) {
pause();
}
};
$('#startBtn').off();
if (settings.platform == 'mobile') {
$('#startBtn').on('touchstart', startBtnHandler);
} else {
$('#startBtn').on('mousedown', startBtnHandler);
}
document.addEventListener('touchmove', function(e) {
e.preventDefault();
}, false);
$(window).resize(scaleCanvas);
$(window).unload(function() {
if (gameState == 1 || gameState == -1 || gameState === 0) localStorage.setItem("saveState", exportSaveState());
else localStorage.setItem("saveState", "{}");
});
addKeyListeners();
(function(i, s, o, g, r, a, m) {
i['GoogleAnalyticsObject'] = r;
i[r] = i[r] || function() {
(i[r].q = i[r].q || []).push(arguments)
}, i[r].l = 1 * new Date();
a = s.createElement(o), m = s.getElementsByTagName(o)[0];
a.async = 1;
a.src = g;
m.parentNode.insertBefore(a, m)
})(window, document, 'script', '//www.google-analytics.com/analytics.js', 'ga');
ga('create', 'UA-51272720-1', 'teamsnowman.github.io');
ga('send', 'pageview');
document.addEventListener("pause", handlePause, false);
document.addEventListener("backbutton", handlePause, false);
document.addEventListener("menubutton", handlePause, false); //menu button on android
setTimeout(function() {
if (settings.platform == "mobile") {
try {
document.body.removeEventListener('touchstart', handleTapBefore, false);
} catch (e) {
}
try {
document.body.removeEventListener('touchstart', handleTap, false);
} catch (e) {
}
document.body.addEventListener('touchstart', handleTapBefore, false);
} else {
try {
document.body.removeEventListener('mousedown', handleClickBefore, false);
} catch (e) {
}
try {
document.body.removeEventListener('mousedown', handleClick, false);
} catch (e) {
}
document.body.addEventListener('mousedown', handleClickBefore, false);
}
}, 1);
}
2014-08-23 02:10:55 +00:00
}
function startBtnHandler() {
2015-05-24 14:54:12 +00:00
setTimeout(function() {
if (settings.platform == "mobile") {
try {
document.body.removeEventListener('touchstart', handleTapBefore, false);
} catch (e) {
2014-08-23 02:10:55 +00:00
2015-05-24 14:54:12 +00:00
}
2014-08-23 02:10:55 +00:00
2015-05-24 14:54:12 +00:00
try {
document.body.removeEventListener('touchstart', handleTap, false);
} catch (e) {
2014-08-23 02:10:55 +00:00
2015-05-24 14:54:12 +00:00
}
2014-08-23 02:10:55 +00:00
2015-05-24 14:54:12 +00:00
document.body.addEventListener('touchstart', handleTap, false);
} else {
try {
document.body.removeEventListener('mousedown', handleClickBefore, false);
} catch (e) {
2014-08-23 02:10:55 +00:00
2015-05-24 14:54:12 +00:00
}
2014-08-23 02:10:55 +00:00
2015-05-24 14:54:12 +00:00
try {
document.body.removeEventListener('mousedown', handleClick, false);
} catch (e) {
2014-08-23 02:10:55 +00:00
2015-05-24 14:54:12 +00:00
}
2014-08-23 02:10:55 +00:00
2015-05-24 14:54:12 +00:00
document.body.addEventListener('mousedown', handleClick, false);
}
}, 5);
2014-08-23 02:10:55 +00:00
2015-05-24 14:54:12 +00:00
if (!canRestart) return false;
2014-08-23 02:10:55 +00:00
2015-05-24 16:35:09 +00:00
if ($('#openSideBar').is(':visible')) {
$('#openSideBar').fadeOut(150, "linear");
2015-05-24 14:54:12 +00:00
}
2014-08-23 02:10:55 +00:00
2015-05-24 14:54:12 +00:00
if (importing == 1) {
init(1);
2015-09-26 05:46:05 +00:00
checkVisualElements(0);
2015-05-24 14:54:12 +00:00
} else {
resumeGame();
}
2014-08-23 02:10:55 +00:00
}
function handlePause() {
2015-05-24 14:54:12 +00:00
if (gameState == 1 || gameState == 2) {
pause();
}
2014-08-23 02:10:55 +00:00
}
function handleTap(e) {
2015-05-24 14:54:12 +00:00
handleClickTap(e.changedTouches[0].clientX, e.changedTouches[0].clientY);
2014-08-23 02:10:55 +00:00
}
function handleClick(e) {
2015-05-24 14:54:12 +00:00
handleClickTap(e.clientX, e.clientY);
2014-08-23 02:10:55 +00:00
}
function handleTapBefore(e) {
2015-05-24 14:54:12 +00:00
var x = e.changedTouches[0].clientX;
var y = e.changedTouches[0].clientY;
2014-08-23 02:10:55 +00:00
if (x < 120 && y < 83 && $('.helpText').is(':visible')) {
2015-05-24 14:54:12 +00:00
showHelp();
return;
}
2014-08-23 02:10:55 +00:00
}
function handleClickBefore(e) {
2015-05-24 14:54:12 +00:00
var x = e.clientX;
var y = e.clientY;
2014-08-23 02:10:55 +00:00
if (x < 120 && y < 83 && $('.helpText').is(':visible')) {
2015-05-24 14:54:12 +00:00
showHelp();
return;
}
2014-08-23 02:10:55 +00:00
}