ctl_utils.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  1. var s_fInverseScaling = 0;
  2. var s_iScaleFactor = 1;
  3. var s_bIsIphone = false;
  4. var s_iOffsetX;
  5. var s_iOffsetY;
  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. function trace(szMsg){
  12. console.log(szMsg);
  13. }
  14. function isChrome(){
  15. var isChrome = /Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor);
  16. return isChrome;
  17. }
  18. function isIOS() {
  19. var iDevices = [
  20. 'iPad Simulator',
  21. 'iPhone Simulator',
  22. 'iPod Simulator',
  23. 'iPad',
  24. 'iPhone',
  25. 'iPod'
  26. ];
  27. if (navigator.userAgent.toLowerCase().indexOf("iphone") !== -1){
  28. s_bIsIphone = true;
  29. }
  30. while (iDevices.length) {
  31. if (navigator.platform === iDevices.pop()){
  32. return true;
  33. }
  34. }
  35. s_bIsIphone = false;
  36. return false;
  37. }
  38. $(window).resize(function() {
  39. sizeHandler();
  40. });
  41. function getSize(Name) {
  42. var size;
  43. var name = Name.toLowerCase();
  44. var document = window.document;
  45. var documentElement = document.documentElement;
  46. if (window["inner" + Name] === undefined) {
  47. // IE6 & IE7 don't have window.innerWidth or innerHeight
  48. size = documentElement["client" + Name];
  49. }
  50. else if (window["inner" + Name] != documentElement["client" + Name]) {
  51. // WebKit doesn't include scrollbars while calculating viewport size so we have to get fancy
  52. // Insert markup to test if a media query will match document.doumentElement["client" + Name]
  53. var bodyElement = document.createElement("body");
  54. bodyElement.id = "vpw-test-b";
  55. bodyElement.style.cssText = "overflow:scroll";
  56. var divElement = document.createElement("div");
  57. divElement.id = "vpw-test-d";
  58. divElement.style.cssText = "position:absolute;top:-1000px";
  59. // Getting specific on the CSS selector so it won't get overridden easily
  60. divElement.innerHTML = "<style>@media(" + name + ":" + documentElement["client" + Name] + "px){body#vpw-test-b div#vpw-test-d{" + name + ":7px!important}}</style>";
  61. bodyElement.appendChild(divElement);
  62. documentElement.insertBefore(bodyElement, document.head);
  63. if (divElement["offset" + Name] == 7) {
  64. // Media query matches document.documentElement["client" + Name]
  65. size = documentElement["client" + Name];
  66. }
  67. else {
  68. // Media query didn't match, use window["inner" + Name]
  69. size = window["inner" + Name];
  70. }
  71. // Cleanup
  72. documentElement.removeChild(bodyElement);
  73. }
  74. else {
  75. // Default to use window["inner" + Name]
  76. size = window["inner" + Name];
  77. }
  78. return size;
  79. };
  80. window.addEventListener("orientationchange", onOrientationChange );
  81. function onOrientationChange(){
  82. if (window.matchMedia("(orientation: portrait)").matches) {
  83. // you're in PORTRAIT mode
  84. sizeHandler();
  85. }
  86. if (window.matchMedia("(orientation: landscape)").matches) {
  87. // you're in LANDSCAPE mode
  88. sizeHandler();
  89. }
  90. }
  91. function getIOSWindowHeight() {
  92. // Get zoom level of mobile Safari
  93. // Note, that such zoom detection might not work correctly in other browsers
  94. // We use width, instead of height, because there are no vertical toolbars :)
  95. var zoomLevel = document.documentElement.clientWidth / window.innerWidth;
  96. // window.innerHeight returns height of the visible area.
  97. // We multiply it by zoom and get out real height.
  98. return window.innerHeight * zoomLevel;
  99. };
  100. // You can also get height of the toolbars that are currently displayed
  101. function getHeightOfIOSToolbars() {
  102. var tH = (window.orientation === 0 ? screen.height : screen.width) - getIOSWindowHeight();
  103. return tH > 1 ? tH : 0;
  104. };
  105. //THIS FUNCTION MANAGES THE CANVAS SCALING TO FIT PROPORTIONALLY THE GAME TO THE CURRENT DEVICE RESOLUTION
  106. function sizeHandler() {
  107. window.scrollTo(0, 1);
  108. if (!$("#canvas")){
  109. return;
  110. }
  111. var h;
  112. var iOS = ( navigator.userAgent.match(/(iPad|iPhone|iPod)/g) ? true : false );
  113. if(iOS){
  114. h = getIOSWindowHeight();
  115. }else{
  116. h = getSize("Height");
  117. }
  118. var w = getSize("Width");
  119. _checkOrientation(w,h);
  120. var multiplier = Math.min((h / CANVAS_HEIGHT), (w / CANVAS_WIDTH));
  121. var destW = CANVAS_WIDTH * multiplier;
  122. var destH = CANVAS_HEIGHT * multiplier;
  123. var iAdd = 0;
  124. if (destH < h){
  125. iAdd = h-destH;
  126. destH += iAdd;
  127. destW += iAdd*(CANVAS_WIDTH/CANVAS_HEIGHT);
  128. }else if (destW < w){
  129. iAdd = w-destW;
  130. destW += iAdd;
  131. destH += iAdd*(CANVAS_HEIGHT/CANVAS_WIDTH);
  132. }
  133. var fOffsetY = ((h / 2) - (destH / 2));
  134. var fOffsetX = ((w / 2) - (destW / 2));
  135. var fGameInverseScaling = (CANVAS_WIDTH/destW);
  136. if( fOffsetX*fGameInverseScaling < -EDGEBOARD_X ||
  137. fOffsetY*fGameInverseScaling < -EDGEBOARD_Y ){
  138. multiplier = Math.min( h / (CANVAS_HEIGHT-(EDGEBOARD_Y*2)), w / (CANVAS_WIDTH-(EDGEBOARD_X*2)));
  139. destW = CANVAS_WIDTH * multiplier;
  140. destH = CANVAS_HEIGHT * multiplier;
  141. fOffsetY = ( h - destH ) / 2;
  142. fOffsetX = ( w - destW ) / 2;
  143. fGameInverseScaling = (CANVAS_WIDTH/destW);
  144. }
  145. s_fInverseScaling = fGameInverseScaling;
  146. s_iOffsetX = (-1*fOffsetX * fGameInverseScaling);
  147. s_iOffsetY = (-1*fOffsetY * fGameInverseScaling);
  148. if(fOffsetY >= 0 ){
  149. s_iOffsetY = 0;
  150. }
  151. if(fOffsetX >= 0 ){
  152. s_iOffsetX = 0;
  153. }
  154. if(s_oInterface !== null){
  155. s_oInterface.refreshButtonPos(s_iOffsetX, s_iOffsetY);
  156. }
  157. if(s_oMenu !== null){
  158. s_oMenu.refreshButtonPos(s_iOffsetX, s_iOffsetY);
  159. }
  160. if(s_oTeamChoose !== null){
  161. s_oTeamChoose.refreshButtonPos(s_iOffsetX, s_iOffsetY);
  162. }
  163. if(s_bIsIphone){
  164. canvas = document.getElementById('canvas');
  165. s_oStage.canvas.width = destW*2;
  166. s_oStage.canvas.height = destH*2;
  167. canvas.style.width = destW+"px";
  168. canvas.style.height = destH+"px";
  169. var iScale = Math.min(destW / CANVAS_WIDTH, destH / CANVAS_HEIGHT);
  170. s_oStage.scaleX = s_oStage.scaleY = iScale*2;
  171. }else if(s_bMobile || isChrome()){
  172. $("#canvas").css("width",destW+"px");
  173. $("#canvas").css("height",destH+"px");
  174. }else{
  175. s_oStage.canvas.width = destW;
  176. s_oStage.canvas.height = destH;
  177. s_iScaleFactor = Math.min(destW / CANVAS_WIDTH, destH / CANVAS_HEIGHT);
  178. s_oStage.scaleX = s_oStage.scaleY = s_iScaleFactor;
  179. }
  180. if(fOffsetY < 0){
  181. $("#canvas").css("top",fOffsetY+"px");
  182. s_iCanvasOffsetHeight=fOffsetY;
  183. }else{
  184. $("#canvas").css("top","0px");
  185. s_iCanvasOffsetHeight=0;
  186. }
  187. $("#canvas").css("left",fOffsetX+"px");
  188. resizeCanvas3D();
  189. s_iCanvasResizeWidth = destW;
  190. s_iCanvasResizeHeight = destH;
  191. s_iCanvasOffsetWidth=fOffsetX;
  192. fullscreenHandler();
  193. };
  194. function _checkOrientation(iWidth,iHeight){
  195. if(s_bMobile && ENABLE_CHECK_ORIENTATION){
  196. if( iWidth>iHeight ){
  197. if( $(".orientation-msg-container").attr("data-orientation") === "landscape" ){
  198. $(".orientation-msg-container").css("display","none");
  199. s_oMain.startUpdate();
  200. }else{
  201. $(".orientation-msg-container").css("display","block");
  202. s_oMain.stopUpdate();
  203. }
  204. }else{
  205. if( $(".orientation-msg-container").attr("data-orientation") === "portrait" ){
  206. $(".orientation-msg-container").css("display","none");
  207. s_oMain.startUpdate();
  208. }else{
  209. $(".orientation-msg-container").css("display","block");
  210. s_oMain.stopUpdate();
  211. }
  212. }
  213. }
  214. }
  215. function createBitmap(oSprite, iWidth, iHeight){
  216. var oBmp = new createjs.Bitmap(oSprite);
  217. var hitObject = new createjs.Shape();
  218. if (iWidth && iHeight){
  219. hitObject .graphics.beginFill("#fff").drawRect(0, 0, iWidth, iHeight);
  220. }else{
  221. hitObject .graphics.beginFill("#ff0").drawRect(0, 0, oSprite.width, oSprite.height);
  222. }
  223. oBmp.hitArea = hitObject;
  224. return oBmp;
  225. }
  226. function createSprite(oSpriteSheet, szState, iRegX,iRegY,iWidth, iHeight){
  227. if(szState !== null){
  228. var oRetSprite = new createjs.Sprite(oSpriteSheet, szState);
  229. }else{
  230. var oRetSprite = new createjs.Sprite(oSpriteSheet);
  231. }
  232. var hitObject = new createjs.Shape();
  233. hitObject .graphics.beginFill("#000000").drawRect(-iRegX, -iRegY, iWidth, iHeight);
  234. oRetSprite.hitArea = hitObject;
  235. return oRetSprite;
  236. }
  237. function randomFloatBetween(minValue,maxValue,precision){
  238. if(typeof(precision) === 'undefined'){
  239. precision = 2;
  240. }
  241. return parseFloat(Math.min(minValue + (Math.random() * (maxValue - minValue)),maxValue).toFixed(precision));
  242. }
  243. function rotateVector2D( iAngle, v) {
  244. var iX = v.getX() * Math.cos( iAngle ) + v.getY() * Math.sin( iAngle );
  245. var iY = v.getX() * (-Math.sin( iAngle )) + v.getY() * Math.cos( iAngle );
  246. v.set( iX, iY );
  247. }
  248. function tweenVectorsOnX( vStart, vEnd, iLerp ){
  249. var iNewX = vStart + iLerp *( vEnd-vStart);
  250. return iNewX;
  251. }
  252. function shuffle(array) {
  253. var currentIndex = array.length
  254. , temporaryValue
  255. , randomIndex
  256. ;
  257. // While there remain elements to shuffle...
  258. while (0 !== currentIndex) {
  259. // Pick a remaining element...
  260. randomIndex = Math.floor(Math.random() * currentIndex);
  261. currentIndex -= 1;
  262. // And swap it with the current element.
  263. temporaryValue = array[currentIndex];
  264. array[currentIndex] = array[randomIndex];
  265. array[randomIndex] = temporaryValue;
  266. }
  267. return array;
  268. }
  269. function bubbleSort(a)
  270. {
  271. var swapped;
  272. do {
  273. swapped = false;
  274. for (var i=0; i < a.length-1; i++) {
  275. if (a[i] > a[i+1]) {
  276. var temp = a[i];
  277. a[i] = a[i+1];
  278. a[i+1] = temp;
  279. swapped = true;
  280. }
  281. }
  282. } while (swapped);
  283. }
  284. function compare(a,b) {
  285. if (a.index > b.index)
  286. return -1;
  287. if (a.index < b.index)
  288. return 1;
  289. return 0;
  290. }
  291. //----------------------
  292. // Linear
  293. /**
  294. * Interpolates a value between b and c parameters
  295. * <p></br><b>Note:</b></br>
  296. * &nbsp&nbsp&nbspt and d parameters can be in frames or seconds/milliseconds
  297. *
  298. * @param t Elapsed time
  299. * @param b Initial position
  300. * @param c Final position
  301. * @param d Duration
  302. * @return A value between b and c parameters
  303. */
  304. function easeLinear (t, b, c, d){
  305. return c*t/d + b;
  306. }
  307. //----------------------
  308. // Quad
  309. /**
  310. * Interpolates a value between b and c parameters
  311. * <p></br><b>Note:</b></br>
  312. * &nbsp&nbsp&nbspt and d parameters can be in frames or seconds/milliseconds
  313. *
  314. * @param t Elapsed time
  315. * @param b Initial position
  316. * @param c Final position
  317. * @param d Duration
  318. * @return A value between b and c parameters
  319. */
  320. function easeInQuad (t, b, c, d){
  321. return c*(t/=d)*t + b;
  322. }
  323. //----------------------
  324. // Sine
  325. /**
  326. * Interpolates a value between b and c parameters
  327. * <p></br><b>Note:</b></br>
  328. * &nbsp&nbsp&nbspt and d parameters can be in frames or seconds/milliseconds
  329. *
  330. * @param t Elapsed time
  331. * @param b Initial position
  332. * @param c Final position
  333. * @param d Duration
  334. * @return A value between b and c parameters
  335. */
  336. function easeInSine (t, b, c, d) {
  337. return -c * Math.cos(t/d * (Math.PI/2)) + c + b;
  338. }
  339. function easeInCubic (t, b, c, d) {
  340. return c*(t/=d)*t*t + b;
  341. };
  342. function easeOutCubic (t, b, c, d) {
  343. return c * ((t = t / d - 1) * t * t + 1) + b;
  344. };
  345. function getTrajectoryPoint(t,p){
  346. var result = new createjs.Point();
  347. var oneMinusTSq = (1-t) * (1-t);
  348. var TSq = t*t;
  349. result.x = oneMinusTSq*p.start.x+2*(1-t)*t*p.traj.x+TSq*p.end.x;
  350. result.y = oneMinusTSq*p.start.y+2*(1-t)*t*p.traj.y+TSq*p.end.y;
  351. return result;
  352. }
  353. function formatTime(iTime){
  354. iTime/=1000;
  355. var iMins = Math.floor(iTime/60);
  356. var iSecs = Math.floor(iTime-(iMins*60));
  357. //iSecs = parseFloat(iSecs).toFixed(1)
  358. var szRet = "";
  359. if ( iMins < 10 ){
  360. szRet += "0" + iMins + ":";
  361. }else{
  362. szRet += iMins + ":";
  363. }
  364. if ( iSecs < 10 ){
  365. szRet += "0" + iSecs;
  366. }else{
  367. szRet += iSecs;
  368. }
  369. return szRet;
  370. }
  371. function degreesToRadians(iAngle){
  372. return iAngle * Math.PI / 180;
  373. }
  374. function checkRectCollision(bitmap1,bitmap2) {
  375. var b1, b2;
  376. b1 = getBounds(bitmap1,0.9);
  377. b2 = getBounds(bitmap2,0.98);
  378. return calculateIntersection(b1,b2);
  379. }
  380. function calculateIntersection(rect1, rect2){
  381. // first we have to calculate the
  382. // center of each rectangle and half of
  383. // width and height
  384. var dx, dy, r1={}, r2={};
  385. r1.cx = rect1.x + (r1.hw = (rect1.width /2));
  386. r1.cy = rect1.y + (r1.hh = (rect1.height/2));
  387. r2.cx = rect2.x + (r2.hw = (rect2.width /2));
  388. r2.cy = rect2.y + (r2.hh = (rect2.height/2));
  389. dx = Math.abs(r1.cx-r2.cx) - (r1.hw + r2.hw);
  390. dy = Math.abs(r1.cy-r2.cy) - (r1.hh + r2.hh);
  391. if (dx < 0 && dy < 0) {
  392. dx = Math.min(Math.min(rect1.width,rect2.width),-dx);
  393. dy = Math.min(Math.min(rect1.height,rect2.height),-dy);
  394. return {x:Math.max(rect1.x,rect2.x),
  395. y:Math.max(rect1.y,rect2.y),
  396. width:dx,
  397. height:dy,
  398. rect1: rect1,
  399. rect2: rect2};
  400. } else {
  401. return null;
  402. }
  403. }
  404. function getBounds(obj,iTolerance) {
  405. var bounds={x:Infinity,y:Infinity,width:0,height:0};
  406. if ( obj instanceof createjs.Container ) {
  407. bounds.x2 = -Infinity;
  408. bounds.y2 = -Infinity;
  409. var children = obj.children, l=children.length, cbounds, c;
  410. for ( c = 0; c < l; c++ ) {
  411. cbounds = getBounds(children[c],1);
  412. if ( cbounds.x < bounds.x ) bounds.x = cbounds.x;
  413. if ( cbounds.y < bounds.y ) bounds.y = cbounds.y;
  414. if ( cbounds.x + cbounds.width > bounds.x2 ) bounds.x2 = cbounds.x + cbounds.width;
  415. if ( cbounds.y + cbounds.height > bounds.y2 ) bounds.y2 = cbounds.y + cbounds.height;
  416. //if ( cbounds.x - bounds.x + cbounds.width > bounds.width ) bounds.width = cbounds.x - bounds.x + cbounds.width;
  417. //if ( cbounds.y - bounds.y + cbounds.height > bounds.height ) bounds.height = cbounds.y - bounds.y + cbounds.height;
  418. }
  419. if ( bounds.x == Infinity ) bounds.x = 0;
  420. if ( bounds.y == Infinity ) bounds.y = 0;
  421. if ( bounds.x2 == Infinity ) bounds.x2 = 0;
  422. if ( bounds.y2 == Infinity ) bounds.y2 = 0;
  423. bounds.width = bounds.x2 - bounds.x;
  424. bounds.height = bounds.y2 - bounds.y;
  425. delete bounds.x2;
  426. delete bounds.y2;
  427. } else {
  428. var gp,gp2,gp3,gp4,imgr={},sr;
  429. if ( obj instanceof createjs.Bitmap ) {
  430. sr = obj.sourceRect || obj.image;
  431. imgr.width = sr.width * iTolerance;
  432. imgr.height = sr.height * iTolerance;
  433. } else if ( obj instanceof createjs.Sprite ) {
  434. if ( obj.spriteSheet._frames && obj.spriteSheet._frames[obj.currentFrame] && obj.spriteSheet._frames[obj.currentFrame].image ) {
  435. var cframe = obj.spriteSheet.getFrame(obj.currentFrame);
  436. imgr.width = cframe.rect.width;
  437. imgr.height = cframe.rect.height;
  438. imgr.regX = cframe.regX;
  439. imgr.regY = cframe.regY;
  440. } else {
  441. bounds.x = obj.x || 0;
  442. bounds.y = obj.y || 0;
  443. }
  444. } else {
  445. bounds.x = obj.x || 0;
  446. bounds.y = obj.y || 0;
  447. }
  448. imgr.regX = imgr.regX || 0; imgr.width = imgr.width || 0;
  449. imgr.regY = imgr.regY || 0; imgr.height = imgr.height || 0;
  450. bounds.regX = imgr.regX;
  451. bounds.regY = imgr.regY;
  452. gp = obj.localToGlobal(0 -imgr.regX,0 -imgr.regY);
  453. gp2 = obj.localToGlobal(imgr.width-imgr.regX,imgr.height-imgr.regY);
  454. gp3 = obj.localToGlobal(imgr.width-imgr.regX,0 -imgr.regY);
  455. gp4 = obj.localToGlobal(0 -imgr.regX,imgr.height-imgr.regY);
  456. bounds.x = Math.min(Math.min(Math.min(gp.x,gp2.x),gp3.x),gp4.x);
  457. bounds.y = Math.min(Math.min(Math.min(gp.y,gp2.y),gp3.y),gp4.y);
  458. bounds.width = Math.max(Math.max(Math.max(gp.x,gp2.x),gp3.x),gp4.x) - bounds.x;
  459. bounds.height = Math.max(Math.max(Math.max(gp.y,gp2.y),gp3.y),gp4.y) - bounds.y;
  460. }
  461. return bounds;
  462. }
  463. function NoClickDelay(el) {
  464. this.element = el;
  465. if( window.Touch ) this.element.addEventListener('touchstart', this, false);
  466. }
  467. //Fisher-Yates Shuffle
  468. function shuffle(array) {
  469. var counter = array.length, temp, index;
  470. // While there are elements in the array
  471. while (counter > 0) {
  472. // Pick a random index
  473. index = Math.floor(Math.random() * counter);
  474. // Decrease counter by 1
  475. counter--;
  476. // And swap the last element with it
  477. temp = array[counter];
  478. array[counter] = array[index];
  479. array[index] = temp;
  480. }
  481. return array;
  482. }
  483. NoClickDelay.prototype = {
  484. handleEvent: function(e) {
  485. switch(e.type) {
  486. case 'touchstart': this.onTouchStart(e); break;
  487. case 'touchmove': this.onTouchMove(e); break;
  488. case 'touchend': this.onTouchEnd(e); break;
  489. }
  490. },
  491. onTouchStart: function(e) {
  492. e.preventDefault();
  493. this.moved = false;
  494. this.element.addEventListener('touchmove', this, false);
  495. this.element.addEventListener('touchend', this, false);
  496. },
  497. onTouchMove: function(e) {
  498. this.moved = true;
  499. },
  500. onTouchEnd: function(e) {
  501. this.element.removeEventListener('touchmove', this, false);
  502. this.element.removeEventListener('touchend', this, false);
  503. if( !this.moved ) {
  504. var theTarget = document.elementFromPoint(e.changedTouches[0].clientX, e.changedTouches[0].clientY);
  505. if(theTarget.nodeType == 3) theTarget = theTarget.parentNode;
  506. var theEvent = document.createEvent('MouseEvents');
  507. theEvent.initEvent('click', true, true);
  508. theTarget.dispatchEvent(theEvent);
  509. }
  510. }
  511. };
  512. (function() {
  513. var hidden = "hidden";
  514. // Standards:
  515. if (hidden in document)
  516. document.addEventListener("visibilitychange", onchange);
  517. else if ((hidden = "mozHidden") in document)
  518. document.addEventListener("mozvisibilitychange", onchange);
  519. else if ((hidden = "webkitHidden") in document)
  520. document.addEventListener("webkitvisibilitychange", onchange);
  521. else if ((hidden = "msHidden") in document)
  522. document.addEventListener("msvisibilitychange", onchange);
  523. // IE 9 and lower:
  524. else if ('onfocusin' in document)
  525. document.onfocusin = document.onfocusout = onchange;
  526. // All others:
  527. else
  528. window.onpageshow = window.onpagehide
  529. = window.onfocus = window.onblur = onchange;
  530. function onchange (evt) {
  531. var v = 'visible', h = 'hidden',
  532. evtMap = {
  533. focus:v, focusin:v, pageshow:v, blur:h, focusout:h, pagehide:h
  534. };
  535. evt = evt || window.event;
  536. if (evt.type in evtMap){
  537. document.body.className = evtMap[evt.type];
  538. }else{
  539. document.body.className = this[hidden] ? "hidden" : "visible";
  540. if(document.body.className === "hidden"){
  541. s_oMain.stopUpdate();
  542. }else{
  543. s_oMain.startUpdate();
  544. }
  545. }
  546. }
  547. })();
  548. function ctlArcadeResume(){
  549. if(s_oMain !== null){
  550. s_oMain.startUpdate();
  551. }
  552. }
  553. function ctlArcadePause(){
  554. if(s_oMain !== null){
  555. s_oMain.stopUpdate();
  556. }
  557. }
  558. function getParamValue(paramName){
  559. var url = window.location.search.substring(1);
  560. var qArray = url.split('&');
  561. for (var i = 0; i < qArray.length; i++)
  562. {
  563. var pArr = qArray[i].split('=');
  564. if (pArr[0] == paramName)
  565. return pArr[1];
  566. }
  567. }
  568. function playSound(szSound,iVolume,bLoop){
  569. if(DISABLE_SOUND_MOBILE === false || s_bMobile === false){
  570. s_aSounds[szSound].play();
  571. s_aSounds[szSound].volume(iVolume);
  572. s_aSounds[szSound].loop(bLoop);
  573. return s_aSounds[szSound];
  574. }
  575. return null;
  576. }
  577. function stopSound(szSound){
  578. if(DISABLE_SOUND_MOBILE === false || s_bMobile === false){
  579. s_aSounds[szSound].stop();
  580. }
  581. }
  582. function setVolume(szSound, iVolume){
  583. if(DISABLE_SOUND_MOBILE === false || s_bMobile === false){
  584. s_aSounds[szSound].volume(iVolume);
  585. }
  586. }
  587. function setMute(szSound, bMute){
  588. if(DISABLE_SOUND_MOBILE === false || s_bMobile === false){
  589. s_aSounds[szSound].mute(bMute);
  590. }
  591. }
  592. function destroySound(oPointer){
  593. if (DISABLE_SOUND_MOBILE === false || s_bMobile === false){
  594. oPointer.destroy();
  595. }
  596. }
  597. function resizeCanvas3D(){
  598. $("canvas").each(function(){
  599. if( $(this).attr("id") == "#canvas"){
  600. return;
  601. }
  602. $(this).css("width", $("#canvas").css("width"));
  603. $(this).css("height", $("#canvas").css("height"));
  604. $(this).css("position", $("#canvas").css("position"));
  605. $(this).css("left", $("#canvas").css("left"));
  606. $(this).css("top", $("#canvas").css("top"));
  607. });
  608. }
  609. function createOrthoGraphicCamera(){
  610. var oCamera = new THREE.PerspectiveCamera(FOV, CANVAS_WIDTH / CANVAS_HEIGHT , NEAR, FAR);
  611. oCamera.rotation.x = 90 * (Math.PI / 180);
  612. oCamera.position.set(0,0,0);
  613. oCamera.updateProjectionMatrix();
  614. oCamera.updateMatrixWorld();
  615. return oCamera;
  616. }
  617. Math.radians = function(degrees) {
  618. return degrees * Math.PI / 180;
  619. };
  620. // Converts from radians to degrees.
  621. Math.degrees = function(radians) {
  622. return radians * 180 / Math.PI;
  623. };
  624. function fullscreenHandler(){
  625. if (!ENABLE_FULLSCREEN || screenfull.enabled === false){
  626. return;
  627. }
  628. if(screen.height < window.innerHeight+3 && screen.height > window.innerHeight-3){
  629. s_bFullscreen = true;
  630. }else{
  631. s_bFullscreen = false;
  632. }
  633. if (s_oInterface !== null){
  634. s_oInterface.resetFullscreenBut();
  635. }
  636. if (s_oMenu !== null){
  637. s_oMenu.resetFullscreenBut();
  638. }
  639. if(s_oTeamChoose !== null){
  640. s_oTeamChoose.resetFullscreenBut();
  641. }
  642. }
  643. if (screenfull.enabled) {
  644. screenfull.on('change', function(){
  645. s_bFullscreen = screenfull.isFullscreen;
  646. if (s_oInterface !== null){
  647. s_oInterface.resetFullscreenBut();
  648. }
  649. if (s_oMenu !== null){
  650. s_oMenu.resetFullscreenBut();
  651. }
  652. if(s_oTeamChoose !== null){
  653. s_oTeamChoose.resetFullscreenBut();
  654. }
  655. });
  656. }