AIPet.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import FSprite, { SpriteActionType, SpriteType } from "../FSprite";
  2. import PSprite from "../PSprite";
  3. import AIBase from "./AIBase";
  4. const {ccclass, property} = cc._decorator;
  5. /**
  6. * 伙伴AI
  7. *
  8. * AI特性:
  9. * 1.有攻击对象的时候,主动攻击
  10. * 2.没有攻击对象的时候,跟随
  11. */
  12. @ccclass
  13. export default class AIPet extends AIBase {
  14. onLoad(){
  15. super.onLoad();
  16. this.atk_CD = 3000;
  17. this.speed = 120;
  18. this.atk_count = 3;
  19. this.sprite.SPEED_WALK = this.speed;
  20. }
  21. update (dt) {
  22. if(this.sprite){
  23. if(this.sprite.gamePause){
  24. return;
  25. }
  26. this.AI();
  27. }
  28. }
  29. public AI(){
  30. let target = this.checkTarget();
  31. if(target){
  32. let time = new Date().getTime();
  33. (this.sprite as PSprite).stopFollow();
  34. if(this.sprite.isActive){
  35. if(this.skills.length > 0){
  36. let skill = this.checkSkill(target);
  37. if(skill){
  38. this.canSkill = false;
  39. skill.exe(target,()=>{
  40. this.canSkill = true;
  41. });
  42. // cc.tween(this.node).sequence(
  43. // cc.delayTime(skill.continued/1000),
  44. // cc.callFunc(()=>{
  45. // })
  46. // ).start();
  47. }
  48. } else{
  49. if(time - this.atk_Time > this.atk_CD){
  50. this.atk_Time = time;
  51. this.fire(target);
  52. }
  53. }
  54. }
  55. }else{
  56. (this.sprite as PSprite).startFollow();
  57. }
  58. }
  59. public fire(target:FSprite){
  60. //判断是否在攻击范围内
  61. let mts = this.sprite.mButtleDis;
  62. let p1 = target.node.getPosition();
  63. let p2 = this.sprite.node.getPosition();
  64. let dis = cc.Vec2.distance(p1,p2);
  65. if(dis > mts){
  66. this.atk_Time = 0;
  67. let tmp = {
  68. x:0,
  69. y:0
  70. }
  71. let px1 = p1.x - p2.x;
  72. if(Math.abs(px1) < 50){
  73. tmp.x = 0;
  74. }else if(px1 > 0){
  75. tmp.x = 1;
  76. }else{
  77. tmp.x = -1;
  78. }
  79. let py1 = p1.y - p2.y;
  80. if(Math.abs(py1) < 50){
  81. tmp.y = 0;
  82. }else if(py1 > 0){
  83. tmp.y = 1;
  84. }else{
  85. tmp.y = -1;
  86. }
  87. this.sprite.setDir(tmp);
  88. }else{
  89. this.sprite.setDir({x:0,y:0});
  90. this.sprite.status = SpriteType.NONE;
  91. this.sprite.setShooting(true);
  92. let count = 0;
  93. this.sprite.setFireCallback(()=>{
  94. count ++;
  95. if(count >= this.atk_count){
  96. this.sprite.setShooting(false);
  97. this.sprite.setFireCallback(null);
  98. cc.tween(this).delay(0.7).call(()=>{
  99. this.walk(this.sprite.mButtleDis);
  100. }).start();
  101. }
  102. });
  103. }
  104. }
  105. }