AiPlayerController.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. var gameConfig = require("../GameConfig.js");
  2. var playerConfig = require("./AiPlayerConfig");
  3. var webView = require("../../WebView");
  4. var globalConfig = require("../../Global");
  5. var lib = require("../../Library.js");
  6. // var mgobe = require("../../Mgobe");
  7. var lockStepClient = require("../../LockStepClient");
  8. cc.Class({
  9. extends: require("BasePlayerController"),
  10. properties: {
  11. mainPlayer: {
  12. default: null,
  13. type: cc.Node,
  14. serializable: true,
  15. },
  16. },
  17. init()//override
  18. {
  19. this._super();
  20. this.areaState = -1;
  21. this.area1 = gameConfig.handrailArea1;
  22. this.area2 = gameConfig.handrailArea2;
  23. },
  24. start()//override
  25. {
  26. this._super();
  27. let Self = this;
  28. //监听帧同步
  29. if (!globalConfig.bAi)
  30. {
  31. // mgobe.startFrameSync();
  32. // mgobe.room.onRecvFrame = this.onRecvFrame.bind(this);
  33. lockStepClient.onRecvFromClient(function (data) {
  34. Self.onRecvFrame(JSON.parse(data));
  35. });
  36. }
  37. },
  38. onRecvFrame: function (data) {
  39. if(data.message == 'jump')
  40. {
  41. this.jump();
  42. }
  43. else if(data.message == 'speedUp')
  44. {
  45. this.speedUp();
  46. }
  47. },
  48. update(dt) {//override
  49. this._super();
  50. if(!globalConfig.bAi) return;
  51. //super class return is not effect to sub class so we need to write if return every subclass
  52. if(this.pStatesSt.currentProState == this.pStatesSt.pProStates.end) return;
  53. //测试用给Ai随机速度
  54. // if (this.pStatesSt.currentSpeed < playerConfig.playerSpeedGrade.fast)
  55. // {
  56. // this.pStatesSt.currentSpeed += lib.randomFromIntRange(0, 1.2);
  57. // // this.pStatesSt.currentSpeed += lib.randomFromIntRange(0, 1);
  58. // }
  59. if(lib.randomFromIntRange(0, 100)>98){
  60. this.speedUp();
  61. }
  62. if(this.pStatesSt.handrailArr.length == this.pStatesSt.passedHurdrailNum)return;
  63. this.hurdrailStartPX = this.pStatesSt.handrailArr[this.pStatesSt.passedHurdrailNum].startPX;
  64. // Enter jump area 1 of handrail
  65. if (this.node.x > this.hurdrailStartPX+this.area1.end/2 &&
  66. this.node.x < this.hurdrailStartPX+this.area1.end)
  67. {
  68. if(this.areaState == -1)
  69. {
  70. this.areaState = 0;
  71. this.rate = lib.randomFromIntRange(0,100);
  72. if(this.rate<40)
  73. {
  74. this.jump();
  75. }
  76. }
  77. }
  78. // Enter jump area 2 of handrail
  79. if (this.node.x > this.hurdrailStartPX + this.area2.end/2 &&
  80. this.node.x < this.hurdrailStartPX + this.area2.end)//-(2487-2374))
  81. {
  82. if(this.areaState == 0)
  83. {
  84. this.areaState = 1;
  85. if (this.rate >= 40 && this.rate < 50) {
  86. this.jump();
  87. }
  88. this.schedule(function() {
  89. this.areaState = -1;
  90. }, 1, 0, 0);
  91. }
  92. }
  93. },
  94. setEndSpeed(){//overwrite
  95. this.pStatesSt.intoTheEndSpeed = this.pStatesSt.currentSpeed / (this.gmSt.terminal.x+35-this.pStatesSt.endLineStopPoint);
  96. },
  97. passedHandrail()//overwrite
  98. {
  99. // console.log('passedHandrail');
  100. let ContainIndex = -1;
  101. for(let i=0;i<this.gStatesSt.drawedhandrailArr.length;i++)
  102. {
  103. // console.log('index',this.gStatesSt.drawedhandrailArr[i].index);
  104. // console.log('Num',this.pStatesSt.passedHurdrailNum);
  105. if(this.gStatesSt.drawedhandrailArr[i].index == this.pStatesSt.passedHurdrailNum)
  106. {
  107. ContainIndex = i;
  108. break;
  109. }
  110. }
  111. if(ContainIndex!=-1)
  112. {
  113. // console.log('Kuolan2 fall');
  114. let handrail = this.gStatesSt.drawedhandrailArr[ContainIndex].handrail.getChildByName('Kuolan2');
  115. let spine = handrail.getComponent(sp.Skeleton);
  116. spine.setAnimation(0, 'Idl1_3', false);
  117. this.ctorSt.stagger();
  118. }
  119. },
  120. jump() {//overwrite
  121. this.pStatesSt.currentState = this.pStatesSt.playerAnimState.jump;
  122. this.ctorSt.jump('hurdling_1', function ()
  123. {
  124. this.pStatesSt.currentState = this.pStatesSt.playerAnimState.run;
  125. this.pStatesSt.passedHurdrailNum++;
  126. }.bind(this));
  127. }
  128. });