PlayerController.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. var gameConfig = require("./GameConfig.js");
  2. var webView = require("../WebView");
  3. var lib = require("../Library");
  4. var mgobe = require("../Mgobe");
  5. var EquipmentAction = require("../EquipmentAction");
  6. cc.Class({
  7. extends: require("BasePlayerController"),
  8. properties: {
  9. mainCamera: {
  10. default: null,
  11. type: cc.Node,
  12. serializable: true,
  13. },
  14. mapArr: {
  15. default: [],
  16. type: [cc.Node], // type 同样写成数组,提高代码可读性
  17. },
  18. locationLine: {
  19. default: null,
  20. type: cc.Node,
  21. serializable: true,
  22. },
  23. staggerAudio: {
  24. default: null,
  25. type: cc.AudioClip,
  26. },
  27. resultBar: {
  28. default: null,
  29. type: cc.Node,
  30. serializable: true,
  31. },
  32. cheerAudio: {
  33. default: null,
  34. type: cc.AudioClip,
  35. },
  36. rival: {
  37. default: null,
  38. type: cc.Node,
  39. serializable: true,
  40. },
  41. //设备数据判断对象
  42. equipmentObj: null,
  43. },
  44. init() {//override
  45. this._super();
  46. this.locationLine.zIndex = 2;
  47. this.resultBarSt = this.resultBar.getComponent('ResultBar');
  48. },
  49. start()//override
  50. {
  51. this._super();
  52. if (lib.openInWebview()) {
  53. // 在app内Webview打开
  54. webView.register(this.node);
  55. this.node.on('onDeviceUpdateData', this.onDeviceUpdateData, this);
  56. // this.gStatesSt.playerId = mgobe.player.id;
  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
  144. let deleteHandrail = this.gStatesSt.drawedhandrailArr.shift();
  145. this.gStatesSt.handrailGCArr.push(deleteHandrail);
  146. if (this.gStatesSt.drawedhandrailArr.length != 0) {
  147. this.pStatesSt.nextHandrail = this.gStatesSt.drawedhandrailArr[0].handrail;
  148. }
  149. //destroy hurdrail
  150. let len = this.gStatesSt.handrailGCArr.length;
  151. for (let i = 0; i < len; i++) {
  152. if (this.node.x - this.gStatesSt.handrailGCArr[i].x > 720) {
  153. let handrail = this.gStatesSt.handrailGCArr[i];
  154. delete this.gStatesSt.handrailGCArr[i];
  155. handrail.destroy();
  156. }
  157. }
  158. // because delete array will left undefined element so we need to remove it from array
  159. for (let i = 0; i < len; i++) {
  160. if (this.gStatesSt.handrailGCArr[i] == undefined) {
  161. this.gStatesSt.handrailGCArr.splice(i);
  162. }
  163. }
  164. },
  165. enterEndline() {//override
  166. this._super();
  167. if (this.gStatesSt.arrEndPArr.length == 1) {
  168. this.scheduleOnce(function () {
  169. this.setResuletBar();
  170. }, 3);
  171. this.firework();
  172. }
  173. else {
  174. this.schedule(function () {
  175. this.resultBar.active = true;
  176. this.resultBar.getChildByName('Win').active = false;
  177. this.resultBar.getChildByName('Fail').active = true;
  178. this.setResuletBar();
  179. }, 1, 0, 0);
  180. }
  181. },
  182. setResuletBar() {
  183. this.resultBarSt.init();
  184. this.gmSt.unschedule(this.gmSt.countGameTime);
  185. this.resultBarSt.updateCostTime(this.gStatesSt.gameTime);
  186. this.resultBarSt.updateAvatar();
  187. //cal calorie
  188. let runUnit = 0;
  189. let jumpUnit = 0;
  190. let calorie = 0;
  191. if( webView.calorieParams != null)
  192. {
  193. runUnit = webView.calorieParams.runUnit;
  194. jumpUnit = webView.calorieParams.jumpUnit;
  195. calorie = parseInt(this.pStatesSt.runTimes * runUnit + this.pStatesSt.jumpTimes * jumpUnit);
  196. }
  197. this.resultBarSt.updateCalorie(calorie);
  198. if (lib.openInWebview()) {
  199. webView.postMessage(100, this.gmSt.countGameTime, calorie);
  200. }
  201. },
  202. firework() {
  203. cc.audioEngine.playEffect(this.cheerAudio, 0, function () { });
  204. let firework = this.gmSt.terminal.getChildByName("Firework");
  205. firework.active = true;
  206. firework.zIndex = 1005;
  207. },
  208. speedUp()//override
  209. {
  210. this._super();
  211. if (lib.openInWebview() && !webView.bAi) {
  212. mgobe.sendFrame('speedUp');
  213. }
  214. },
  215. jump() {//overwrite
  216. //super class return is not effect to sub class so we need to write if return every subclass
  217. // if (this.gStatesSt.drawedhandrailArr.length == 0) return;
  218. if (this.pStatesSt.currentState == this.pStatesSt.playerAnimState.jump) return;
  219. //init locationLine do once
  220. this.locationLine.opacity = 0;
  221. this.locationLine.x = this.node.x;
  222. //No perfect jump area
  223. if (this.locationLine.x > this.pStatesSt.nextHandrail.x &&
  224. this.locationLine.x < this.pStatesSt.nextHandrail.x + gameConfig.handrailArea1.end) {
  225. this.pStatesSt.currentState = this.pStatesSt.playerAnimState.jump;
  226. this.locationLine.opacity = 255;
  227. this.ctorSt.jump('hurdling_1', function () {
  228. this.pStatesSt.currentState = this.pStatesSt.playerAnimState.run;
  229. this.createAndDestroyHandrail();
  230. this.pStatesSt.passedHurdrailNum++;
  231. this.pStatesSt.jumpTimes++;
  232. }.bind(this),1);
  233. if (lib.openInWebview() && !webView.bAi) {
  234. mgobe.sendFrame('jump');
  235. }
  236. return;
  237. }
  238. //perfect jump area
  239. if (this.locationLine.x > this.pStatesSt.nextHandrail.x + gameConfig.handrailArea2.start &&
  240. this.locationLine.x < this.pStatesSt.nextHandrail.x + gameConfig.handrailArea2.end) {
  241. this.pStatesSt.nextHandrail.getChildByName('AccelerationBandCenter').destroy();
  242. this.locationLine.opacity = 255;
  243. this.pStatesSt.currentState = this.pStatesSt.playerAnimState.jump;
  244. this.ctorSt.jump('hurdling_1', function () {
  245. this.pStatesSt.currentState = this.pStatesSt.playerAnimState.run;
  246. this.createAndDestroyHandrail();
  247. this.pStatesSt.passedHurdrailNum++;
  248. this.pStatesSt.jumpTimes++;
  249. if (lib.openInWebview() && !webView.bAi) {
  250. mgobe.sendFrame('jump');
  251. }
  252. }.bind(this),1);
  253. return;
  254. }
  255. // this.pStatesSt.currentState = this.pStatesSt.playerAnimState.jump;
  256. // this.locationLine.opacity = 255;
  257. // this.ctorSt.jump('hurdling_1', function () {
  258. // this.pStatesSt.currentState = this.pStatesSt.playerAnimState.run;
  259. // // this.createAndDestroyHandrail();
  260. // // this.pStatesSt.passedHurdrailNum++;
  261. // // this.pStatesSt.jumpTimes++;
  262. // }.bind(this),0);
  263. // if (lib.openInWebview() && !webView.bAi) {
  264. // mgobe.sendFrame('jump');
  265. // }
  266. },
  267. //interact with device
  268. onDeviceUpdateData(data) {
  269. // console.log(data);
  270. //判断数据类型
  271. if (data.dataType == "Box") {
  272. // webView.onBLEBoxUpdate(data);
  273. let gameData = data.data;
  274. this.equipmentObj.updateJumpAndRun({
  275. // xA: data.data.acc.ax,
  276. // zA: data.data.acc.ay,
  277. // yA: data.data.acc.az,
  278. acc: gameData.acc,
  279. gyro: gameData.gyro,
  280. bLimitRebound: false
  281. })
  282. }
  283. // if (data.F == 0) {
  284. // console.log("F == 0");
  285. // this.speedUp();
  286. // } else if (data.F == 1) {
  287. // console.log("F == 1");
  288. // this.speedUp();
  289. // }
  290. // else if (data.F == 2) {
  291. // console.log("F == 2");
  292. // this.jump();
  293. // }
  294. },
  295. });
  296. // mgobe