var s_iScaleFactor = 1; var s_bIsIphone = false; var s_oCanvasLeft; var s_oCanvasTop; var s_bFocus = true; var browserName = (function (agent) { switch (true) { case agent.indexOf("edge") > -1: return "MS Edge"; case agent.indexOf("edg/") > -1: return "Edge ( chromium based)"; case agent.indexOf("opr") > -1 && !!window.opr: return "Opera"; case agent.indexOf("chrome") > -1 && !!window.chrome: return "Chrome"; case agent.indexOf("trident") > -1: return "MS IE"; case agent.indexOf("firefox") > -1: return "Mozilla Firefox"; case agent.indexOf("safari") > -1: return "Safari"; default: return "other"; } })(window.navigator.userAgent.toLowerCase()); window.addEventListener('resize', function(event) { sizeHandler(); }, true); function trace(szMsg){ console.log(szMsg); } function isChrome(){ var isChrome = /Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor); return isChrome; } function isIpad() { var isIpad = navigator.userAgent.toLowerCase().indexOf('ipad') !== -1; if (!isIpad && navigator.userAgent.match(/Mac/) && navigator.maxTouchPoints && navigator.maxTouchPoints > 2) { return true; } return isIpad; } function isMobile(){ if(isIpad()){ return true; } if (navigator.userAgent.match(/Android/i) || navigator.userAgent.match(/webOS/i) || navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPad/i) || navigator.userAgent.match(/iPod/i) || navigator.userAgent.match(/BlackBerry/i) || navigator.userAgent.match(/Windows Phone/i)) { //MOBILE return true; }else{ //DESKTOP return false; } }; function isIOS() { var iDevices = [ 'iPad Simulator', 'iPhone Simulator', 'iPod Simulator', 'iPad', 'iPhone', 'iPod' ]; if (navigator.userAgent.toLowerCase().indexOf("iphone") !== -1){ s_bIsIphone = true; return true; } while (iDevices.length) { if (navigator.platform === iDevices.pop()){ return true; } } s_bIsIphone = false; return false; } function getSize(Name) { var size; var name = Name.toLowerCase(); var document = window.document; var documentElement = document.documentElement; if (window["inner" + Name] === undefined) { // IE6 & IE7 don't have window.innerWidth or innerHeight size = documentElement["client" + Name]; } else if (window["inner" + Name] != documentElement["client" + Name]) { // WebKit doesn't include scrollbars while calculating viewport size so we have to get fancy // Insert markup to test if a media query will match document.doumentElement["client" + Name] var bodyElement = document.createElement("body"); bodyElement.id = "vpw-test-b"; bodyElement.style.cssText = "overflow:scroll"; var divElement = document.createElement("div"); divElement.id = "vpw-test-d"; divElement.style.cssText = "position:absolute;top:-1000px"; // Getting specific on the CSS selector so it won't get overridden easily divElement.innerHTML = ""; bodyElement.appendChild(divElement); documentElement.insertBefore(bodyElement, document.head); if (divElement["offset" + Name] == 7) { // Media query matches document.documentElement["client" + Name] size = documentElement["client" + Name]; } else { // Media query didn't match, use window["inner" + Name] size = window["inner" + Name]; } // Cleanup documentElement.removeChild(bodyElement); } else { // Default to use window["inner" + Name] size = window["inner" + Name]; } return size; }; window.addEventListener("orientationchange", onOrientationChange); function onOrientationChange(){ if (window.matchMedia("(orientation: portrait)").matches) { // you're in PORTRAIT mode sizeHandler(); } if (window.matchMedia("(orientation: landscape)").matches) { // you're in LANDSCAPE mode sizeHandler(); } } function getIOSWindowHeight() { // Get zoom level of mobile Safari // Note, that such zoom detection might not work correctly in other browsers // We use width, instead of height, because there are no vertical toolbars :) var zoomLevel = document.documentElement.clientWidth / window.innerWidth; // window.innerHeight returns height of the visible area. // We multiply it by zoom and get out real height. return window.innerHeight * zoomLevel; }; // You can also get height of the toolbars that are currently displayed function getHeightOfIOSToolbars() { var tH = (window.orientation === 0 ? screen.height : screen.width) - getIOSWindowHeight(); return tH > 1 ? tH : 0; }; //THIS FUNCTION MANAGES THE CANVAS SCALING TO FIT PROPORTIONALLY THE GAME TO THE CURRENT DEVICE RESOLUTION function sizeHandler() { window.scrollTo(0, 1); if (!document.querySelector("#canvas")){ return; } var h; if(platform.name !== null && platform.name.toLowerCase() === "safari"){ h = getIOSWindowHeight(); } else{ h = getSize("Height"); } var w = getSize("Width"); if(s_bFocus){ _checkOrientation(w,h); } var multiplier = Math.min((h / CANVAS_HEIGHT), (w / CANVAS_WIDTH)); var destW = Math.round( CANVAS_WIDTH * multiplier ); var destH = Math.round( CANVAS_HEIGHT * multiplier ); var iAdd = 0; if (destH < h){ iAdd = h - destH; destH += iAdd; destW += iAdd * (CANVAS_WIDTH / CANVAS_HEIGHT); } else if (destW < w){ iAdd = w - destW; destW += iAdd; destH += iAdd * (CANVAS_HEIGHT / CANVAS_WIDTH); } var fOffsetY = ((h / 2) - (destH / 2)); var fOffsetX = ((w / 2) - (destW / 2)); var fGameInverseScaling = (CANVAS_WIDTH / destW); if (fOffsetX * fGameInverseScaling < - EDGEBOARD_X || fOffsetY * fGameInverseScaling < - EDGEBOARD_Y){ multiplier = Math.min(h / (CANVAS_HEIGHT - (EDGEBOARD_Y * 2)), w / (CANVAS_WIDTH - (EDGEBOARD_X * 2))); destW = Math.round( CANVAS_WIDTH * multiplier ); destH = Math.round( CANVAS_HEIGHT * multiplier ); fOffsetY = (h - destH) / 2; fOffsetX = (w - destW) / 2; fGameInverseScaling = (CANVAS_WIDTH / destW); } s_iOffsetX = ( - 1 * fOffsetX * fGameInverseScaling); s_iOffsetY = ( - 1 * fOffsetY * fGameInverseScaling); if (fOffsetY >= 0){ s_iOffsetY = 0; } if (fOffsetX >= 0){ s_iOffsetX = 0; } if (s_oInterface !== null){ s_oInterface.refreshButtonPos(s_iOffsetX, s_iOffsetY); } if (s_oMenu !== null){ s_oMenu.refreshButtonPos(s_iOffsetX, s_iOffsetY); } if (s_oLevelMenu !== null){ s_oLevelMenu.refreshButtonPos(s_iOffsetX, s_iOffsetY); } if(s_bIsIphone && s_oStage){ canvas = document.getElementById('canvas'); s_oStage.canvas.width = destW*2; s_oStage.canvas.height = destH*2; canvas.style.width = destW+"px"; canvas.style.height = destH+"px"; s_iScaleFactor = Math.min(destW / CANVAS_WIDTH, destH / CANVAS_HEIGHT); s_iScaleFactor *= 2; s_oStage.scaleX = s_oStage.scaleY = s_iScaleFactor; }else if(s_bMobile || isChrome()){ document.querySelector("#canvas").style.width = destW+"px"; document.querySelector("#canvas").style.height = destH+"px"; }else if(s_oStage){ s_oStage.canvas.width = destW; s_oStage.canvas.height = destH; s_iScaleFactor = Math.min(destW / CANVAS_WIDTH, destH / CANVAS_HEIGHT); s_oStage.scaleX = s_oStage.scaleY = s_iScaleFactor; } if(fOffsetY >= 0){ fOffsetY = (h - destH)/2; } document.querySelector("#canvas").style.top = fOffsetY+"px"; document.querySelector("#canvas").style.left = fOffsetX+"px"; fullscreenHandler(); }; function createBitmap(oSprite, iWidth, iHeight){ var oBmp = new createjs.Bitmap(oSprite); var hitObject = new createjs.Shape(); if (iWidth && iHeight){ hitObject.graphics.beginFill("#fff").drawRect(0, 0, iWidth, iHeight); } else{ hitObject.graphics.beginFill("#ff0").drawRect(0, 0, oSprite.width, oSprite.height); } oBmp.hitArea = hitObject; return oBmp; } function _checkOrientation(iWidth,iHeight){ if(s_bMobile && ENABLE_CHECK_ORIENTATION){ if( iWidth>iHeight ){ if( document.querySelector(".orientation-msg-container").getAttribute("data-orientation") === "landscape" ){ document.querySelector(".orientation-msg-container").style.display = "none"; s_oMain.startUpdate(); }else{ document.querySelector(".orientation-msg-container").style.display = "block"; s_oMain.stopUpdate(); } }else{ if( document.querySelector(".orientation-msg-container").getAttribute("data-orientation") === "portrait" ){ document.querySelector(".orientation-msg-container").style.display = "none"; s_oMain.startUpdate(); }else{ document.querySelector(".orientation-msg-container").style.display = "block"; s_oMain.stopUpdate(); } } } } function createSprite(oSpriteSheet, szState, iRegX, iRegY, iWidth, iHeight){ if (szState !== null){ var oRetSprite = new createjs.Sprite(oSpriteSheet, szState); } else{ var oRetSprite = new createjs.Sprite(oSpriteSheet); } var hitObject = new createjs.Shape(); hitObject.graphics.beginFill("#000000").drawRect( - iRegX, - iRegY, iWidth, iHeight); oRetSprite.hitArea = hitObject; return oRetSprite; } function randomFloatBetween(minValue, maxValue, precision){ if (typeof (precision) === 'undefined'){ precision = 2; } return parseFloat(Math.min(minValue + (Math.random() * (maxValue - minValue)), maxValue).toFixed(precision)); } function formatTime(iTime){ iTime /= 1000; var iMins = Math.floor(iTime / 60); var iSecs = Math.floor(iTime - (iMins * 60)); //iSecs = parseFloat(iSecs).toFixed(1) var szRet = ""; if (iMins < 10){ szRet += "0" + iMins + ":"; } else{ szRet += iMins + ":"; } if (iSecs < 10){ szRet += "0" + iSecs; } else{ szRet += iSecs; } return szRet; } function NoClickDelay(el) { this.element = el; if (window.Touch) this.element.addEventListener('touchstart', this, false); } //Fisher-Yates Shuffle function shuffle(array) { var counter = array.length, temp, index; // While there are elements in the array while (counter > 0) { // Pick a random index index = Math.floor(Math.random() * counter); // Decrease counter by 1 counter--; // And swap the last element with it temp = array[counter]; array[counter] = array[index]; array[index] = temp; } return array; } NoClickDelay.prototype = { handleEvent: function(e) { switch (e.type) { case 'touchstart': this.onTouchStart(e); break; case 'touchmove': this.onTouchMove(e); break; case 'touchend': this.onTouchEnd(e); break; } }, onTouchStart: function(e) { e.preventDefault(); this.moved = false; this.element.addEventListener('touchmove', this, false); this.element.addEventListener('touchend', this, false); }, onTouchMove: function(e) { this.moved = true; }, onTouchEnd: function(e) { this.element.removeEventListener('touchmove', this, false); this.element.removeEventListener('touchend', this, false); if (!this.moved) { var theTarget = document.elementFromPoint(e.changedTouches[0].clientX, e.changedTouches[0].clientY); if (theTarget.nodeType == 3) theTarget = theTarget.parentNode; var theEvent = document.createEvent('MouseEvents'); theEvent.initEvent('click', true, true); theTarget.dispatchEvent(theEvent); } } }; function toRadian(n) { return ((n) * (Math.PI / 180)); } function toDegree(n) { return ((n) * (180 / Math.PI)); } function ctlArcadeResume(){ if (s_oMain !== null){ s_oMain.startUpdate(); } } function ctlArcadePause(){ if (s_oMain !== null){ s_oMain.stopUpdate(); } } function getParamValue(paramName){ var url = window.location.search.substring(1); var qArray = url.split('&'); for (var i = 0; i < qArray.length; i++) { var pArr = qArray[i].split('='); if (pArr[0] == paramName) return pArr[1]; } } function playSound(szSound,iVolume,bLoop){ if(DISABLE_SOUND_MOBILE === false || s_bMobile === false){ s_aSounds[szSound].play(); s_aSounds[szSound].volume(iVolume); s_aSounds[szSound].loop(bLoop); return s_aSounds[szSound]; } return null; } function stopSound(szSound){ if(DISABLE_SOUND_MOBILE === false || s_bMobile === false){ s_aSounds[szSound].stop(); } } function setVolume(szSound, iVolume){ if(DISABLE_SOUND_MOBILE === false || s_bMobile === false){ s_aSounds[szSound].volume(iVolume); } } function setMute(szSound, bMute){ if(DISABLE_SOUND_MOBILE === false || s_bMobile === false){ s_aSounds[szSound].mute(bMute); } } (function() { var hidden = "hidden"; // Standards: if (hidden in document) document.addEventListener("visibilitychange", onchange); else if ((hidden = "mozHidden") in document) document.addEventListener("mozvisibilitychange", onchange); else if ((hidden = "webkitHidden") in document) document.addEventListener("webkitvisibilitychange", onchange); else if ((hidden = "msHidden") in document) document.addEventListener("msvisibilitychange", onchange); // IE 9 and lower: else if ('onfocusin' in document) document.onfocusin = document.onfocusout = onchange; // All others: else window.onpageshow = window.onpagehide = window.onfocus = window.onblur = onchange; function onchange (evt) { var v = 'visible', h = 'hidden', evtMap = { focus:v, focusin:v, pageshow:v, blur:h, focusout:h, pagehide:h }; evt = evt || window.event; if (evt.type in evtMap){ document.body.className = evtMap[evt.type]; }else{ document.body.className = this[hidden] ? "hidden" : "visible"; if(document.body.className === "hidden"){ s_oMain.stopUpdate(); s_bFocus = false; }else{ s_oMain.startUpdate(); s_bFocus = true; } } } })(); function fullscreenHandler(){ if (!ENABLE_FULLSCREEN || !screenfull.isEnabled){ return; } s_bFullscreen = screenfull.isFullscreen; if (s_oInterface !== null){ s_oInterface.resetFullscreenBut(); } if (s_oMenu !== null){ s_oMenu.resetFullscreenBut(); } } if (screenfull.isEnabled) { screenfull.on('change', function(){ s_bFullscreen = screenfull.isFullscreen; if (s_oInterface !== null){ s_oInterface.resetFullscreenBut(); } if (s_oMenu !== null){ s_oMenu.resetFullscreenBut(); } }); } function saveItem(szItem,oValue){ if(s_bStorageAvailable){ localStorage.setItem(szItem, oValue); } } function getItem(szItem){ if(s_bStorageAvailable){ return localStorage.getItem(szItem); } return null; }