import { _decorator, Node, PhysicsGroup, Vec3 } from 'cc'; import { Role, RoleState } from '../Role'; import { RoleData } from '../../../DataItem/ItemData'; import { LifeBar } from './LifeBar'; import { Enemy } from './Enemy'; import { MyTower } from './MyTower'; import { EnemyTower } from './EnemyTower'; const { ccclass, property } = _decorator; @ccclass('MyRole') export class MyRole extends Role { private _lifeBar: LifeBar = null; protected onLoad(): void { this._lifeBar = this.node.getComponent(LifeBar); } init(name: string, pos: Vec3, data: RoleData[]) { super.init(name, pos, data, 1) this._initLifeBar(); } //初始化血条 private _initLifeBar(){ this._lifeBar._curHp = this.hp; this._lifeBar._totalHp = this.hp; } update(deltaTime: number) { super.update(deltaTime); //this.stop(); if(this._lifeBar._curHp <= 0){ super.playAnimation(RoleState.Die); } } stop(): void { if(this.node.getWorldPosition().x >= 1080){ this.isStop = true; super.playAnimation(RoleState.Idle); this.moveSpeed = 0; } } //必须实现抽象方法 protected _getCollisionGroup(): number { return PhysicsGroup.Role; //实际分组值根据项目设置 } protected _isValidTarget(target: Role): boolean { //通过组件类型判断是否为敌人 return target instanceof Enemy || target instanceof EnemyTower; } }