Enemy.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import { _decorator, Node, PhysicsGroup, 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. const { ccclass, property } = _decorator;
  7. @ccclass('Enemy')
  8. export class Enemy extends Role {
  9. private _lifeBar: LifeBar = null;
  10. private _roles: Node = null;
  11. protected onLoad(): void {
  12. this._lifeBar = this.node.getComponent(LifeBar);
  13. this._roles = this.node.parent.parent.getChildByPath("Road/Roles");
  14. }
  15. init(name: string, pos: Vec3, data: RoleData[], dir?: number) {
  16. super.init(name, pos, data, dir);
  17. this._initLifeBar();
  18. }
  19. //初始化血条
  20. private _initLifeBar() {
  21. this._lifeBar._curHp = this.hp;
  22. this._lifeBar._totalHp = this.hp;
  23. }
  24. update(deltaTime: number) {
  25. super.update(deltaTime);
  26. super.stop();
  27. }
  28. stop(): void {
  29. if (this.node.getWorldPosition().x <= 200) {
  30. this.playAnimation(RoleState.Idle);
  31. this.moveSpeed = 0;
  32. }
  33. }
  34. //必须实现抽象方法
  35. protected _getCollisionGroup(): number {
  36. return PhysicsGroup.Enemy; //实际分组值根据项目设置
  37. }
  38. protected _isValidTarget(target: Role): boolean {
  39. //通过组件类型判断是否为敌人
  40. return target instanceof MyRole;
  41. }
  42. }