PlayerController.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. console.log("openInWebview start");
  53. if (lib.openInWebview()) {
  54. // 在app内Webview打开
  55. webView.register(this.node);
  56. this.node.on('onDeviceUpdateData', this.onDeviceUpdateData, this);
  57. console.log("openInWebview");
  58. //监听回调
  59. uni.postMessage({
  60. data: {
  61. funName: "addDeviceUpdateListener",
  62. gameData: {}
  63. }
  64. })
  65. //开启设备回调
  66. uni.postMessage({
  67. data: {
  68. funName: "writeBLEConnectionValue",
  69. gameData: {
  70. value: "3" //开启设备数据
  71. }
  72. }
  73. })
  74. //开启设备频率
  75. uni.postMessage({
  76. data: {
  77. funName: "writeBLEConnectionValue",
  78. gameData: {
  79. value: "b" //60ms
  80. }
  81. }
  82. })
  83. this.equipmentObj = new EquipmentAction();
  84. this.equipmentObj.addEventListener("resultant", (res) => {
  85. let data = res;
  86. console.log("resultant:",data);
  87. if(data.type == "jump"){
  88. this.jump();
  89. }else if(data.type == "runing"){
  90. this.speedUp();
  91. }
  92. })
  93. // this.gStatesSt.playerId = mgobe.player.id;
  94. }
  95. },
  96. update(dt) {//override
  97. this._super();
  98. //make ui and camera follow the player
  99. this.gmSt.ui.x = this.node.x;
  100. this.mainCamera.x = this.node.x;
  101. if (this.pStatesSt.currentProState == this.pStatesSt.pProStates.end) return;
  102. //indicator
  103. if (this.node.x - this.rival.x > 360)//forward show left bar
  104. {
  105. this.ctorSt.showIndicater(1, Math.abs(Math.round(this.node.x - this.rival.x - 38)));
  106. }
  107. else if (this.node.x - this.rival.x < -360)//backward show right bar
  108. {
  109. this.ctorSt.showIndicater(0, Math.abs(Math.round(this.node.x - this.rival.x + 38)));
  110. }
  111. else {
  112. this.ctorSt.hideIndicater();
  113. }
  114. //生成主角前方地图
  115. this.switchMap();
  116. },
  117. switchMap() {
  118. if (this.node.x > this.pStatesSt.targetPoint) {
  119. this.mapArr[0].x += 2160;
  120. lib.swapLeft(this.mapArr);
  121. this.pStatesSt.targetPoint += 720;
  122. }
  123. },
  124. setEndSpeed() {//overwrite
  125. this.pStatesSt.intoTheEndSpeed = this.pStatesSt.currentSpeed / (this.gmSt.terminal.x - this.pStatesSt.endLineStopPoint);
  126. },
  127. passedHandrail()//overwrite
  128. {
  129. let handrail = this.pStatesSt.nextHandrail.getChildByName('Kuolan1');
  130. let spine = handrail.getComponent(sp.Skeleton);
  131. spine.setAnimation(0, 'Idl1_3', false);
  132. this.ctorSt.stagger(function () {
  133. this.pStatesSt.Combo = 0;
  134. this.createAndDestroyHandrail();
  135. }.bind(this));
  136. cc.audioEngine.playEffect(this.staggerAudio, 0, function () { });
  137. },
  138. createAndDestroyHandrail() {
  139. while (true) {
  140. let hurdrailPX = this.gmSt.createHandrail();
  141. if (hurdrailPX - this.node.x > 360 || hurdrailPX == -1) {
  142. break;
  143. }
  144. }
  145. //GC drawedhandrail
  146. let deleteHandrail = this.gStatesSt.drawedhandrailArr.shift();
  147. this.gStatesSt.handrailGCArr.push(deleteHandrail);
  148. if (this.gStatesSt.drawedhandrailArr.length != 0) {
  149. this.pStatesSt.nextHandrail = this.gStatesSt.drawedhandrailArr[0].handrail;
  150. }
  151. //destroy hurdrail
  152. let len = this.gStatesSt.handrailGCArr.length;
  153. for (let i = 0; i < len; i++) {
  154. if (this.node.x - this.gStatesSt.handrailGCArr[i].x > 720) {
  155. let handrail = this.gStatesSt.handrailGCArr[i];
  156. delete this.gStatesSt.handrailGCArr[i];
  157. handrail.destroy();
  158. }
  159. }
  160. // because delete array will left undefined element so we need to remove it from array
  161. for (let i = 0; i < len; i++) {
  162. if (this.gStatesSt.handrailGCArr[i] == undefined) {
  163. this.gStatesSt.handrailGCArr.splice(i);
  164. }
  165. }
  166. },
  167. enterEndline() {//override
  168. this._super();
  169. if (this.gStatesSt.arrEndPArr.length == 1) {
  170. this.scheduleOnce(function () {
  171. this.setResuletBar();
  172. }, 3);
  173. this.firework();
  174. }
  175. else {
  176. this.schedule(function () {
  177. this.resultBar.active = true;
  178. this.resultBar.getChildByName('Win').active = false;
  179. this.resultBar.getChildByName('Fail').active = true;
  180. this.setResuletBar();
  181. }, 1, 0, 0);
  182. }
  183. },
  184. setResuletBar() {
  185. this.resultBarSt.init();
  186. this.gmSt.unschedule(this.gmSt.countGameTime);
  187. this.resultBarSt.updateCostTime(this.gStatesSt.gameTime);
  188. this.resultBarSt.updateAvatar();
  189. //cal calorie
  190. let runUnit = webView.calorieParams.runUnit;
  191. let jumpUnit = webView.calorieParams.jumpUnit;
  192. let calorie = parseInt(this.pStatesSt.runTimes * runUnit + this.pStatesSt.jumpTimes * jumpUnit);
  193. this.resultBarSt.updateCalorie(calorie);
  194. if (lib.openInWebview()) {
  195. webView.postMessage(100, this.gmSt.countGameTime, calorie);
  196. }
  197. },
  198. firework() {
  199. cc.audioEngine.playEffect(this.cheerAudio, 0, function () { });
  200. let firework = this.gmSt.terminal.getChildByName("Firework");
  201. firework.active = true;
  202. firework.zIndex = 1005;
  203. },
  204. speedUp()//override
  205. {
  206. this._super();
  207. if (lib.openInWebview() && !webView.bAi) {
  208. mgobe.sendFrame('speedUp');
  209. }
  210. },
  211. jump() {//overwrite
  212. //super class return is not effect to sub class so we need to write if return every subclass
  213. // if (this.gStatesSt.drawedhandrailArr.length == 0) return;
  214. if (this.pStatesSt.currentState == this.pStatesSt.playerAnimState.jump) return;
  215. //init locationLine do once
  216. this.locationLine.opacity = 0;
  217. this.locationLine.x = this.node.x;
  218. //No perfect jump area
  219. if (this.locationLine.x > this.pStatesSt.nextHandrail.x &&
  220. this.locationLine.x < this.pStatesSt.nextHandrail.x + gameConfig.handrailArea1.end) {
  221. this.pStatesSt.currentState = this.pStatesSt.playerAnimState.jump;
  222. this.locationLine.opacity = 255;
  223. // this.ctorSt.jump('hurdling_1', function () {
  224. this.ctorSt.jump('Jump', function () {
  225. this.pStatesSt.currentState = this.pStatesSt.playerAnimState.run;
  226. this.createAndDestroyHandrail();
  227. this.pStatesSt.passedHurdrailNum++;
  228. this.pStatesSt.jumpTimes++;
  229. }.bind(this),1);
  230. if (lib.openInWebview() && !webView.bAi) {
  231. // mgobe.sendFrame('jump');
  232. mgobe.sendFrame('Jump');
  233. }
  234. return;
  235. }
  236. //perfect jump area
  237. if (this.locationLine.x > this.pStatesSt.nextHandrail.x + gameConfig.handrailArea2.start &&
  238. this.locationLine.x < this.pStatesSt.nextHandrail.x + gameConfig.handrailArea2.end) {
  239. this.pStatesSt.nextHandrail.getChildByName('AccelerationBandCenter').destroy();
  240. this.locationLine.opacity = 255;
  241. this.pStatesSt.currentState = this.pStatesSt.playerAnimState.jump;
  242. // this.ctorSt.jump('hurdling_1', function () {
  243. this.ctorSt.jump('Jump', function () {
  244. this.pStatesSt.currentState = this.pStatesSt.playerAnimState.run;
  245. this.createAndDestroyHandrail();
  246. this.pStatesSt.passedHurdrailNum++;
  247. this.pStatesSt.jumpTimes++;
  248. if (lib.openInWebview() && !webView.bAi) {
  249. // mgobe.sendFrame('jump');
  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.ctorSt.jump('Jump', function () {
  259. this.pStatesSt.currentState = this.pStatesSt.playerAnimState.run;
  260. // this.createAndDestroyHandrail();
  261. // this.pStatesSt.passedHurdrailNum++;
  262. // this.pStatesSt.jumpTimes++;
  263. }.bind(this),0);
  264. if (lib.openInWebview() && !webView.bAi) {
  265. // mgobe.sendFrame('jump');
  266. mgobe.sendFrame('Jump');
  267. }
  268. },
  269. //interact with device
  270. onDeviceUpdateData(data) {
  271. // console.log(data);
  272. //判断数据类型
  273. if (data.dataType == "Box") {
  274. // webView.onBLEBoxUpdate(data);
  275. let gameData = data.data;
  276. this.equipmentObj.updateJumpAndRun({
  277. // xA: data.data.acc.ax,
  278. // zA: data.data.acc.ay,
  279. // yA: data.data.acc.az,
  280. acc: gameData.acc,
  281. gyro: gameData.gyro,
  282. bLimitRebound: false
  283. })
  284. }
  285. // if (data.F == 0) {
  286. // console.log("F == 0");
  287. // this.speedUp();
  288. // } else if (data.F == 1) {
  289. // console.log("F == 1");
  290. // this.speedUp();
  291. // }
  292. // else if (data.F == 2) {
  293. // console.log("F == 2");
  294. // this.jump();
  295. // }
  296. },
  297. });
  298. // mgobe