Enemy.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { _decorator, instantiate, Node, PhysicsGroup, tween, Vec3 } from 'cc';
  2. import { Role, RoleState } from '../Role';
  3. import { RoleData } from '../../../DataItem/ItemData';
  4. import { LifeBar } from './LifeBar';
  5. import { MyRole } from './MyRole';
  6. import { MyTower } from './MyTower';
  7. import { resMgr } from '../../../Frames/ResourcesMgr';
  8. import { messageMgr } from '../../../Frames/MessageMgr';
  9. const { ccclass, property } = _decorator;
  10. @ccclass('Enemy')
  11. export class Enemy extends Role {
  12. private _lifeBar: LifeBar = null;
  13. private _isDie: boolean = false;
  14. private _orePos: Vec3 = new Vec3(200, 100, 0);
  15. protected onLoad(): void {
  16. this._lifeBar = this.node.getComponent(LifeBar);
  17. this._isDie = false;
  18. }
  19. init(name: string, pos: Vec3, data: RoleData[], dir?: number) {
  20. super.init(name, pos, data, dir);
  21. this._initLifeBar();
  22. }
  23. //初始化血条
  24. private _initLifeBar() {
  25. this._lifeBar._curHp = this.hp;
  26. this._lifeBar._totalHp = this.hp;
  27. }
  28. update(deltaTime: number) {
  29. super.update(deltaTime);
  30. //this.stop();
  31. if (this._lifeBar._curHp <= 0) {
  32. super.playAnimation(RoleState.Die);
  33. if (this._isDie) return;
  34. this.createOre();
  35. this._isDie = true;
  36. }
  37. }
  38. createOre() {
  39. //激活消息
  40. messageMgr.dispatch("addOreCount");
  41. const ore: Node = instantiate(resMgr.getPrefab("Ore"));
  42. ore.parent = this.bulletLayer;
  43. ore.position = this.node.position;
  44. tween(ore)
  45. .delay(0.3)
  46. .to(2, { position: this._orePos })
  47. .delay(0.3)
  48. .call(() => { ore.destroy() })
  49. .start();
  50. }
  51. stop(): void {
  52. if (this.node.getWorldPosition().x <= 200) {
  53. this.isStop = true;
  54. super.playAnimation(RoleState.Idle);
  55. //this.playAnimation(RoleState.Attack);
  56. this.moveSpeed = 0;
  57. }
  58. }
  59. //必须实现抽象方法
  60. protected _getCollisionGroup(): number {
  61. return PhysicsGroup.Enemy; //实际分组值根据项目设置
  62. }
  63. protected _isValidTarget(target: Role): boolean {
  64. //通过组件类型判断是否为敌人
  65. return target instanceof MyRole || target instanceof MyTower;
  66. }
  67. }