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