AIPet.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import CMath from "../../../../util/CMath";
  2. import { SpriteActionType } from "../FSprite";
  3. import PSprite from "../PSprite";
  4. import SkillBase from "../skill/SkillBase";
  5. import SkillFixed from "../skill/SkillFixed";
  6. import AIBase from "./AIBase";
  7. const { ccclass, property } = cc._decorator;
  8. /**
  9. * 伙伴AI
  10. *
  11. * AI特性:
  12. * 1.有攻击对象的时候,主动攻击
  13. * 2.没有攻击对象的时候,跟随
  14. */
  15. @ccclass
  16. export default class AIPet extends AIBase {
  17. onLoad() {
  18. super.onLoad();
  19. this.speed = 300;
  20. this.sprite.SPEED_WALK = this.speed;
  21. this.addSkill()
  22. }
  23. public addSkill() {
  24. let skillFixed: SkillFixed = this.node.addComponent(SkillFixed)
  25. skillFixed.count = 1;
  26. skillFixed.time = 0;
  27. skillFixed.btime = 1;
  28. skillFixed.CD = 800;
  29. cc.resources.load('prefab/bullet/' + this.sprite.attrData.bullet, cc.Prefab, (err, prefab: cc.Prefab) => {
  30. if (err) {
  31. cc.error(err);
  32. } else {
  33. //加载结束
  34. skillFixed.mBullet = prefab
  35. this.skills = this.node.getComponents(SkillBase);
  36. }
  37. });
  38. }
  39. update(dt) {
  40. if (this.sprite && this.sprite.isActive) {
  41. if (this.sprite.gamePause) {
  42. return;
  43. }
  44. this.AI();
  45. }
  46. }
  47. public AI() {
  48. let target = this.checkTarget();
  49. if (target) {
  50. if (!this.canSkill) {
  51. return;
  52. }
  53. (this.sprite as PSprite).stopFollow();
  54. if (this.sprite.isActive) {
  55. let skill: SkillBase = this.checkSkill(target);
  56. if (skill) {
  57. if (this.speed <= 0) {
  58. this.canSkill = false;
  59. skill.exe(target, () => {
  60. this.canSkill = true;
  61. // cc.log('技能使用结束 :',skill)
  62. });
  63. } else {
  64. let p1 = this.sprite.node.getPosition()
  65. let p2 = target.node.getPosition()
  66. let dis = CMath.getDistance(p1, p2);
  67. if (dis < skill.range) {
  68. this.canSkill = false;
  69. skill.exe(target, () => {
  70. this.canSkill = true;
  71. // cc.log('技能使用结束 :',skill)
  72. });
  73. } else {
  74. this.walk(skill.range);
  75. // this.moveToTarget(target)
  76. }
  77. }
  78. } else {
  79. // this.walk(300);
  80. }
  81. }
  82. } else {
  83. (this.sprite as PSprite).startFollow();
  84. }
  85. }
  86. /**
  87. * 远程怪物的闲逛
  88. */
  89. public walk(distance) {
  90. if (this.speed > 0) {
  91. this.canSkill = false;
  92. this.sprite.setDir(this.getRandState(distance));
  93. this.sprite.playAction(SpriteActionType.move, true)
  94. cc.tween(this).delay(1).call(() => {
  95. this.sprite.setDir({ x: 0, y: 0 });
  96. this.sprite.playAction(SpriteActionType.stand, true)
  97. }).delay(1).call(() => {
  98. this.canSkill = true;
  99. }).start()
  100. } else {
  101. this.canSkill = true;
  102. this.sprite.setDir({ x: 0, y: 0 });
  103. this.sprite.playAction(SpriteActionType.stand, true)
  104. }
  105. }
  106. }