Enemy.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import { _decorator, instantiate, Node, PhysicsGroup, tween, 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. import { MyTower } from './MyTower';
  7. import { resMgr } from '../../../Frames/ResourcesMgr';
  8. import { messageMgr } from '../../../Frames/MessageMgr';
  9. import { GameOverData } from './Data/GameOverData';
  10. const { ccclass, property } = _decorator;
  11. @ccclass('Enemy')
  12. export class Enemy extends Role {
  13. private _lifeBar: LifeBar = null;
  14. private _isDie: boolean = false;
  15. private _orePos: Vec3 = new Vec3(200, 100, 0);
  16. protected onLoad(): void {
  17. this._lifeBar = this.node.getComponent(LifeBar);
  18. this._isDie = false;
  19. }
  20. init(name: string, pos: Vec3, data: RoleData[], dir?: number) {
  21. super.init(name, pos, data, dir);
  22. this._initLifeBar();
  23. }
  24. //初始化血条
  25. private _initLifeBar() {
  26. this._lifeBar._curHp = this.hp;
  27. this._lifeBar._totalHp = this.hp;
  28. }
  29. update(deltaTime: number) {
  30. super.update(deltaTime);
  31. //this.stop();
  32. if (this._lifeBar._curHp <= 0) {
  33. super.playAnimation(RoleState.Die);
  34. if (this._isDie) return;
  35. this.createOre();
  36. this._isDie = true;
  37. }
  38. }
  39. createOre() {
  40. GameOverData.Instance.KillCount = GameOverData.Instance.KillCount + 1;
  41. //触发消息
  42. messageMgr.dispatch("addOreCount");
  43. const ore: Node = instantiate(resMgr.getPrefab("Ore"));
  44. ore.parent = this.bulletLayer;
  45. ore.position = this.node.position;
  46. tween(ore)
  47. .delay(0.3)
  48. .to(2, { position: this._orePos })
  49. .delay(0.3)
  50. .call(() => { ore.destroy() })
  51. .start();
  52. }
  53. stop(): void {
  54. if (this.node.getWorldPosition().x <= 200) {
  55. this.isStop = true;
  56. super.playAnimation(RoleState.Idle);
  57. //this.playAnimation(RoleState.Attack);
  58. this.moveSpeed = 0;
  59. }
  60. }
  61. //必须实现抽象方法
  62. protected _getCollisionGroup(): number {
  63. return PhysicsGroup.Enemy; //实际分组值根据项目设置
  64. }
  65. protected _isValidTarget(target: Role): boolean {
  66. //通过组件类型判断是否为敌人
  67. return target instanceof MyRole || target instanceof MyTower;
  68. }
  69. }