Enemy.ts 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import { _decorator, Component, Node, Vec3 } from 'cc';
  2. import { Role } from '../Role';
  3. import { LevelData, RoleData } from '../../../DataItem/ItemData';
  4. import { LifeBar } from './LifeBar';
  5. const { ccclass, property } = _decorator;
  6. @ccclass('Enemy')
  7. export class Enemy extends Role {
  8. private _lifeBar: LifeBar = null;
  9. private _roles: Node = null;
  10. protected onLoad(): void {
  11. this._lifeBar = this.node.getComponent(LifeBar);
  12. this._roles = this.node.parent.parent.getChildByPath("Road/Roles");
  13. }
  14. init(name: string, pos: Vec3, data: RoleData[], dir?: number) {
  15. super.init(name, pos, data, dir);
  16. this._initLifeBar();
  17. }
  18. //初始化血条
  19. private _initLifeBar(){
  20. this._lifeBar._curHp = this.hp;
  21. this._lifeBar._totalHp = this.hp;
  22. }
  23. update(deltaTime: number) {
  24. if (!this.isStop) {
  25. this.move(deltaTime)
  26. }
  27. this.stop();
  28. if(!this.isAttacking){
  29. super.getEnemiesAhead(this._roles);
  30. }
  31. }
  32. stop(): void {
  33. if (this.node.getWorldPosition().x <= 200) {
  34. this.isStop = true;
  35. }
  36. }
  37. }