HeroControl_ai.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. const MOVE_RIGHT = 2;
  2. cc.macro.ENABLE_TILEDMAP_CULLING = false;
  3. cc.Class({
  4. extends: cc.Component,
  5. properties: {
  6. InitialPositionY:0,
  7. CurrentSpeed: 300,
  8. jumps: 2,
  9. acceleration: 1500,
  10. jumpSpeed: 600,
  11. drag: 600,
  12. Level: {
  13. default: null,
  14. type: cc.Node
  15. },
  16. player: {
  17. default: null,
  18. type: cc.Node
  19. }
  20. },
  21. // use this for initialization
  22. onLoad: function () {
  23. this._up = false;
  24. this.body = this.getComponent(cc.RigidBody);
  25. this.speed = cc.p(0, 0);
  26. this.InitialPositionY = this.node.getPositionY();
  27. this.EnableHeroMoving(true);
  28. },
  29. HeroJump:function()
  30. {
  31. if(this.node.getPositionY()>this.InitialPositionY)return;
  32. this._up = true;
  33. // this.CurrentSpeed=500;
  34. // this.schedule(function()
  35. // {
  36. // this.CurrentSpeed=300;
  37. // }, 1,1);
  38. },
  39. EnableHeroMoving:function(bMoving)
  40. {
  41. if(bMoving)
  42. {
  43. this._moveFlags = MOVE_RIGHT;
  44. }
  45. else
  46. {
  47. this._moveFlags = -1;
  48. }
  49. },
  50. HeroUpSpeed:function(UpValue)
  51. {
  52. // cc.log('speed up');
  53. if(this.CurrentSpeed<1000 )
  54. {
  55. // cc.log('speed ='+this.CurrentSpeed);
  56. this.CurrentSpeed+=UpValue;
  57. }
  58. },
  59. // Stagger:function()
  60. // {
  61. // // cc.log('Stagger')
  62. // var PlayerStateScript = this.node.getComponent("PlayerState");
  63. // // cc.log("踉跄",PlayerStateScript.BStagger);
  64. // if(PlayerStateScript.BStagger) return;
  65. // PlayerStateScript.BStagger = !PlayerStateScript.BStagger;
  66. //
  67. // //踉跄+减速
  68. // this.player.getComponent("Charactor").setOnTripListener();
  69. //
  70. // this.player.getComponent("Charactor").setEndListener(function (animationName) {
  71. // if (animationName == "run-05") {
  72. // // PlayerStateScript.BStagger = !PlayerStateScript.BStagger;
  73. // }
  74. // }.bind(this));
  75. // this.CurrentSpeed = 50;
  76. // this.getComponent("easing").startDamp(this.CurrentSpeed,0.1,0,500,2);
  77. //
  78. // this.getComponent("easing").sethandleCallBack(function () {
  79. // // cc.log("轨迹 结束");
  80. // PlayerStateScript.BStagger = !PlayerStateScript.BStagger;
  81. // }.bind(this));
  82. // this.getComponent("easing").setreduceListnener(function (s) {
  83. // this.CurrentSpeed=s;
  84. // }.bind(this));
  85. // this.getComponent("easing").setincreaseListnener(function (s) {
  86. // this.CurrentSpeed=s;
  87. // }.bind(this));
  88. // },
  89. onBeginContact: function (contact, selfCollider, otherCollider) {
  90. // if (selfCollider.node.y > otherCollider.node.y) {
  91. // this.jumps = 2;
  92. // }
  93. // if (otherCollider.tag === 100) {
  94. // this.camera.shakeCamera();
  95. // }
  96. },
  97. // called every frame, uncomment this function to activate update callback
  98. update: function (dt) {
  99. this.speed = this.body.linearVelocity;
  100. // this.Level.getComponent('Level').SetDistance(this.node.getPosition().x);
  101. // this.Level.getComponent('LevelControl').SetDistance(this.node.getPosition().x);
  102. if (this._moveFlags === MOVE_RIGHT) {
  103. // this.anim.play('walk');
  104. if(this.node.scaleX < 0) {
  105. this.node.scaleX *= -1;
  106. }
  107. this.speed.x += this.acceleration * dt;
  108. if(this.speed.x > this.CurrentSpeed) {
  109. this.speed.x = this.CurrentSpeed;
  110. }
  111. }
  112. else {
  113. if(this.speed.x !== 0) {
  114. var d = this.drag * dt;
  115. if(Math.abs(this.speed.x) <= d) {
  116. this.speed.x = 0;
  117. // this.anim.play('idle');
  118. } else {
  119. this.speed.x -= this.speed.x > 0 ? d : -d;
  120. }
  121. }
  122. }
  123. if (this.jumps > 0 && this._up) {
  124. this.speed.y = this.jumpSpeed;
  125. this.jumps--;
  126. }
  127. this._up = false;
  128. this.body.linearVelocity = this.speed;
  129. },
  130. //todo 所有的都移动出去了
  131. // allMoveEndListnener : function (node) {
  132. // // cc.log('我所有的都出去了',this.player);
  133. // this.player.getComponent("Charactor").setOnTripListener();
  134. // }
  135. });