ctl_utils.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. var s_iScaleFactor = 1;
  2. var s_bIsIphone = false;
  3. var s_iOffsetX;
  4. var s_iOffsetY;
  5. var s_bFocus = true;
  6. /**
  7. * jQuery.browser.mobile (http://detectmobilebrowser.com/)
  8. * jQuery.browser.mobile will be true if the browser is a mobile device
  9. **/
  10. (function(a){(jQuery.browser = jQuery.browser || {}).mobile = /android|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(ad|hone|od)|iris|kindle|lge |maemo|midp|mmp|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|symbian|tablet|treo|up\.(browser|link)|vodafone|wap|webos|windows (ce|phone)|xda|xiino/i.test(a) || /1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|e\-|e\/|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(di|rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|xda(\-|2|g)|yas\-|your|zeto|zte\-/i.test(a.substr(0, 4))})(navigator.userAgent || navigator.vendor || window.opera);
  11. $(window).resize(function() {
  12. sizeHandler();
  13. });
  14. function trace(szMsg){
  15. console.log(szMsg);
  16. }
  17. function isChrome(){
  18. var isChrome = /Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor);
  19. return isChrome;
  20. }
  21. function isIOS() {
  22. var iDevices = [
  23. 'iPad Simulator',
  24. 'iPhone Simulator',
  25. 'iPod Simulator',
  26. 'iPad',
  27. 'iPhone',
  28. 'iPod'
  29. ];
  30. if (navigator.userAgent.toLowerCase().indexOf("iphone") !== -1){
  31. s_bIsIphone = true;
  32. return true;
  33. }
  34. while (iDevices.length) {
  35. if (navigator.platform === iDevices.pop()){
  36. return true;
  37. }
  38. }
  39. s_bIsIphone = false;
  40. return false;
  41. }
  42. function isIpad() {
  43. var isIpad = navigator.userAgent.toLowerCase().indexOf('ipad') !== -1;
  44. if (!isIpad && navigator.userAgent.match(/Mac/) && navigator.maxTouchPoints && navigator.maxTouchPoints > 2) {
  45. return true;
  46. }
  47. return isIpad;
  48. }
  49. function isMobile(){
  50. if(isIpad()){
  51. return true;
  52. }
  53. return jQuery.browser.mobile;
  54. }
  55. function getSize(Name) {
  56. var size;
  57. var name = Name.toLowerCase();
  58. var document = window.document;
  59. var documentElement = document.documentElement;
  60. if (window["inner" + Name] === undefined) {
  61. // IE6 & IE7 don't have window.innerWidth or innerHeight
  62. size = documentElement["client" + Name];
  63. }
  64. else if (window["inner" + Name] != documentElement["client" + Name]) {
  65. // WebKit doesn't include scrollbars while calculating viewport size so we have to get fancy
  66. // Insert markup to test if a media query will match document.doumentElement["client" + Name]
  67. var bodyElement = document.createElement("body");
  68. bodyElement.id = "vpw-test-b";
  69. bodyElement.style.cssText = "overflow:scroll";
  70. var divElement = document.createElement("div");
  71. divElement.id = "vpw-test-d";
  72. divElement.style.cssText = "position:absolute;top:-1000px";
  73. // Getting specific on the CSS selector so it won't get overridden easily
  74. divElement.innerHTML = "<style>@media(" + name + ":" + documentElement["client" + Name] + "px){body#vpw-test-b div#vpw-test-d{" + name + ":7px!important}}</style>";
  75. bodyElement.appendChild(divElement);
  76. documentElement.insertBefore(bodyElement, document.head);
  77. if (divElement["offset" + Name] == 7) {
  78. // Media query matches document.documentElement["client" + Name]
  79. size = documentElement["client" + Name];
  80. }
  81. else {
  82. // Media query didn't match, use window["inner" + Name]
  83. size = window["inner" + Name];
  84. }
  85. // Cleanup
  86. documentElement.removeChild(bodyElement);
  87. }
  88. else {
  89. // Default to use window["inner" + Name]
  90. size = window["inner" + Name];
  91. }
  92. return size;
  93. };
  94. window.addEventListener("orientationchange", onOrientationChange);
  95. function onOrientationChange(){
  96. if (window.matchMedia("(orientation: portrait)").matches) {
  97. // you're in PORTRAIT mode
  98. sizeHandler();
  99. }
  100. if (window.matchMedia("(orientation: landscape)").matches) {
  101. // you're in LANDSCAPE mode
  102. sizeHandler();
  103. }
  104. }
  105. function getIOSWindowHeight() {
  106. // Get zoom level of mobile Safari
  107. // Note, that such zoom detection might not work correctly in other browsers
  108. // We use width, instead of height, because there are no vertical toolbars :)
  109. var zoomLevel = document.documentElement.clientWidth / window.innerWidth;
  110. // window.innerHeight returns height of the visible area.
  111. // We multiply it by zoom and get out real height.
  112. return window.innerHeight * zoomLevel;
  113. };
  114. // You can also get height of the toolbars that are currently displayed
  115. function getHeightOfIOSToolbars() {
  116. var tH = (window.orientation === 0 ? screen.height : screen.width) - getIOSWindowHeight();
  117. return tH > 1 ? tH : 0;
  118. };
  119. //THIS FUNCTION MANAGES THE CANVAS SCALING TO FIT PROPORTIONALLY THE GAME TO THE CURRENT DEVICE RESOLUTION
  120. function sizeHandler() {
  121. window.scrollTo(0, 1);
  122. if (!$("#canvas")){
  123. return;
  124. }
  125. var h;
  126. if(platform.name !== null && platform.name.toLowerCase() === "safari"){
  127. h = getIOSWindowHeight();
  128. }else{
  129. h = getSize("Height");
  130. }
  131. var w = getSize("Width");
  132. if(s_bFocus){
  133. _checkOrientation(w,h);
  134. }
  135. var multiplier = Math.min((h / CANVAS_HEIGHT), (w / CANVAS_WIDTH));
  136. var destW = Math.round(CANVAS_WIDTH * multiplier);
  137. var destH = Math.round(CANVAS_HEIGHT * multiplier);
  138. var iAdd = 0;
  139. if (destH < h){
  140. iAdd = h - destH;
  141. destH += iAdd;
  142. destW += iAdd * (CANVAS_WIDTH / CANVAS_HEIGHT);
  143. } else if (destW < w){
  144. iAdd = w - destW;
  145. destW += iAdd;
  146. destH += iAdd * (CANVAS_HEIGHT / CANVAS_WIDTH);
  147. }
  148. var fOffsetY = ((h / 2) - (destH / 2));
  149. var fOffsetX = ((w / 2) - (destW / 2));
  150. var fGameInverseScaling = (CANVAS_WIDTH / destW);
  151. if (fOffsetX * fGameInverseScaling < - EDGEBOARD_X ||
  152. fOffsetY * fGameInverseScaling < - EDGEBOARD_Y){
  153. multiplier = Math.min(h / (CANVAS_HEIGHT - (EDGEBOARD_Y * 2)), w / (CANVAS_WIDTH - (EDGEBOARD_X * 2)));
  154. destW = Math.round(CANVAS_WIDTH * multiplier);
  155. destH = Math.round(CANVAS_HEIGHT * multiplier);
  156. fOffsetY = (h - destH) / 2;
  157. fOffsetX = (w - destW) / 2;
  158. fGameInverseScaling = (CANVAS_WIDTH / destW);
  159. }
  160. s_iOffsetX = ( - 1 * fOffsetX * fGameInverseScaling);
  161. s_iOffsetY = ( - 1 * fOffsetY * fGameInverseScaling);
  162. if (fOffsetY >= 0){
  163. s_iOffsetY = 0;
  164. }
  165. if (fOffsetX >= 0){
  166. s_iOffsetX = 0;
  167. }
  168. if (s_oInterface !== null){
  169. s_oInterface.refreshButtonPos();
  170. }
  171. if (s_oMenu !== null){
  172. s_oMenu.refreshButtonPos();
  173. }
  174. if(s_bIsIphone && s_oStage){
  175. canvas = document.getElementById('canvas');
  176. s_oStage.canvas.width = destW*2;
  177. s_oStage.canvas.height = destH*2;
  178. canvas.style.width = destW+"px";
  179. canvas.style.height = destH+"px";
  180. var iScale = Math.min(destW / CANVAS_WIDTH, destH / CANVAS_HEIGHT);
  181. s_oStage.scaleX = s_oStage.scaleY = iScale*2;
  182. }else if(s_bMobile || isChrome()){
  183. $("#canvas").css("width",destW+"px");
  184. $("#canvas").css("height",destH+"px");
  185. }else if(s_oStage){
  186. s_oStage.canvas.width = destW;
  187. s_oStage.canvas.height = destH;
  188. s_iScaleFactor = Math.min(destW / CANVAS_WIDTH, destH / CANVAS_HEIGHT);
  189. s_oStage.scaleX = s_oStage.scaleY = s_iScaleFactor;
  190. }
  191. if(fOffsetY < 0){
  192. $("#canvas").css("top",fOffsetY+"px");
  193. }else{
  194. // centered game
  195. fOffsetY = (h - destH)/2;
  196. $("#canvas").css("top",fOffsetY+"px");
  197. }
  198. $("#canvas").css("left", fOffsetX + "px");
  199. fullscreenHandler();
  200. };
  201. function createBitmap(oSprite, iWidth, iHeight){
  202. var oBmp = new createjs.Bitmap(oSprite);
  203. var hitObject = new createjs.Shape();
  204. if (iWidth && iHeight){
  205. hitObject.graphics.beginFill("#fff").drawRect(0, 0, iWidth, iHeight);
  206. } else{
  207. hitObject.graphics.beginFill("#ff0").drawRect(0, 0, oSprite.width, oSprite.height);
  208. }
  209. oBmp.hitArea = hitObject;
  210. return oBmp;
  211. }
  212. function createSprite(oSpriteSheet, szState, iRegX, iRegY, iWidth, iHeight){
  213. if (szState !== null){
  214. var oRetSprite = new createjs.Sprite(oSpriteSheet, szState);
  215. } else{
  216. var oRetSprite = new createjs.Sprite(oSpriteSheet);
  217. }
  218. var hitObject = new createjs.Shape();
  219. hitObject.graphics.beginFill("#000000").drawRect( - iRegX, - iRegY, iWidth, iHeight);
  220. oRetSprite.hitArea = hitObject;
  221. return oRetSprite;
  222. }
  223. function randomFloatBetween(minValue, maxValue, precision){
  224. if (typeof (precision) === 'undefined'){
  225. precision = 2;
  226. }
  227. return parseFloat(Math.min(minValue + (Math.random() * (maxValue - minValue)), maxValue).toFixed(precision));
  228. }
  229. function formatTime(iTime){
  230. iTime /= 1000;
  231. var iMins = Math.floor(iTime / 60);
  232. var iSecs = Math.floor(iTime - (iMins * 60));
  233. //iSecs = parseFloat(iSecs).toFixed(1)
  234. var szRet = "";
  235. if (iMins < 10){
  236. szRet += "0" + iMins + ":";
  237. } else{
  238. szRet += iMins + ":";
  239. }
  240. if (iSecs < 10){
  241. szRet += "0" + iSecs;
  242. } else{
  243. szRet += iSecs;
  244. }
  245. return szRet;
  246. }
  247. function NoClickDelay(el) {
  248. this.element = el;
  249. if (window.Touch) this.element.addEventListener('touchstart', this, false);
  250. }
  251. //Fisher-Yates Shuffle
  252. function shuffle(array) {
  253. var counter = array.length, temp, index;
  254. // While there are elements in the array
  255. while (counter > 0) {
  256. // Pick a random index
  257. index = Math.floor(Math.random() * counter);
  258. // Decrease counter by 1
  259. counter--;
  260. // And swap the last element with it
  261. temp = array[counter];
  262. array[counter] = array[index];
  263. array[index] = temp;
  264. }
  265. return array;
  266. }
  267. NoClickDelay.prototype = {
  268. handleEvent: function(e) {
  269. switch (e.type) {
  270. case 'touchstart': this.onTouchStart(e); break;
  271. case 'touchmove': this.onTouchMove(e); break;
  272. case 'touchend': this.onTouchEnd(e); break;
  273. }
  274. },
  275. onTouchStart: function(e) {
  276. e.preventDefault();
  277. this.moved = false;
  278. this.element.addEventListener('touchmove', this, false);
  279. this.element.addEventListener('touchend', this, false);
  280. },
  281. onTouchMove: function(e) {
  282. this.moved = true;
  283. },
  284. onTouchEnd: function(e) {
  285. this.element.removeEventListener('touchmove', this, false);
  286. this.element.removeEventListener('touchend', this, false);
  287. if (!this.moved) {
  288. var theTarget = document.elementFromPoint(e.changedTouches[0].clientX, e.changedTouches[0].clientY);
  289. if (theTarget.nodeType == 3) theTarget = theTarget.parentNode;
  290. var theEvent = document.createEvent('MouseEvents');
  291. theEvent.initEvent('click', true, true);
  292. theTarget.dispatchEvent(theEvent);
  293. }
  294. }
  295. };
  296. function ctlArcadeResume(){
  297. if (s_oMain !== null){
  298. s_oMain.startUpdate();
  299. }
  300. }
  301. function ctlArcadePause(){
  302. if (s_oMain !== null){
  303. s_oMain.stopUpdate();
  304. }
  305. }
  306. function getParamValue(paramName){
  307. var url = window.location.search.substring(1);
  308. var qArray = url.split('&');
  309. for (var i = 0; i < qArray.length; i++)
  310. {
  311. var pArr = qArray[i].split('=');
  312. if (pArr[0] == paramName)
  313. return pArr[1];
  314. }
  315. }
  316. function playSound(szSound,iVolume,bLoop){
  317. if(DISABLE_SOUND_MOBILE === false || s_bMobile === false){
  318. s_aSounds[szSound].play();
  319. s_aSounds[szSound].volume(iVolume);
  320. s_aSounds[szSound].loop(bLoop);
  321. return s_aSounds[szSound];
  322. }
  323. return null;
  324. }
  325. function stopSound(szSound){
  326. if(DISABLE_SOUND_MOBILE === false || s_bMobile === false){
  327. s_aSounds[szSound].stop();
  328. }
  329. }
  330. function setVolume(szSound, iVolume){
  331. if(DISABLE_SOUND_MOBILE === false || s_bMobile === false){
  332. s_aSounds[szSound].volume(iVolume);
  333. }
  334. }
  335. function setMute(szSound, bMute){
  336. if(DISABLE_SOUND_MOBILE === false || s_bMobile === false){
  337. s_aSounds[szSound].mute(bMute);
  338. }
  339. }
  340. (function() {
  341. var hidden = "hidden";
  342. // Standards:
  343. if (hidden in document)
  344. document.addEventListener("visibilitychange", onchange);
  345. else if ((hidden = "mozHidden") in document)
  346. document.addEventListener("mozvisibilitychange", onchange);
  347. else if ((hidden = "webkitHidden") in document)
  348. document.addEventListener("webkitvisibilitychange", onchange);
  349. else if ((hidden = "msHidden") in document)
  350. document.addEventListener("msvisibilitychange", onchange);
  351. // IE 9 and lower:
  352. else if ('onfocusin' in document)
  353. document.onfocusin = document.onfocusout = onchange;
  354. // All others:
  355. else
  356. window.onpageshow = window.onpagehide
  357. = window.onfocus = window.onblur = onchange;
  358. function onchange (evt) {
  359. var v = 'visible', h = 'hidden',
  360. evtMap = {
  361. focus:v, focusin:v, pageshow:v, blur:h, focusout:h, pagehide:h
  362. };
  363. evt = evt || window.event;
  364. if (evt.type in evtMap){
  365. document.body.className = evtMap[evt.type];
  366. }else{
  367. document.body.className = this[hidden] ? "hidden" : "visible";
  368. if(document.body.className === "hidden"){
  369. s_oMain.stopUpdate();
  370. s_bFocus = false;
  371. }else{
  372. s_oMain.startUpdate();
  373. s_bFocus = true;
  374. }
  375. }
  376. }
  377. })();
  378. function _checkOrientation(iWidth,iHeight){
  379. if(s_bMobile && ENABLE_CHECK_ORIENTATION){
  380. if( iWidth>iHeight ){
  381. if( $(".orientation-msg-container").attr("data-orientation") === "landscape" ){
  382. $(".orientation-msg-container").css("display","none");
  383. s_oMain.startUpdate();
  384. }else{
  385. $(".orientation-msg-container").css("display","block");
  386. s_oMain.stopUpdate();
  387. }
  388. }else{
  389. if( $(".orientation-msg-container").attr("data-orientation") === "portrait" ){
  390. $(".orientation-msg-container").css("display","none");
  391. s_oMain.startUpdate();
  392. }else{
  393. $(".orientation-msg-container").css("display","block");
  394. s_oMain.stopUpdate();
  395. }
  396. }
  397. }
  398. }
  399. function easeLinear (t, b, c, d) {
  400. return c * t / d + b;
  401. };
  402. function collisionWithCircle(oObj1, oObj2,fFactor){
  403. var iDx = oObj1.getX() - oObj2.getX();
  404. var iDy = oObj1.getY() - oObj2.getY();
  405. var fdistance = Math.sqrt((iDx * iDx) + (iDy * iDy));
  406. if (fdistance < oObj1.getCollision() * fFactor + oObj2.getCollision() * fFactor) {
  407. return true;
  408. }else{
  409. return false;
  410. }
  411. }
  412. function fullscreenHandler(){
  413. if (!ENABLE_FULLSCREEN || screenfull.isEnabled === false){
  414. return;
  415. }
  416. s_bFullscreen = screenfull.isFullscreen;
  417. if (s_oInterface !== null){
  418. s_oInterface.resetFullscreenBut();
  419. }
  420. if (s_oMenu !== null){
  421. s_oMenu.resetFullscreenBut();
  422. }
  423. }
  424. if (screenfull.isEnabled) {
  425. screenfull.on('change', function(){
  426. s_bFullscreen = screenfull.isFullscreen;
  427. if (s_oInterface !== null){
  428. s_oInterface.resetFullscreenBut();
  429. }
  430. if (s_oMenu !== null){
  431. s_oMenu.resetFullscreenBut();
  432. }
  433. });
  434. }