game.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. function Game(heartUrl, callback) {
  2. this.fCode = ['anbaodi', 'huazhuang', 'xiaoheiz'];
  3. this.callback = callback || '';
  4. this.renderType = 'canvas';
  5. this.heartUrl = heartUrl;
  6. this.gameContainer = document.getElementById('gameContainer');
  7. this.text = document.getElementById('text');
  8. this.minutes = document.getElementById('minutes');
  9. this.seconds = document.getElementById('seconds');
  10. this.finalBonus = document.getElementById('finalBonus');
  11. this.select = document.getElementById('select');
  12. this.countText = document.getElementById('countText');
  13. this.money = document.getElementById('money');
  14. this.finalCount = document.getElementById('finalCount');
  15. this.finalMoney = document.getElementById('finalMoney');
  16. this.moneyText = 20;
  17. this.w = 750;
  18. this.h = 1217;
  19. this.averageN = Math.floor(this.w / 94) - 2;
  20. this.averageW = this.w / this.averageN;
  21. this.stage = null;
  22. this.ticker = null;
  23. this.Tween = null;
  24. this.Ease = null;
  25. this.count = 0;
  26. this.times = 20;
  27. this.fcode_base_url = "https://passport.lrlz.com/mobile/index.php?act=bonusex&op=open&client_type=wap&type_sn=";
  28. this.type_sn = '';
  29. this.fcode_url = this.fcode_base_url + this.type_sn;
  30. this.gameContainer.style.height = this.h + 'px';
  31. this.draw = new Draw();
  32. this.init();
  33. this.start();
  34. this.draw.setValue(0);
  35. }
  36. Game.prototype = {
  37. init: function () {
  38. var _this = this;
  39. this.stage = new Hilo.Stage({
  40. renderType: this.renderType,
  41. container: this.gameContainer,
  42. width: this.w,
  43. height: this.h
  44. });
  45. this.stage.enableDOMEvent(Hilo.event.POINTER_START, true);
  46. this.ticker = new Hilo.Ticker(60);
  47. this.Tween = Hilo.Tween;
  48. this.Ease = Hilo.Ease;
  49. this.ticker.addTick(this.stage);
  50. this.ticker.addTick(this.Tween);
  51. this.ticker.interval(function () {
  52. _this.times--;
  53. _this.countTimesText();
  54. if (_this.times <= 0) {
  55. this.gameContainer.innerHTML = '';
  56. _this.finalResult();
  57. $('#speaker_handle').fadeOut('fast');
  58. $('#Jaudio').attr('src', '');
  59. _this.stage.removeAllChildren();
  60. _this.ticker.stop();
  61. }
  62. }, 1000);
  63. this.ticker.start();
  64. },
  65. start: function () {
  66. var _this = this;
  67. for (var i = 0; i < this.averageN; i++) {
  68. var minP = this.averageW * i + 47;
  69. var maxP = this.averageW * (i + 1) - 47;
  70. var x = Math.round(Math.random() * (maxP - minP) + minP);
  71. (function (j, x) {
  72. setTimeout(function () {
  73. _this.create(x);
  74. }, j * 80);
  75. })(i, x);
  76. }
  77. },
  78. create: function (x) {
  79. var _this = this;
  80. var heartSpeed = (Math.random() * 5 + 8).toFixed(2);
  81. var left = x || Math.round(Math.random() * (this.w - 94) + 47);
  82. var heart0 = new Hilo.Bitmap({
  83. image: _this.heartUrl,
  84. rect: [0, 0, 94, 80],
  85. x: left,
  86. y: _this.h,
  87. scaleX: 1,
  88. scaleY: 1,
  89. pivotX: 47,
  90. pivotY: 40,
  91. onUpdate: function () {
  92. this.y -= this.speed;
  93. if (this.y < 0) {
  94. this.removeFromParent();
  95. setTimeout(function () {
  96. _this.create();
  97. }, 100);
  98. }
  99. },
  100. }).addTo(_this.stage);
  101. heart0.on(Hilo.event.POINTER_START, function (e) {
  102. var self = this;
  103. _this.Tween.to(this, {
  104. scaleX: 1.4,
  105. scaleY: 1.4,
  106. alpha: .7
  107. }, {
  108. duration: 300,
  109. ease: _this.Ease.Bounce.EaseIn,
  110. onComplete: function () {
  111. self.removeFromParent();
  112. _this.count++;
  113. _this.calcScore();
  114. setTimeout(function () {
  115. _this.create();
  116. }, 100)
  117. }
  118. });
  119. }, false);
  120. heart0.speed = heartSpeed;
  121. },
  122. countTimesText: function() {
  123. var times = this.times >= 10 ? this.times.toString() : '0' + this.times;
  124. var arr = times.split('');
  125. this.minutes.innerHTML = arr[0];
  126. this.seconds.innerHTML = arr[1];
  127. },
  128. calcScore: function() {
  129. var score = 0;
  130. // 微信分享后玩第二次, app点击后可以玩第二次
  131. if(from_app) {
  132. if(appClick == 1) {
  133. score = this.count * 2 > 70 ? 70 : this.count * 2;
  134. }
  135. else {
  136. score = this.count * 2 > 100 ? 100 : this.count * 2;
  137. }
  138. }
  139. else {
  140. if(!isShare) {
  141. score = this.count * 2 > 70 ? 70 : this.count * 2;
  142. }
  143. else {
  144. score = this.count * 2 > 100 ? 100 : this.count * 2;
  145. }
  146. }
  147. // if(!score)return;
  148. if(score < 21) {
  149. this.moneyText = 20;
  150. this.type_sn = '75871534212363894680';
  151. }
  152. else if(score < 41) {
  153. this.moneyText = 40;
  154. this.type_sn = '90771534212382068658';
  155. }
  156. else if(score < 61) {
  157. this.moneyText = 60;
  158. this.type_sn = '36011534212399441256';
  159. }
  160. else if(score < 81) {
  161. this.moneyText = 80;
  162. this.type_sn = '81851534212419720914'
  163. }
  164. else if(score < 100) {
  165. this.moneyText = 99;
  166. this.type_sn = '42891534212437309614'
  167. }
  168. else {
  169. this.moneyText = 100;
  170. this.type_sn = '46851534212455402795'
  171. }
  172. this.draw.setValue(score);
  173. },
  174. caclBonus: function() {
  175. this.countText.innerHTML = this.finalCount.innerHTML = this.count;
  176. this.money.innerHTML = this.finalMoney.innerHTML = this.moneyText;
  177. var fcode_url = this.fcode_base_url + this.type_sn;
  178. $('.receive_bonus, .final_bonus_link').attr('src', fcode_url);
  179. },
  180. restart: function() {
  181. this.gameContainer.innerHTML = '';
  182. this.times = 20;
  183. this.countTimesText();
  184. this.init();
  185. this.start();
  186. },
  187. finalResult: function() {
  188. this.caclBonus();
  189. if(from_app) {
  190. if(appClick == 1) {
  191. this.select.style.display = 'block';
  192. return;
  193. }
  194. }
  195. else {
  196. if(!isShare) {
  197. this.select.style.display = 'block';
  198. return;
  199. }
  200. }
  201. if(this.count < 100) {
  202. this.finalBonus.style.display = 'block'
  203. }
  204. else {
  205. this.callback && this.callback();
  206. }
  207. }
  208. }