GameMode.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. var gameConfig = require("./GameConfig.js");
  2. var lib = require("../Library");
  3. var globalConfig = require("../Global");
  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. this.ctorSt1 = this.pConSt1.node.getChildByName('Charactor').getComponent('BaseCharactor');
  81. this.ctorSt2 = this.pConSt2.node.getChildByName('Charactor').getComponent('BaseCharactor');
  82. //gameStatesScript
  83. this.gStatesSt = this.gameStates.getComponent('GameStates');
  84. this.armatureDisplay = this.readyGoAnim.getComponent(dragonBones.ArmatureDisplay);
  85. this.armature = this.armatureDisplay.armature();
  86. },
  87. initUI() {
  88. this.ui.zIndex = 10003;
  89. // init player information
  90. let playerUI1 = this.ui.getChildByName('Player1');
  91. let pUIName1 = playerUI1.getChildByName('NameBG').getChildByName('Name');
  92. let pUIAvatar1 = playerUI1.getChildByName('Mask').getChildByName('Avatar');
  93. let pUIGender1 = playerUI1.getChildByName('Gender');
  94. let playerUI2 = this.ui.getChildByName('Player2');
  95. let pUIName2 = playerUI2.getChildByName('NameBG').getChildByName('Name');
  96. let pUIAvatar2 = playerUI2.getChildByName('Mask').getChildByName('Avatar');
  97. let pUIGender2 = playerUI2.getChildByName('Gender');
  98. let setGender = function (genderNode, bMale) {
  99. if (bMale) {
  100. genderNode.getChildByName('Female').active = false;
  101. genderNode.getChildByName('Male').active = true;
  102. } else {
  103. genderNode.getChildByName('Female').active = true;
  104. genderNode.getChildByName('Male').active = false;
  105. }
  106. };
  107. pUIName1.getComponent(cc.Label).string = globalConfig.Name;
  108. pUIName2.getComponent(cc.Label).string = globalConfig.rivalName;
  109. lib.setImageBase64(globalConfig.avatarBase64, function (texture2D) {
  110. pUIAvatar1.getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(texture2D);
  111. });
  112. lib.setImageBase64(globalConfig.rivalAvatarBase64, function (texture2D) {
  113. pUIAvatar2.getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(texture2D);
  114. });
  115. setGender(pUIGender1, globalConfig.gender);
  116. setGender(pUIGender2, globalConfig.rivalGender);
  117. },
  118. start()
  119. {
  120. //because player state have not be created so this code write start function
  121. //currentStartPositionX from start line(720,0) not (0,0)
  122. //跨栏总数是两个部分组 和同样间隔的跨栏组 和当组里面创建出来多少跨栏
  123. let cStartPX = 720;
  124. let hPSetArr = gameConfig.handrailPosSetArr;
  125. for(let i = 0;i<hPSetArr.length;i++)
  126. {
  127. let space = hPSetArr[i].handrailSpace;
  128. for(let j = 0;j<hPSetArr[i].handrailNum;j++)
  129. {
  130. cStartPX+=space;
  131. this.pStatesSt1.handrailArr.push({"startPX":(cStartPX),
  132. 'bFallDown':0});
  133. this.pStatesSt2.handrailArr.push({"startPX":(cStartPX),
  134. 'bFallDown':0});
  135. }
  136. }
  137. this.terminal.x = cStartPX + 720*2;
  138. },
  139. animationEventHandler(event)
  140. {
  141. if (event.type === dragonBones.EventObject.COMPLETE)
  142. {
  143. if (event.animationState.name === "animation")
  144. {
  145. // Debug.Log("attack 动作播放完毕!!!");
  146. this.startGame();
  147. this.bgm.getComponent(cc.AudioSource).play();
  148. }
  149. }
  150. },
  151. startGame()
  152. {
  153. cc.audioEngine.playEffect(this.startAudio, 0, function () {});
  154. this.resetZindex();
  155. this.playerRun();
  156. //如果总共要创建0个跨栏就不创建,如果1个就创建一个,如果2个以上就创建两个
  157. //之所以创建两个是因为不换穿帮,1个可以看到前面的跨栏突然生产
  158. if(this.pStatesSt1.handrailArr.length == 1)
  159. {
  160. this.createHandrail();
  161. }
  162. else if(this.pStatesSt1.handrailArr.length > 1)
  163. {
  164. for(let i = 0;i<2;i++)
  165. {
  166. this.createHandrail();
  167. }
  168. }
  169. this.pStatesSt1.nextHandrail = this.gStatesSt.drawedhandrailArr[0].handrail;
  170. //count game time
  171. let interval = 1; // 以秒为单位的时间间隔let
  172. let repeat = cc.macro.REPEAT_FOREVER; // 重复次数
  173. let delay = 0; // 开始延时
  174. this.schedule(this.countGameTime, interval, repeat, delay);
  175. //destroy Arrow on the player top
  176. this.schedule(function() {
  177. this.Player1Arrow.destroy();
  178. }, 5, 0, 0);
  179. },
  180. countGameTime()
  181. {
  182. this.gStatesSt.gameTime++;
  183. },
  184. createHandrail()
  185. {
  186. if (this.gStatesSt.currentHandrailIndex == this.pStatesSt1.handrailArr.length &&
  187. this.gStatesSt.currentHandrailIndex !=0)
  188. {
  189. return -1;
  190. }
  191. //get new handrail position x
  192. let currentLevel = gameConfig.handrailPosSetArr[this.gStatesSt.currentHandrailPositionLevel];
  193. let positionSpace = currentLevel.handrailSpace;
  194. let handrailPos = this.gStatesSt.lastHandrailPosition + positionSpace;
  195. this.gStatesSt.lastHandrailPosition = handrailPos;
  196. //create handrail
  197. let res = this.handrailPrefab;
  198. let handrail = cc.instantiate(res);
  199. handrail.x = handrailPos + this.gStatesSt.startLinePX;
  200. handrail.zIndex = 1;
  201. this.canvas.addChild(handrail);
  202. this.gStatesSt.drawedhandrailArr.push({'handrail':handrail,
  203. index:this.gStatesSt.currentHandrailIndex});
  204. //set player2 have not drawed hurndrail state
  205. let handrail2 = handrail.getChildByName('Kuolan2');
  206. if(this.pStatesSt2.handrailArr[this.gStatesSt.currentHandrailIndex].bFallDown)
  207. {
  208. let spine = handrail2.getComponent(sp.Skeleton);
  209. spine.setAnimation(0, 'Idl1_3', false);
  210. }
  211. //count current handrailNum and
  212. this.gStatesSt.currentHandrailIndex++;
  213. this.gStatesSt.currentHandrailIndexInLevel++;
  214. if (this.gStatesSt.currentHandrailIndexInLevel == currentLevel.handrailNum)
  215. {
  216. this.gStatesSt.currentHandrailIndexInLevel = 0;
  217. this.gStatesSt.currentHandrailPositionLevel++;
  218. }
  219. return handrail.x;
  220. },
  221. resetZindex() {
  222. this.pConSt1.resetZindex();
  223. this.pConSt2.resetZindex();
  224. },
  225. playerRun() {
  226. this.pConSt1.currentState = this.pStatesSt1.playerAnimState.run;
  227. this.ctorSt1.run('idle');
  228. this.pConSt2.currentState = this.pStatesSt2.playerAnimState.run;
  229. this.ctorSt2.run('idle');
  230. },
  231. replay()
  232. {
  233. this.schedule(function() {
  234. cc.director.loadScene("Game");
  235. }, 0.1, 0, 0);
  236. }
  237. });