AIPet.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 = 100;
  20. this.sprite.SPEED_WALK = this.speed;
  21. this.addSkill()
  22. }
  23. public addSkill() {
  24. let skillFixed: SkillFixed = this.node.addComponent(SkillFixed)
  25. cc.resources.load('prefab/bullet/' + this.sprite.attrData.bullet, cc.Prefab, (err, prefab: cc.Prefab) => {
  26. if (err) {
  27. cc.error(err);
  28. } else {
  29. //加载结束
  30. skillFixed.mBullet = prefab
  31. this.skills = this.node.getComponents(SkillBase);
  32. }
  33. });
  34. }
  35. update(dt) {
  36. if (this.sprite && this.sprite.isActive) {
  37. if (this.sprite.gamePause) {
  38. return;
  39. }
  40. this.AI();
  41. }
  42. }
  43. public AI() {
  44. if (!this.canSkill) {
  45. return;
  46. }
  47. let target = this.checkTarget();
  48. if (target) {
  49. (this.sprite as PSprite).stopFollow();
  50. if (this.sprite.isActive) {
  51. let skill: SkillBase = this.checkSkill(target);
  52. if (skill) {
  53. if (this.speed <= 0) {
  54. this.canSkill = false;
  55. skill.exe(target, () => {
  56. this.canSkill = true;
  57. // cc.log('技能使用结束 :',skill)
  58. });
  59. } else {
  60. let p1 = this.sprite.node.getPosition()
  61. let p2 = target.node.getPosition()
  62. let dis = CMath.getDistance(p1, p2);
  63. if (dis < skill.range) {
  64. this.canSkill = false;
  65. skill.exe(target, () => {
  66. this.canSkill = true;
  67. // cc.log('技能使用结束 :',skill)
  68. });
  69. } else {
  70. this.walk(skill.range);
  71. // this.moveToTarget(target)
  72. }
  73. }
  74. } else {
  75. this.walk(300);
  76. }
  77. }
  78. } else {
  79. (this.sprite as PSprite).startFollow();
  80. }
  81. }
  82. /**
  83. * 远程怪物的闲逛
  84. */
  85. public walk(distance) {
  86. if (this.speed > 0) {
  87. this.canSkill = false;
  88. this.sprite.setDir(this.getRandState(distance));
  89. this.sprite.playAction(SpriteActionType.move, true)
  90. cc.tween(this).delay(1).call(() => {
  91. this.sprite.setDir({ x: 0, y: 0 });
  92. this.sprite.playAction(SpriteActionType.stand, true)
  93. }).delay(1).call(() => {
  94. this.canSkill = true;
  95. }).start()
  96. } else {
  97. this.canSkill = true;
  98. this.sprite.setDir({ x: 0, y: 0 });
  99. this.sprite.playAction(SpriteActionType.stand, true)
  100. }
  101. }
  102. }