PlayerController.js 11 KB

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