MyRole.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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 { Enemy } from './Enemy';
  6. import { EnemyTower } from './EnemyTower';
  7. const { ccclass, property } = _decorator;
  8. @ccclass('MyRole')
  9. export class MyRole extends Role {
  10. private _lifeBar: LifeBar = null;
  11. protected onLoad(): void {
  12. this._lifeBar = this.node.getComponent(LifeBar);
  13. }
  14. init(name: string, pos: Vec3, data: RoleData[]) {
  15. super.init(name, pos, data, 1)
  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. super.update(deltaTime);
  25. //this.stop();
  26. if(this._lifeBar._curHp <= 0){
  27. super.playAnimation(RoleState.Die);
  28. }
  29. }
  30. stop(): void {
  31. if(this.node.getWorldPosition().x >= 1080){
  32. this.isStop = true;
  33. super.playAnimation(RoleState.Idle);
  34. this.moveSpeed = 0;
  35. }
  36. }
  37. //必须实现抽象方法
  38. protected _getCollisionGroup(): number {
  39. return PhysicsGroup.Role; //实际分组值根据项目设置
  40. }
  41. protected _isValidTarget(target: Role): boolean {
  42. //通过组件类型判断是否为敌人
  43. return target instanceof Enemy || target instanceof EnemyTower;
  44. }
  45. }