GameMode.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. let Self = this;
  75. //playerControllerScript
  76. this.pConSt1 = this.player1.getComponent('BasePlayerController');
  77. this.pConSt2 = this.player2.getComponent('BasePlayerController');
  78. // because player controller onload event after game mode so use getChildByName to get PlayerStates
  79. this.pStatesSt1 = this.pConSt1.node.getChildByName('PlayerStates').getComponent('BasePlayerStates');
  80. this.pStatesSt2 = this.pConSt2.node.getChildByName('PlayerStates').getComponent('BasePlayerStates');
  81. this.ctorSt1 = this.pConSt1.node.getChildByName('Charactor').getComponent('BaseCharactor');
  82. this.ctorSt2 = this.pConSt2.node.getChildByName('Charactor').getComponent('BaseCharactor');
  83. if (!globalConfig.bAi) {
  84. server.on('onRecvLockStep',function(data){
  85. Self.onRecvLockStep(data);
  86. });
  87. }
  88. //gameStatesScript
  89. this.gStatesSt = this.gameStates.getComponent('GameStates');
  90. this.armatureDisplay = this.readyGoAnim.getComponent(dragonBones.ArmatureDisplay);
  91. this.armature = this.armatureDisplay.armature();
  92. },
  93. initUI() {
  94. this.ui.zIndex = 10003;
  95. // init player information
  96. let playerUI1 = this.ui.getChildByName('Player1');
  97. let pUIName1 = playerUI1.getChildByName('NameBG').getChildByName('Name');
  98. let pUIAvatar1 = playerUI1.getChildByName('Mask').getChildByName('Avatar');
  99. let pUIGender1 = playerUI1.getChildByName('Gender');
  100. let playerUI2 = this.ui.getChildByName('Player2');
  101. let pUIName2 = playerUI2.getChildByName('NameBG').getChildByName('Name');
  102. let pUIAvatar2 = playerUI2.getChildByName('Mask').getChildByName('Avatar');
  103. let pUIGender2 = playerUI2.getChildByName('Gender');
  104. let setGender = function (genderNode, bMale) {
  105. if (bMale) {
  106. genderNode.getChildByName('Female').active = false;
  107. genderNode.getChildByName('Male').active = true;
  108. } else {
  109. genderNode.getChildByName('Female').active = true;
  110. genderNode.getChildByName('Male').active = false;
  111. }
  112. };
  113. pUIName1.getComponent(cc.Label).string = globalConfig.Name;
  114. pUIName2.getComponent(cc.Label).string = globalConfig.rivalName;
  115. lib.setImageBase64(globalConfig.avatarBase64, function (texture2D) {
  116. pUIAvatar1.getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(texture2D);
  117. });
  118. lib.setImageBase64(globalConfig.rivalAvatarBase64, function (texture2D) {
  119. pUIAvatar2.getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(texture2D);
  120. });
  121. setGender(pUIGender1, globalConfig.gender);
  122. setGender(pUIGender2, globalConfig.rivalGender);
  123. },
  124. start()
  125. {
  126. //because player state have not be created so this code write start function
  127. //currentStartPositionX from start line(720,0) not (0,0)
  128. //跨栏总数是两个部分组 和同样间隔的跨栏组 和当组里面创建出来多少跨栏
  129. let cStartPX = 720;
  130. let hPSetArr = gameConfig.handrailPosSetArr;
  131. for(let i = 0;i<hPSetArr.length;i++)
  132. {
  133. let space = hPSetArr[i].handrailSpace;
  134. for(let j = 0;j<hPSetArr[i].handrailNum;j++)
  135. {
  136. cStartPX+=space;
  137. this.pStatesSt1.handrailArr.push({"startPX":(cStartPX),
  138. 'bFallDown':0});
  139. this.pStatesSt2.handrailArr.push({"startPX":(cStartPX),
  140. 'bFallDown':0});
  141. }
  142. }
  143. this.terminal.x = cStartPX + 720*2;
  144. },
  145. animationEventHandler(event)
  146. {
  147. if (event.type === dragonBones.EventObject.COMPLETE)
  148. {
  149. if (event.animationState.name === "animation")
  150. {
  151. // Debug.Log("attack 动作播放完毕!!!");
  152. this.startGame();
  153. this.bgm.getComponent(cc.AudioSource).play();
  154. }
  155. }
  156. },
  157. startGame()
  158. {
  159. cc.audioEngine.playEffect(this.startAudio, 0, function () {});
  160. this.resetZindex();
  161. this.playerRun();
  162. //如果总共要创建0个跨栏就不创建,如果1个就创建一个,如果2个以上就创建两个
  163. //之所以创建两个是因为不换穿帮,1个可以看到前面的跨栏突然生产
  164. if(this.pStatesSt1.handrailArr.length == 1)
  165. {
  166. this.createHandrail();
  167. }
  168. else if(this.pStatesSt1.handrailArr.length > 1)
  169. {
  170. for(let i = 0;i<2;i++)
  171. {
  172. this.createHandrail();
  173. }
  174. }
  175. this.pStatesSt1.nextHandrail = this.gStatesSt.drawedhandrailArr[0].handrail;
  176. //count game time
  177. let interval = 1; // 以秒为单位的时间间隔let
  178. let repeat = cc.macro.REPEAT_FOREVER; // 重复次数
  179. let delay = 0; // 开始延时
  180. this.schedule(this.countGameTime, interval, repeat, delay);
  181. //destroy Arrow on the player top
  182. this.schedule(function() {
  183. this.Player1Arrow.destroy();
  184. }, 5, 0, 0);
  185. },
  186. countGameTime()
  187. {
  188. this.gStatesSt.gameTime++;
  189. },
  190. createHandrail()
  191. {
  192. if (this.gStatesSt.currentHandrailIndex == this.pStatesSt1.handrailArr.length &&
  193. this.gStatesSt.currentHandrailIndex !=0)
  194. {
  195. return -1;
  196. }
  197. //get new handrail position x
  198. let currentLevel = gameConfig.handrailPosSetArr[this.gStatesSt.currentHandrailPositionLevel];
  199. let positionSpace = currentLevel.handrailSpace;
  200. let handrailPos = this.gStatesSt.lastHandrailPosition + positionSpace;
  201. this.gStatesSt.lastHandrailPosition = handrailPos;
  202. //create handrail
  203. let res = this.handrailPrefab;
  204. let handrail = cc.instantiate(res);
  205. handrail.x = handrailPos + this.gStatesSt.startLinePX;
  206. handrail.zIndex = 1;
  207. this.canvas.addChild(handrail);
  208. this.gStatesSt.drawedhandrailArr.push({'handrail':handrail,
  209. index:this.gStatesSt.currentHandrailIndex});
  210. //set player2 have not drawed hurndrail state
  211. let handrail2 = handrail.getChildByName('Kuolan2');
  212. if(this.pStatesSt2.handrailArr[this.gStatesSt.currentHandrailIndex].bFallDown)
  213. {
  214. let spine = handrail2.getComponent(sp.Skeleton);
  215. spine.setAnimation(0, 'Idl1_3', false);
  216. }
  217. //count current handrailNum and
  218. this.gStatesSt.currentHandrailIndex++;
  219. this.gStatesSt.currentHandrailIndexInLevel++;
  220. if (this.gStatesSt.currentHandrailIndexInLevel == currentLevel.handrailNum)
  221. {
  222. this.gStatesSt.currentHandrailIndexInLevel = 0;
  223. this.gStatesSt.currentHandrailPositionLevel++;
  224. }
  225. return handrail.x;
  226. },
  227. resetZindex() {
  228. this.pConSt1.resetZindex();
  229. this.pConSt2.resetZindex();
  230. },
  231. playerRun() {
  232. this.pConSt1.currentState = this.pStatesSt1.playerAnimState.run;
  233. this.ctorSt1.run('idle');
  234. this.pConSt2.currentState = this.pStatesSt2.playerAnimState.run;
  235. this.ctorSt2.run('idle');
  236. },
  237. replay()
  238. {
  239. this.schedule(function() {
  240. cc.director.loadScene("Game");
  241. }, 0.1, 0, 0);
  242. },
  243. onRecvLockStep(data){
  244. //看看是否数组空,数组空证明角色停下来了,需要执行动画
  245. let bDoAction = false;
  246. if(this.pStatesSt1.length == 0)
  247. {
  248. bDoAction = true;
  249. }
  250. //把新数组里面的动作灌入动作执行数组
  251. let player1_order_arr = data.order[globalConfig.openid];
  252. for(let i=0;i<player1_order_arr.length;i++){
  253. let order = player1_order_arr[i];
  254. this.pStatesSt1.actionArr.push(order);
  255. }
  256. let player2_order_arr = data.order[globalConfig.rivalOpenid];
  257. for(let i=0;i<player2_order_arr.length;i++){
  258. let order = player2_order_arr[i];
  259. this.pStatesSt2.actionArr.push(order);
  260. }
  261. //数组空执行新动作
  262. if(!bDoAction) return;
  263. let player1ActionName = this.pStatesSt1.actionArr.shift();
  264. if(player1ActionName == 'speedUp')
  265. {
  266. this.pConSt1.speedUp();
  267. // console.log('speedUp1')
  268. }
  269. else if(player1ActionName == 'jump')
  270. {
  271. this.pConSt1.jump();
  272. }
  273. let player2ActionName = this.pStatesSt2.actionArr.shift();
  274. if(player2ActionName == 'speedUp')
  275. {
  276. this.pConSt2.speedUp();
  277. // console.log('speedUp2')
  278. }
  279. else if(player2ActionName == 'jump')
  280. {
  281. this.pConSt2.jump();
  282. }
  283. },
  284. onRivalQuitGame(){
  285. }
  286. });