GameMode.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. var gameConfig = require("./GameConfig.js");
  2. var webView = require("../WebView");
  3. var lib = require("../Library");
  4. cc.Class({
  5. extends: cc.Component,
  6. properties: {
  7. gameStates: {
  8. default: null,
  9. type: cc.Node,
  10. serializable: true,
  11. },
  12. ui: {
  13. default: null,
  14. type: cc.Node,
  15. serializable: true,
  16. },
  17. player1: {
  18. default: null,
  19. type: cc.Node,
  20. serializable: true,
  21. },
  22. player2: {
  23. default: null,
  24. type: cc.Node,
  25. serializable: true,
  26. },
  27. canvas: {
  28. default: null,
  29. type: cc.Node,
  30. serializable: true,
  31. },
  32. readyGoAnim: {
  33. default: null,
  34. type: cc.Node,
  35. serializable: true,
  36. },
  37. handrailPrefab: {
  38. default: null,
  39. type: cc.Prefab,
  40. },
  41. starting: {
  42. default: null,
  43. type: cc.Node,
  44. serializable: true,
  45. },
  46. terminal: {
  47. default: null,
  48. type: cc.Node,
  49. serializable: true,
  50. },
  51. bgm: {
  52. default: null,
  53. type: cc.AudioSource,
  54. serializable: true,
  55. },
  56. Player1Arrow: {
  57. default: null,
  58. type: cc.Node,
  59. serializable: true,
  60. },
  61. startAudio: {
  62. default: null,
  63. type: cc.AudioClip,
  64. },
  65. },
  66. // LIFE-CYCLE CALLBACKS:
  67. onLoad() {
  68. this.init();
  69. this.initUI();
  70. //添加动画事件监听
  71. this.armatureDisplay.addEventListener(dragonBones.EventObject.COMPLETE, this.animationEventHandler, this);
  72. },
  73. init() {
  74. //playerControllerScript
  75. this.pConSt1 = this.player1.getComponent('BasePlayerController');
  76. this.pConSt2 = this.player2.getComponent('BasePlayerController');
  77. // because player controller onload event after game mode so use getChildByName to get PlayerStates
  78. this.pStatesSt1 = this.pConSt1.node.getChildByName('PlayerStates').getComponent('BasePlayerStates');
  79. this.pStatesSt2 = this.pConSt2.node.getChildByName('PlayerStates').getComponent('BasePlayerStates');
  80. //gameStatesScript
  81. this.gStatesSt = this.gameStates.getComponent('GameStates');
  82. this.armatureDisplay = this.readyGoAnim.getComponent(dragonBones.ArmatureDisplay);
  83. this.armature = this.armatureDisplay.armature();
  84. },
  85. initUI() {
  86. this.ui.zIndex = 10003;
  87. // init player information
  88. let playerUI1 = this.ui.getChildByName('Player1');
  89. let pUIName1 = playerUI1.getChildByName('NameBG').getChildByName('Name');
  90. let pUIAvatar1 = playerUI1.getChildByName('Mask').getChildByName('Avatar');
  91. let pUIGender1 = playerUI1.getChildByName('Gender');
  92. let playerUI2 = this.ui.getChildByName('Player2');
  93. let pUIName2 = playerUI2.getChildByName('NameBG').getChildByName('Name');
  94. let pUIAvatar2 = playerUI2.getChildByName('Mask').getChildByName('Avatar');
  95. let pUIGender2 = playerUI2.getChildByName('Gender');
  96. let setGender = function (genderNode, bMale) {
  97. if (bMale) {
  98. genderNode.getChildByName('Female').active = false;
  99. genderNode.getChildByName('Male').active = true;
  100. } else {
  101. genderNode.getChildByName('Female').active = true;
  102. genderNode.getChildByName('Male').active = false;
  103. }
  104. };
  105. pUIName1.getComponent(cc.Label).string = webView.userName;
  106. pUIName2.getComponent(cc.Label).string = webView.rivalUserName;
  107. lib.setImageBase64(webView.avatarBase64, function (texture2D) {
  108. pUIAvatar1.getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(texture2D);
  109. });
  110. lib.setImageBase64(webView.rivalavatarBase64, function (texture2D) {
  111. pUIAvatar2.getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(texture2D);
  112. });
  113. setGender(pUIGender1, webView.gender);
  114. setGender(pUIGender2, webView.rivalGender);
  115. },
  116. start()
  117. {
  118. //because player state have not be created so this code write start function
  119. //currentStartPositionX from start line(720,0) not (0,0)
  120. let cStartPX = 720;
  121. let hPSetArr = gameConfig.handrailPosSetArr;
  122. for(let i = 0;i<hPSetArr.length;i++)
  123. {
  124. let space = hPSetArr[i].handrailSpace;
  125. for(let j = 0;j<hPSetArr[i].handrailNum;j++)
  126. {
  127. cStartPX+=space;
  128. this.pStatesSt1.handrailArr.push({"startPX":(cStartPX),
  129. 'bFallDown':0});
  130. this.pStatesSt2.handrailArr.push({"startPX":(cStartPX),
  131. 'bFallDown':0});
  132. }
  133. }
  134. this.terminal.x = cStartPX + 720*2;
  135. },
  136. animationEventHandler(event)
  137. {
  138. if (event.type === dragonBones.EventObject.COMPLETE)
  139. {
  140. if (event.animationState.name === "animation")
  141. {
  142. // Debug.Log("attack 动作播放完毕!!!");
  143. this.startGame();
  144. this.bgm.getComponent(cc.AudioSource).play();
  145. }
  146. }
  147. },
  148. startGame()
  149. {
  150. cc.audioEngine.playEffect(this.startAudio, 0, function () {});
  151. this.resetZindex();
  152. this.playerRun();
  153. for (let i = 0; i < 2; i++)
  154. {
  155. this.createHandrail();
  156. }
  157. this.pStatesSt1.nextHandrail = this.gStatesSt.drawedhandrailArr[0].handrail;
  158. //count game time
  159. let interval = 1;  // 以秒为单位的时间间隔let
  160. let repeat = cc.macro.REPEAT_FOREVER;  // 重复次数
  161. let delay = 0;  // 开始延时
  162. this.schedule(this.countGameTime, interval, repeat, delay);
  163. //destroy Arrow on the player top
  164. this.schedule(function() {
  165. this.Player1Arrow.destroy();
  166. }, 5, 0, 0);
  167. },
  168. countGameTime()
  169. {
  170. this.gStatesSt.gameTime++;
  171. },
  172. createHandrail()
  173. {
  174. if (this.gStatesSt.currentHandrailIndex == this.pStatesSt1.handrailArr.length) return -1;
  175. //get new handrail position x
  176. let currentLevel = gameConfig.handrailPosSetArr[this.gStatesSt.currentHandrailPositionLevel];
  177. let positionSpace = currentLevel.handrailSpace;
  178. let handrailPos = this.gStatesSt.lastHandrailPosition + positionSpace;
  179. this.gStatesSt.lastHandrailPosition = handrailPos;
  180. //create handrail
  181. let res = this.handrailPrefab;
  182. let handrail = cc.instantiate(res);
  183. handrail.x = handrailPos + this.gStatesSt.startLinePX;
  184. handrail.zIndex = 1;
  185. this.canvas.addChild(handrail);
  186. this.gStatesSt.drawedhandrailArr.push({'handrail':handrail,
  187. index:this.gStatesSt.currentHandrailIndex});
  188. //set player2 have not drawed hurndrail state
  189. let handrail2 = handrail.getChildByName('Kuolan2');
  190. if(this.pStatesSt2.handrailArr[this.gStatesSt.currentHandrailIndex].bFallDown)
  191. {
  192. let spine = handrail2.getComponent(sp.Skeleton);
  193. spine.setAnimation(0, 'Idl1_3', false);
  194. }
  195. //count current handrailNum and
  196. this.gStatesSt.currentHandrailIndex++;
  197. this.gStatesSt.currentHandrailIndexInLevel++;
  198. if (this.gStatesSt.currentHandrailIndexInLevel == currentLevel.handrailNum)
  199. {
  200. this.gStatesSt.currentHandrailIndexInLevel = 0;
  201. this.gStatesSt.currentHandrailPositionLevel++;
  202. }
  203. return handrail.x;
  204. },
  205. resetZindex() {
  206. this.pConSt1.resetZindex();
  207. this.pConSt2.resetZindex();
  208. },
  209. playerRun() {
  210. this.pConSt1.run();
  211. this.pConSt2.run();
  212. },
  213. replay()
  214. {
  215. this.schedule(function() {
  216. cc.director.loadScene("Game");
  217. }, 0.1, 0, 0);
  218. }
  219. });