pkstart.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // Learn cc.Class:
  2. // - https://docs.cocos.com/creator/manual/en/scripting/class.html
  3. // Learn Attribute:
  4. // - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
  5. // Learn life-cycle callbacks:
  6. // - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html
  7. cc.Class({
  8. extends: cc.Component,
  9. properties: {
  10. mycar_runnode: cc.Node,
  11. opposite_runnode: cc.Node,
  12. endpic: cc.Node,
  13. endcarnum: 0,
  14. mylist: [],
  15. opposite_list: [],
  16. pic_toast: cc.Node,
  17. reword_number:cc.Label,
  18. },
  19. // LIFE-CYCLE CALLBACKS:
  20. onLoad() {
  21. this.node.on('click',this.startpk,this);
  22. },
  23. startpk() {
  24. MyAudio.PlayEffect_pkbgm();
  25. let opposite_level = Number(Storage.Get_storage(Storage.GameMessageType.pklevel)) + 1;
  26. this.endpic.active = false;
  27. this.node.active = false;
  28. this.my_list = this.GetRandomList(Global.MaxCarLevel);
  29. this.opposite_list = this.GetRandomList(opposite_level);
  30. this.MyCarAction( this.my_list);
  31. this.OppositeCarAction(this.opposite_list);
  32. Global.CocosAnalysticCustomEvent('PK','Start')
  33. },
  34. OppositeCarAction(randomlist) {
  35. //初始位置
  36. let placeAction = cc.place(470, 0);
  37. let actionMove_1_1 = cc.moveTo(randomlist[0]/2, cc.v2(470, -300));
  38. let actionMove_1_2 = cc.moveTo(randomlist[1], cc.v2(-470, 300));
  39. let actionMove_1_3 = cc.moveTo(randomlist[2]/2, cc.v2(470, 0));
  40. let actionMove_2_1 = cc.moveTo(randomlist[5]/2, cc.v2(470, -300));
  41. let actionMove_2_2 = cc.moveTo(randomlist[6], cc.v2(-470, 300));
  42. let actionMove_2_3 = cc.moveTo(randomlist[7]/2, cc.v2(470, 0));
  43. let finish1 = cc.callFunc(this.showendpic, this);
  44. let finish2 = cc.callFunc(this.endpk, this);
  45. //下半圆转弯
  46. let down_point0 = cc.v2(470, -300);
  47. let down_point1 = cc.v2(Math.cos(Math.PI / 8) * 470, -300 - Math.sin(Math.PI / 8) * 470);
  48. let down_point2 = cc.v2(Math.cos(Math.PI / 4) * 470, -300 - Math.sin(Math.PI / 4) * 470);
  49. let down_point3 = cc.v2(Math.cos(3 * Math.PI / 8) * 470, -300 - Math.sin(3 * Math.PI / 8) * 470);
  50. let down_point4 = cc.v2(Math.cos(Math.PI / 2) * 470, -300 - Math.sin(Math.PI / 2) * 470);
  51. let down_point5 = cc.v2(Math.cos(5 * Math.PI / 8) * 470, -300 - Math.sin(5 * Math.PI / 8) * 470);
  52. let down_point6 = cc.v2(Math.cos(6 * Math.PI / 8) * 470, -300 - Math.sin(6 * Math.PI / 8) * 470);
  53. let down_point7 = cc.v2(Math.cos(7 * Math.PI / 8) * 470, -300 - Math.sin(7 * Math.PI / 8) * 470);
  54. let down_point8 = cc.v2(- 470, -300);
  55. //上半圆
  56. let up_point0 = cc.v2(-470, 300);
  57. let up_point1 = cc.v2(Math.cos(Math.PI / 8) * -470, 300 + Math.sin(Math.PI / 8) * 470);
  58. let up_point2 = cc.v2(Math.cos(Math.PI / 4) * -470, 300 + Math.sin(Math.PI / 4) * 470);
  59. let up_point3 = cc.v2(Math.cos(3 * Math.PI / 8) * -470, 300 + Math.sin(3 * Math.PI / 8) * 470);
  60. let up_point4 = cc.v2(Math.cos(Math.PI / 2) * -470, 300 + Math.sin(Math.PI / 2) * 470);
  61. let up_point5 = cc.v2(Math.cos(5 * Math.PI / 8) * -470, 300 + Math.sin(5 * Math.PI / 8) * 470);
  62. let up_point6 = cc.v2(Math.cos(6 * Math.PI / 8) * -470, 300 + Math.sin(6 * Math.PI / 8) * 470);
  63. let up_point7 = cc.v2(Math.cos(7 * Math.PI / 8) * -470, 300 + Math.sin(7 * Math.PI / 8) * 470);
  64. let up_point8 = cc.v2(470, 300);
  65. let actionCircle1_1 = cc.spawn(cc.cardinalSplineTo(randomlist[3]*2.22, [down_point0,down_point1,down_point2,down_point3,down_point4,down_point5,down_point6,down_point7,down_point8], 0),cc.rotateBy(randomlist[3]*2.22, 180));
  66. let actionCircle1_2 = cc.spawn(cc.cardinalSplineTo(randomlist[4]*2.22, [up_point0,up_point1,up_point2,up_point3,up_point4,up_point5,up_point6,up_point7,up_point8], 0), cc.rotateBy(randomlist[4]*2.22, 180));
  67. let actionCircle2_1 = cc.spawn(cc.cardinalSplineTo(randomlist[8]*2.22, [down_point0,down_point1,down_point2,down_point3,down_point4,down_point5,down_point6,down_point7,down_point8], 0),cc.rotateBy(randomlist[8]*2.22, 180));
  68. let actionCircle2_2 = cc.spawn(cc.cardinalSplineTo(randomlist[9]*2.22, [up_point0,up_point1,up_point2,up_point3,up_point4,up_point5,up_point6,up_point7,up_point8], 0), cc.rotateBy(randomlist[9]*2.22, 180));
  69. let ActionAll = cc.sequence(placeAction,
  70. actionMove_1_1, actionCircle1_1, actionMove_1_2, actionCircle1_2, actionMove_1_3,finish1,
  71. actionMove_2_1, actionCircle2_1,actionMove_2_2,actionCircle2_2,actionMove_2_3,finish2
  72. );
  73. this.opposite_runnode.runAction(ActionAll);
  74. },
  75. MyCarAction(randomlist) {
  76. //初始位置
  77. let placeAction = cc.place(400, 0);
  78. //直线
  79. let actionMove_1_1 = cc.moveTo(randomlist[0]/2, cc.v2(400, -300));
  80. let actionMove_1_2 = cc.moveTo(randomlist[1], cc.v2(-400, 300));
  81. let actionMove_1_3 = cc.moveTo(randomlist[2]/2, cc.v2(400, 0));
  82. let actionMove_2_1 = cc.moveTo(randomlist[5]/2, cc.v2(400, -300));
  83. let actionMove_2_2 = cc.moveTo(randomlist[6], cc.v2(-400, 300));
  84. let actionMove_2_3 = cc.moveTo(randomlist[7]/2, cc.v2(400, 0));
  85. let finish1 = cc.callFunc(this.showendpic, this);
  86. let finish2 = cc.callFunc(this.endpk, this);
  87. //下半圆转弯
  88. let down_point0 = cc.v2(400, -300);
  89. let down_point1 = cc.v2(Math.cos(Math.PI / 8) * 400, -300 - Math.sin(Math.PI / 8) * 400);
  90. let down_point2 = cc.v2(Math.cos(Math.PI / 4) * 400, -300 - Math.sin(Math.PI / 4) * 400);
  91. let down_point3 = cc.v2(Math.cos(3 * Math.PI / 8) * 400, -300 - Math.sin(3 * Math.PI / 8) * 400);
  92. let down_point4 = cc.v2(Math.cos(Math.PI / 2) * 400, -300 - Math.sin(Math.PI / 2) * 400);
  93. let down_point5 = cc.v2(Math.cos(5 * Math.PI / 8) * 400, -300 - Math.sin(5 * Math.PI / 8) * 400);
  94. let down_point6 = cc.v2(Math.cos(6 * Math.PI / 8) * 400, -300 - Math.sin(6 * Math.PI / 8) * 400);
  95. let down_point7 = cc.v2(Math.cos(7 * Math.PI / 8) * 400, -300 - Math.sin(7 * Math.PI / 8) * 400);
  96. let down_point8 = cc.v2(- 400, -300);
  97. //上半圆
  98. let up_point0 = cc.v2(-400, 300);
  99. let up_point1 = cc.v2(Math.cos(Math.PI / 8) * -400, 300 + Math.sin(Math.PI / 8) * 400);
  100. let up_point2 = cc.v2(Math.cos(Math.PI / 4) * -400, 300 + Math.sin(Math.PI / 4) * 400);
  101. let up_point3 = cc.v2(Math.cos(3 * Math.PI / 8) * -400, 300 + Math.sin(3 * Math.PI / 8) * 400);
  102. let up_point4 = cc.v2(Math.cos(Math.PI / 2) * -400, 300 + Math.sin(Math.PI / 2) * 400);
  103. let up_point5 = cc.v2(Math.cos(5 * Math.PI / 8) * -400, 300 + Math.sin(5 * Math.PI / 8) * 400);
  104. let up_point6 = cc.v2(Math.cos(6 * Math.PI / 8) * -400, 300 + Math.sin(6 * Math.PI / 8) * 400);
  105. let up_point7 = cc.v2(Math.cos(7 * Math.PI / 8) * -400, 300 + Math.sin(7 * Math.PI / 8) * 400);
  106. let up_point8 = cc.v2(400, 300);
  107. let actionCircle1_1 = cc.spawn(cc.cardinalSplineTo(randomlist[3]*2.22, [down_point0,down_point1,down_point2,down_point3,down_point4,down_point5,down_point6,down_point7,down_point8], 0),cc.rotateBy(randomlist[3]*2.22, 180));
  108. let actionCircle1_2 = cc.spawn(cc.cardinalSplineTo(randomlist[4]*2.22, [up_point0,up_point1,up_point2,up_point3,up_point4,up_point5,up_point6,up_point7,up_point8], 0), cc.rotateBy(randomlist[4]*2.22, 180));
  109. let actionCircle2_1 = cc.spawn(cc.cardinalSplineTo(randomlist[8]*2.22, [down_point0,down_point1,down_point2,down_point3,down_point4,down_point5,down_point6,down_point7,down_point8], 0),cc.rotateBy(randomlist[8]*2.22, 180));
  110. let actionCircle2_2 = cc.spawn(cc.cardinalSplineTo(randomlist[9]*2.22, [up_point0,up_point1,up_point2,up_point3,up_point4,up_point5,up_point6,up_point7,up_point8], 0), cc.rotateBy(randomlist[9]*2.22, 180));
  111. let ActionAll = cc.sequence(placeAction,
  112. actionMove_1_1, actionCircle1_1, actionMove_1_2, actionCircle1_2, actionMove_1_3,finish1,
  113. actionMove_2_1, actionCircle2_1,actionMove_2_2,actionCircle2_2,actionMove_2_3,finish2
  114. );
  115. this.mycar_runnode.runAction(ActionAll);
  116. },
  117. /*
  118. @level:汽车等级
  119. */
  120. GetRandomList(level) {
  121. const DEFAULT_TIME = (3 / 5);
  122. const DEFAULT_UP_DOWM = 0.05;
  123. //每个段落的时间,在默认时间上下浮动5%;
  124. //每提升一级,默认时间缩短5%,以保证级大一级极高概率赢
  125. let Curr_level_default_time =DEFAULT_TIME * Math.pow(1-DEFAULT_UP_DOWM, level);
  126. let timelist = [];
  127. for (let i = 0; i < 10; i++){
  128. timelist.push((1-Math.random() * DEFAULT_UP_DOWM) * Curr_level_default_time);
  129. }
  130. return timelist;
  131. },
  132. sum(list) {
  133. let res = 0;
  134. list.forEach(element => {
  135. res += element;
  136. });
  137. return res;
  138. },
  139. showendpic() {
  140. this.scheduleOnce(function () {
  141. this.endpic.active = true;
  142. },0.3)
  143. },
  144. endpk() {
  145. //PK结束,我方胜利则发放奖金,对方胜利则显示失败。
  146. if (this.endcarnum == 0) {
  147. this.endcarnum += 1;
  148. return;
  149. }
  150. if (this.endcarnum == 1) {
  151. MyAudio.StopEffect_pkbgm();
  152. this.showinterad();
  153. Global.showbanner();
  154. this.node.active = true;
  155. this.pic_toast.active = true;
  156. let res = this.sum(this.my_list) - this.sum(this.opposite_list);
  157. let respicnode = cc.find('Canvas/BG/弹窗/BG/label');
  158. if (res < 0) {
  159. MyAudio.PlayEffect_pksuccess();
  160. respicnode.getComponent('languagesprite').updatelanguage(respicnode.getComponent(cc.Sprite), "你赢了");
  161. // cc.resources.load("automerge/你赢了", cc.SpriteFrame, function (err, spriteFrame) {
  162. // cc.find("Canvas/BG/弹窗/BG/label").getComponent(cc.Sprite).spriteFrame = spriteFrame;
  163. // });
  164. this.reword_number.string = Number(Storage.Get_storage(Storage.GameMessageType.buy_coin)) * 3;
  165. cc.find("Canvas/BG/弹窗/BG/fail_label").active = false;
  166. } else {
  167. MyAudio.PlayEffect_pkfail();
  168. this.reword_number.string = Number(Storage.Get_storage(Storage.GameMessageType.buy_coin));
  169. cc.find("Canvas/BG/弹窗/BG/fail_label").active = true;
  170. // console.log("对方胜利")
  171. respicnode.getComponent('languagesprite').updatelanguage(respicnode.getComponent(cc.Sprite), "你输了");
  172. //先判断小游戏平台,再判断原生平台
  173. if (cc.sys.platform === cc.sys.WECHAT_GAME) {
  174. return;
  175. }
  176. //调用广告,奖励3个汽车钱
  177. if (cc.sys.platform === cc.sys.ANDROID) {
  178. Admob.ShowInterstitialRewardAD();
  179. return;
  180. }
  181. }
  182. this.endcarnum = 0;
  183. }
  184. Global.EndRecode();
  185. },
  186. showinterad(){
  187. // 在适合的场景显示插屏广告
  188. let interstitialAd = Global.interstitialAd;
  189. if (interstitialAd) {
  190. interstitialAd.show().catch((err) => {
  191. console.error(err)
  192. })
  193. }
  194. }
  195. // update (dt) {},
  196. });