common.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. // 全局链接的配置
  2. var btGame;
  3. ;~function(bt){
  4. // 因考虑到,以后可能拿链接的时候,可能要区分游戏
  5. // 所以,统一通过函数返回链接吧~,能更加灵活的更改逻辑了~
  6. bt.URL = {
  7. root: "http://game.9g.com"
  8. ,getMoreGame: function(){
  9. // 点击更多
  10. bt.dc("more");
  11. return "http://game.9g.com";
  12. }
  13. ,getConcern: function(){
  14. return mebtnopenurl;
  15. }
  16. };
  17. // 先用着一个很搓,很有效的方式~
  18. bt.getGameId = function(){
  19. var href = location.href;
  20. href = href.slice(href.indexOf("://") + 3);
  21. var id = href.split("/")[2];
  22. return id;
  23. }
  24. // 先用着一个很搓,很有效的方式~
  25. bt.getGamePath = function(){
  26. var href = location.href;
  27. href = href.slice(0, href.lastIndexOf("/") + 1);
  28. return href;
  29. }
  30. // 调用dc的统计
  31. bt.dc = function(button){
  32. window.Dc_SetButtonClickData && Dc_SetButtonClickData(bt.getGameId(), button);
  33. }
  34. }(btGame || (btGame = {}));
  35. // 基础弹出窗口的定义
  36. var btGame;
  37. ;~function(bt){
  38. function popupBox(id, hideClass){
  39. this.elemId = id;
  40. this.hideClass = hideClass || "bt-hide";
  41. };
  42. popupBox.prototype = {
  43. beforeShow: function(){
  44. // 给别人继承的,什么都不做
  45. }
  46. ,show: function(){
  47. this.beforeShow();
  48. // 为了让动画生效,加了一个timer
  49. var that = this;
  50. setTimeout(function(){
  51. $("#" + that.elemId).removeClass(that.hideClass);
  52. }, 1);
  53. }
  54. ,hide: function(){
  55. $("#" + this.elemId).addClass(this.hideClass);
  56. }
  57. };
  58. bt.popupBox = popupBox;
  59. }(btGame || (btGame = {}));
  60. // 代理函数
  61. var btGame;
  62. ;~function(bt){
  63. bt.proxy = function(func, define){
  64. return function(){
  65. func.apply(define, arguments);
  66. }
  67. }
  68. }(btGame || (btGame = {}));
  69. // 发布者
  70. var btGame;
  71. ;~function(bt){
  72. var publisher = function(obj){
  73. this.__publisher__ = obj;
  74. };
  75. publisher.prototype = {
  76. on: function(ev, func){
  77. this.__publisher__.on(ev, bt.proxy(func, this));
  78. },
  79. fire: function(ev){
  80. this.__publisher__.trigger(ev, [].slice.call(arguments, 1));
  81. },
  82. off: function(ev, func){
  83. if(func){
  84. this.__publisher__.off(ev, bt.proxy(func, this));
  85. }else{
  86. this.__publisher__.off(ev);
  87. }
  88. }
  89. };
  90. bt.makePublisher = function(obj){
  91. var type = typeof obj;
  92. var p = new publisher($("<div></div>"));
  93. if(type == "function"){
  94. obj.prototype.__publisher__ = p.__publisher__;
  95. $.extend(obj.prototype, publisher.prototype);
  96. }else if(type == "object"){
  97. obj.__publisher__ = p.__publisher__;
  98. $.extend(obj, publisher.prototype);
  99. }
  100. }
  101. }(btGame || (btGame = {}));
  102. // 获取 body 元素
  103. var btGame;
  104. ;~function(bt){
  105. // 因为插入,大部分都是基于 body 元素的
  106. // 提供一个获取 body 对象的方法
  107. var body;
  108. function getB(){
  109. if(!body){
  110. body = document.body || document.getElementsByTagName("body")[0];
  111. }
  112. return body;
  113. }
  114. bt.getDomBody = getB;
  115. function craeteDiv(){
  116. return document.createElement("div");
  117. }
  118. bt.getNewDiv = craeteDiv;
  119. }(btGame || (btGame = {}));
  120. // 锁屏
  121. var btGame;
  122. ;~function(bt){
  123. // 指定lock的id,如果没有,则调用默认的锁屏
  124. // 当调用show的时候,再检测锁屏是否存在,不存在锁屏,则创建
  125. // 通过添加、移除 class:bt-hide,进行显示和隐藏锁屏
  126. // 锁屏的动画,是通过 class:bt-animation 实现的
  127. var defaultLockId = "bt-lock-screen";
  128. var createLock = function(id){
  129. var div = bt.getNewDiv();
  130. div.id = id;
  131. var body = bt.getDomBody();
  132. body.appendChild(div);
  133. return $(div);
  134. };
  135. var lock = function(lockId){
  136. bt.popupBox.call(this, lockId || defaultLockId);
  137. };
  138. lock.__super__ = bt.popupBox;
  139. lock.prototype = $.extend({}, bt.popupBox.prototype, {
  140. beforeShow: function(){
  141. var elem = this.getElem();
  142. if(elem.size() <= 0){
  143. elem = createLock(this.elemId);
  144. elem.addClass("bt-lock-screen bt-animation bt-hide");
  145. }
  146. }
  147. ,remove: function(){
  148. var elem = this.getElem();
  149. if(elem.size() > 0){
  150. elem.addClass("bt-hide");
  151. // 为了看到动画,延迟一下吧
  152. setTimeout(function(){
  153. elem.remove();
  154. }, 200);
  155. }
  156. }
  157. ,getElem: function(){
  158. return $("#" + this.elemId);
  159. }
  160. });
  161. bt.lockScreen = function(id){
  162. return new lock(id);
  163. }
  164. }(btGame || (btGame = {}));
  165. // 加载中的loading
  166. var btGame;
  167. ;~function(bt){
  168. var loadingDiv = null;
  169. var loadingText = null;
  170. var loading = function(rate, error){
  171. if(rate > 0 && !loadingDiv){
  172. loadingDiv = $(btGame.getNewDiv());
  173. loadingDiv.addClass("bt-game-loading");
  174. loadingDiv.html('<table><tr><td><img class="bt-img" src="'+bt.URL.root+'/dqe/preloadImage.png" /><div class="bt-text"></div></td></tr></table>');
  175. bt.getDomBody().appendChild(loadingDiv[0]);
  176. loadingText = loadingDiv.find(".bt-text");
  177. }
  178. if(loadingDiv){
  179. if(error){
  180. loadingText.html(error);
  181. }else{
  182. var r = Math.round(rate * 100);
  183. loadingText.html("加载进度:" + r + "%");
  184. }
  185. }
  186. if(rate >= 1){
  187. loadingDiv && loadingDiv.remove();
  188. loadingDiv = null;
  189. }
  190. }
  191. bt.gameLoading = loading;
  192. }(btGame || (btGame = {}));
  193. // 游戏区域大小设定
  194. // btGame.resizePlayArea($elem, width, height, top, left)
  195. // $elem: jQuery元素
  196. // width: 画布期望宽度
  197. // height: 画布期望高度
  198. // top: "top", "center", "bottom" 或 px
  199. // left: "left", "center", "right" 或 px
  200. var btGame;
  201. ;~function(bt){
  202. // @width: canvas期望的宽度
  203. // @height: canvas期望的高度
  204. function rate(width, height){
  205. var wWidth = window.innerWidth,
  206. wHeight = window.innerHeight;
  207. var mid;
  208. if(width <= wWidth && height <= wHeight){
  209. // 如果在屏幕内,就不用改了
  210. }else if(width > wWidth && height > wHeight){
  211. // 如果都大于屏幕
  212. var rateW = wWidth / width, rateH = wHeight / height;
  213. // 看谁更小,就以谁作为标准
  214. if(rateW <= rateH){
  215. mid = width;
  216. width = wWidth;
  217. height = height * width / mid;
  218. }else{
  219. mid = height;
  220. height = wHeight;
  221. width = width * height / mid;
  222. }
  223. }else if(width > wWidth){
  224. // 只有宽度大于屏幕
  225. mid = width;
  226. width = wWidth;
  227. height = height * wWidth / mid;
  228. }else if(height > wHeight){
  229. // 只有高度大于屏幕
  230. mid = height;
  231. height = wHeight;
  232. width = width * wHeight / mid;
  233. }else{
  234. // 没救了
  235. }
  236. var top = (wHeight - height) / 2, left = (wWidth - width) / 2;
  237. return {
  238. width: width
  239. ,height: height
  240. ,top: top
  241. ,left: left
  242. };
  243. }
  244. function resize($elem, width, height, top, left){
  245. var result = rate(width, height);
  246. $elem.css({
  247. width: result.width
  248. ,height: result.height
  249. ,top: top == "center" ? result.top : top == "left" ? 0 : top
  250. ,left: left == "center" ? result.left : left == "left" ? 0 : left
  251. });
  252. switch(top){
  253. case "top":
  254. $elem.css({top: 0});
  255. break;
  256. case "center":
  257. $elem.css({top: result.top});
  258. break;
  259. case "bottom":
  260. $elem.css({bottom: 0});
  261. break;
  262. default:
  263. $elem.css({top: top});
  264. }
  265. switch(left){
  266. case "left":
  267. $elem.css({left: 0});
  268. break;
  269. case "center":
  270. $elem.css({left: result.left});
  271. break;
  272. case "right":
  273. $elem.css({right: 0});
  274. break;
  275. default:
  276. $elem.css({left: left});
  277. }
  278. $elem.trigger("resizePlayArea", [result]);
  279. }
  280. function bindResize($elem, width, height, top, left){
  281. bt.checkHScreen(function(){
  282. setTimeout(function(){
  283. resize($elem, width, height, top, left);
  284. }, 500);
  285. });
  286. }
  287. bt.resizeArea = resize;
  288. bt.resizePlayArea = bindResize;
  289. }(btGame || (btGame = {}));
  290. // 横竖屏检测
  291. var btGame;
  292. ;~function(bt){
  293. // 检测,如果是横屏:true,如果是竖屏:false
  294. // 主要看window的宽高大小,如果width > height,就是横屏,反之~
  295. // btGame.checkHScreen(callback, once);
  296. // @param callback 回调函数 callback(true) -> 横屏
  297. // @param once 是否只检测一次,默认false,一直检测
  298. var screenResize = function(cb) {
  299. // 横屏true,竖屏false
  300. cb && cb(window.innerWidth > window.innerHeight);
  301. };
  302. function check(callback, once){
  303. if(!once){
  304. window.addEventListener("orientationchange", function() {
  305. screenResize(callback);
  306. });
  307. window.addEventListener("resize", function() {
  308. screenResize(callback);
  309. });
  310. }
  311. screenResize(callback);
  312. }
  313. bt.checkHScreen = check;
  314. }(btGame || (btGame = {}));
  315. // 横、竖屏的提醒
  316. var btGame;
  317. ;~function(bt){
  318. var onlyH = function(once, callback){
  319. this.myCallback = callback;
  320. this.tipsCount = 0;
  321. bt.checkHScreen(bt.proxy(this.callback, this), false);
  322. if(once){
  323. this.once = once;
  324. }
  325. };
  326. onlyH.prototype = {
  327. hscreen: function(){
  328. // 是横屏
  329. // 设置了提醒一次,就真的只提醒一次了
  330. this.buildScreen();
  331. if(this.once && this.tipsCount <= 0){
  332. this.screen && this.screen.show();
  333. }else if(!this.once){
  334. this.screen && this.screen.show();
  335. }
  336. this.tipsCount++;
  337. }
  338. ,vscreen: function(){
  339. // 不是横屏,把提醒去掉
  340. this.screen && this.screen.hide();
  341. this.myCallback && this.myCallback(this.tipsCount);
  342. }
  343. ,getScreenOption: function(){
  344. return {
  345. id: "bt-h-scrren"
  346. ,html: "<table><tr><td><img class='bt-h-screen-img' src='"+bt.URL.root+"/dqe/bt-play-h-screen.png' /></td></tr></table>"
  347. ,time: 0
  348. ,lockId: 'bt-hide-lock'
  349. };
  350. }
  351. // 下面的两个,逻辑是不用重新更改的
  352. ,buildScreen: function(){
  353. !this.screen && (
  354. this.screen = btGame.advertisement(this.getScreenOption())
  355. );
  356. }
  357. ,callback: function(isHScreen){
  358. isHScreen ? this.vscreen() : this.hscreen();
  359. }
  360. };
  361. // 竖屏的检测,继承于横屏
  362. var onlyV = function(once, callback){
  363. onlyH.call(this, once, callback);
  364. }
  365. onlyV.__super__ = onlyH;
  366. onlyV.prototype = $.extend({}, onlyH.prototype, {
  367. hscreen: function(){
  368. onlyH.prototype.vscreen.call(this);
  369. }
  370. ,vscreen: function(){
  371. onlyH.prototype.hscreen.call(this);
  372. }
  373. ,getScreenOption: function(){
  374. return {
  375. id: "bt-v-scrren"
  376. ,html: "<table><tr><td><img class='bt-v-screen-img' src='"+bt.URL.root+"/dqe/bt-play-v-screen.png' /></td></tr></table>"
  377. ,time: 0
  378. ,lockId: 'bt-hide-lock'
  379. };
  380. }
  381. });
  382. bt.onlyHScreen = function(once, callback){
  383. return new onlyH(once, callback);
  384. };
  385. bt.onlyVScreen = function(once, callback){
  386. return new onlyV(once, callback);
  387. }
  388. }(btGame || (btGame = {}));