PlayerController.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. var gameConfig = require("../GameConfig.js");
  2. var webView = require("../../WebView");
  3. var globalConfig = require("../../Global");
  4. var lib = require("../../Library");
  5. var EquipmentAction = require("../../EquipmentAction");
  6. var lockStepClient = require("../../LockStepClient");
  7. cc.Class({
  8. extends: require("BasePlayerController"),
  9. properties: {
  10. mainCamera: {
  11. default: null,
  12. type: cc.Node,
  13. serializable: true,
  14. },
  15. mapArr: {
  16. default: [],
  17. type: [cc.Node], // type 同样写成数组,提高代码可读性
  18. },
  19. locationLine: {
  20. default: null,
  21. type: cc.Node,
  22. serializable: true,
  23. },
  24. staggerAudio: {
  25. default: null,
  26. type: cc.AudioClip,
  27. },
  28. resultBar: {
  29. default: null,
  30. type: cc.Node,
  31. serializable: true,
  32. },
  33. cheerAudio: {
  34. default: null,
  35. type: cc.AudioClip,
  36. },
  37. rival: {
  38. default: null,
  39. type: cc.Node,
  40. serializable: true,
  41. },
  42. //设备数据判断对象
  43. equipmentObj: null,
  44. },
  45. init() {//override
  46. this._super();
  47. this.locationLine.zIndex = 2;
  48. this.resultBarSt = this.resultBar.getComponent('ResultBar');
  49. },
  50. start()//override
  51. {
  52. this._super();
  53. if (lib.openInWebview()) {
  54. // 在app内Webview打开
  55. webView.register(this.node);
  56. this.node.on('onDeviceUpdateData', this.onDeviceUpdateData, this);
  57. //监听回调
  58. uni.postMessage({
  59. data: {
  60. funName: "addDeviceUpdateListener",
  61. gameData: {}
  62. }
  63. })
  64. //开启设备回调
  65. uni.postMessage({
  66. data: {
  67. funName: "writeBLEConnectionValue",
  68. gameData: {
  69. value: "3" //开启设备数据
  70. }
  71. }
  72. })
  73. //开启设备频率
  74. uni.postMessage({
  75. data: {
  76. funName: "writeBLEConnectionValue",
  77. gameData: {
  78. value: "b" //60ms
  79. }
  80. }
  81. })
  82. this.equipmentObj = new EquipmentAction();
  83. this.equipmentObj.addEventListener("resultant", (res) => {
  84. let data = res;
  85. // console.log("resultant:",data);
  86. if(data.type == "jump"){
  87. this.jump();
  88. }else if(data.type == "runing"){
  89. this.speedUp();
  90. }
  91. })
  92. }
  93. },
  94. update(dt) {//override
  95. this._super();
  96. //make ui and camera follow the player
  97. this.gmSt.ui.x = this.node.x;
  98. this.mainCamera.x = this.node.x;
  99. if (this.pStatesSt.currentProState == this.pStatesSt.pProStates.end) return;
  100. //indicator
  101. if (this.node.x - this.rival.x > 360)//forward show left bar
  102. {
  103. this.ctorSt.showIndicater(1, Math.abs(Math.round(this.node.x - this.rival.x - 38)));
  104. }
  105. else if (this.node.x - this.rival.x < -360)//backward show right bar
  106. {
  107. this.ctorSt.showIndicater(0, Math.abs(Math.round(this.node.x - this.rival.x + 38)));
  108. }
  109. else {
  110. this.ctorSt.hideIndicater();
  111. }
  112. //生成主角前方地图
  113. this.switchMap();
  114. },
  115. switchMap() {
  116. if (this.node.x > this.pStatesSt.targetPoint) {
  117. this.mapArr[0].x += 2160;
  118. lib.swapLeft(this.mapArr);
  119. this.pStatesSt.targetPoint += 720;
  120. }
  121. },
  122. setEndSpeed() {//overwrite
  123. this.pStatesSt.intoTheEndSpeed = this.pStatesSt.currentSpeed / (this.gmSt.terminal.x - this.pStatesSt.endLineStopPoint);
  124. },
  125. passedHandrail()//overwrite
  126. {
  127. let handrail = this.pStatesSt.nextHandrail.getChildByName('Kuolan1');
  128. let spine = handrail.getComponent(sp.Skeleton);
  129. spine.setAnimation(0, 'Idl1_3', false);
  130. this.ctorSt.stagger(function () {
  131. this.pStatesSt.Combo = 0;
  132. this.createAndDestroyHandrail();
  133. }.bind(this));
  134. cc.audioEngine.playEffect(this.staggerAudio, 0, function () { });
  135. },
  136. createAndDestroyHandrail() {
  137. while (true) {
  138. let hurdrailPX = this.gmSt.createHandrail();
  139. if (hurdrailPX - this.node.x > 360 || hurdrailPX == -1) {
  140. break;
  141. }
  142. }
  143. //GC drawedhandrail 如果出了玩家1的视野再回收
  144. if (this.node.x - this.gStatesSt.drawedhandrailArr[0].x > 720)
  145. {
  146. let deleteHandrail = this.gStatesSt.drawedhandrailArr.shift();
  147. this.gStatesSt.handrailGCArr.push(deleteHandrail);
  148. }
  149. //set next jump handrail设置下一个要跨越的跨栏
  150. if (this.gStatesSt.drawedhandrailArr.length != 0) {
  151. let nextHandrailIndex = -1;
  152. for(let i = 0;i<this.gStatesSt.drawedhandrailArr.length;i++)
  153. {
  154. //一开始nextHandrail是当前跨栏,当数组里找到当前跨栏后,它下一位的跨栏就是下一个跨栏
  155. //for now nextHandrail is current handrail, so if find current handrail in array means next index is next handrial
  156. if(this.pStatesSt.nextHandrail == this.gStatesSt.drawedhandrailArr[i].handrail)
  157. {
  158. nextHandrailIndex = i;
  159. break;
  160. }
  161. }
  162. //最后一跨栏就不设置了
  163. //if current handrail is last handrail do not set next handrail
  164. if(nextHandrailIndex!=-1 && this.gStatesSt.drawedhandrailArr.length >1)
  165. {
  166. this.pStatesSt.nextHandrail = this.gStatesSt.drawedhandrailArr[nextHandrailIndex+1].handrail;
  167. }
  168. }
  169. //destroy hurdrail
  170. let len = this.gStatesSt.handrailGCArr.length;
  171. for (let i = 0; i < len; i++) {
  172. if (this.node.x - this.gStatesSt.handrailGCArr[i].x > 720) {
  173. let handrail = this.gStatesSt.handrailGCArr[i];
  174. delete this.gStatesSt.handrailGCArr[i];
  175. handrail.destroy();
  176. }
  177. }
  178. // because delete array will left undefined element so we need to remove it from array
  179. for (let i = 0; i < len; i++) {
  180. if (this.gStatesSt.handrailGCArr[i] == undefined) {
  181. this.gStatesSt.handrailGCArr.splice(i);
  182. }
  183. }
  184. },
  185. enterEndline() {//override
  186. this._super();
  187. if (this.gStatesSt.arrEndPArr.length == 1) {
  188. this.scheduleOnce(function () {
  189. this.setResuletBar();
  190. }, 3);
  191. this.firework();
  192. }
  193. else {
  194. this.schedule(function () {
  195. this.resultBar.active = true;
  196. this.resultBar.getChildByName('Win').active = false;
  197. this.resultBar.getChildByName('Fail').active = true;
  198. this.setResuletBar();
  199. }, 1, 0, 0);
  200. }
  201. },
  202. setResuletBar() {
  203. this.resultBarSt.init();
  204. this.gmSt.unschedule(this.gmSt.countGameTime);
  205. this.resultBarSt.updateCostTime(this.gStatesSt.gameTime);
  206. this.resultBarSt.updateAvatar();
  207. //cal calorie
  208. let runUnit = 0;
  209. let jumpUnit = 0;
  210. let calorie = 0;
  211. if( globalConfig.calorieParams != null)
  212. {
  213. runUnit = globalConfig.calorieParams.hitUnit;
  214. jumpUnit = globalConfig.calorieParams.hitUnit;
  215. calorie = parseInt(this.pStatesSt.runTimes * runUnit + this.pStatesSt.jumpTimes * jumpUnit);
  216. }
  217. this.resultBarSt.updateCalorie(calorie);
  218. if (lib.openInWebview()) {
  219. webView.postMessage(100, this.gmSt.countGameTime, calorie);
  220. }
  221. },
  222. firework() {
  223. cc.audioEngine.playEffect(this.cheerAudio, 0, function () { });
  224. let firework = this.gmSt.terminal.getChildByName("Firework");
  225. firework.active = true;
  226. firework.zIndex = 1005;
  227. },
  228. speedUp(){
  229. this.speedUp_client();
  230. this._super();
  231. },
  232. speedUp_client(){
  233. if (!globalConfig.bAi) {
  234. let obj = {};
  235. obj.type = 'message';
  236. obj.message = 'speedUp';
  237. lockStepClient.sendMessage( globalConfig.openid,JSON.stringify(obj));
  238. }
  239. },
  240. jump() {//overwrite
  241. //super class return is not effect to sub class so we need to write if return every subclass
  242. // if (this.gStatesSt.drawedhandrailArr.length == 0) return;
  243. if (this.pStatesSt.currentState == this.pStatesSt.playerAnimState.jump) return;
  244. //init locationLine do once
  245. this.locationLine.opacity = 0;
  246. this.locationLine.x = this.node.x;
  247. //No perfect jump area
  248. if (this.locationLine.x > this.pStatesSt.nextHandrail.x &&
  249. this.locationLine.x < this.pStatesSt.nextHandrail.x + gameConfig.handrailArea1.end) {
  250. this.locationLine.opacity = 255;
  251. this.pStatesSt.currentState = this.pStatesSt.playerAnimState.jump;
  252. this.ctorSt.jump('hurdling_1', function () {
  253. this.pStatesSt.currentState = this.pStatesSt.playerAnimState.run;
  254. this.createAndDestroyHandrail();
  255. this.pStatesSt.passedHurdrailNum++;
  256. this.pStatesSt.jumpTimes++;
  257. this.run();
  258. }.bind(this),1);
  259. if (!globalConfig.bAi) {
  260. let obj = {};
  261. obj.type = 'message';
  262. obj.message = 'jump';
  263. lockStepClient.sendMessage( globalConfig.openid,JSON.stringify(obj));
  264. }
  265. return;
  266. }
  267. //perfect jump area
  268. if (this.locationLine.x > this.pStatesSt.nextHandrail.x + gameConfig.handrailArea2.start &&
  269. this.locationLine.x < this.pStatesSt.nextHandrail.x + gameConfig.handrailArea2.end) {
  270. this.pStatesSt.nextHandrail.getChildByName('AccelerationBandCenter').destroy();
  271. this.locationLine.opacity = 255;
  272. this.pStatesSt.currentState = this.pStatesSt.playerAnimState.jump;
  273. this.ctorSt.jump('hurdling_1', function () {
  274. this.pStatesSt.currentState = this.pStatesSt.playerAnimState.run;
  275. this.createAndDestroyHandrail();
  276. this.pStatesSt.passedHurdrailNum++;
  277. this.pStatesSt.jumpTimes++;
  278. this.run();
  279. }.bind(this),1);
  280. if (!globalConfig.bAi) {
  281. let obj = {};
  282. obj.type = 'message';
  283. obj.message = 'jump';
  284. lockStepClient.sendMessage( globalConfig.openid,JSON.stringify(obj));
  285. }
  286. return;
  287. }
  288. // this.pStatesSt.currentState = this.pStatesSt.playerAnimState.jump;
  289. // this.locationLine.opacity = 255;
  290. // this.ctorSt.jump('hurdling_1', function () {
  291. // this.pStatesSt.currentState = this.pStatesSt.playerAnimState.run;
  292. // // this.createAndDestroyHandrail();
  293. // // this.pStatesSt.passedHurdrailNum++;
  294. // // this.pStatesSt.jumpTimes++;
  295. // }.bind(this),0);
  296. // if (lib.openInWebview() && !globalConfig.bAi) {
  297. // mgobe.sendFrame('jump');
  298. // }
  299. },
  300. //interact with device
  301. onDeviceUpdateData(data) {
  302. // console.log(data);
  303. //判断数据类型
  304. if (data.dataType == "Box") {
  305. // webView.onBLEBoxUpdate(data);
  306. let gameData = data.data;
  307. this.equipmentObj.updateJumpAndRun({
  308. acc: gameData.acc,
  309. gyro: gameData.gyro,
  310. bLimitRebound: false
  311. })
  312. }
  313. },
  314. });
  315. // mgobe