MyRole.ts 1.5 KB

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