RadarComponent.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import { _decorator, Camera, Component, Node, tween, UIOpacity, UITransform, Vec3, view } from 'cc';
  2. import { EventDispatcher } from 'db://assets/core_tgx/easy_ui_framework/EventDispatcher';
  3. import { GameEvent } from '../Enum/GameEvent';
  4. import { AliensGlobalInstance } from '../AliensGlobalInstance';
  5. import { GameUtil } from '../GameUtil';
  6. import { AliensAudioMgr } from '../Manager/AliensAudioMgr';
  7. const { ccclass, property } = _decorator;
  8. @ccclass('RadarComponent')
  9. export class RadarComponent extends Component {
  10. @property(Node)
  11. activeNode: Node = null!;
  12. private _tween: any = null;
  13. //渲染的目标节点
  14. private _targetNode: Node = null!;
  15. private _shouldUpdatePosition = true;
  16. private _index = 0;
  17. protected onLoad(): void {
  18. this.registerEvent();
  19. }
  20. protected onEnable(): void {
  21. this.activeNode.active = true;
  22. this.startFadeAnimation();
  23. }
  24. private startFadeAnimation() {
  25. if (!this.activeNode) return;
  26. if (this._tween) {
  27. this._tween.stop();
  28. }
  29. const uiOpacity = this.activeNode.getComponent(UIOpacity);
  30. uiOpacity.opacity = 255;
  31. this._tween = tween(uiOpacity)
  32. .to(0.5, { opacity: 0 })
  33. .to(0.5, { opacity: 255 })
  34. .union()
  35. .repeatForever()
  36. .start();
  37. }
  38. private registerEvent() {
  39. EventDispatcher.instance.on(GameEvent.EVENT_CAMERA_SCREENSHOT_RADAR, this.onRadar, this);
  40. EventDispatcher.instance.on(GameEvent.EVENT_CAMERA_SHOOT_ENEMY, this.cancelRadar, this);
  41. EventDispatcher.instance.on(GameEvent.EVENT_LAST_ENEMY_KILLED, this.cancelRadar, this);
  42. }
  43. private unregisterEvent() {
  44. EventDispatcher.instance.off(GameEvent.EVENT_CAMERA_SCREENSHOT_RADAR, this.onRadar, this);
  45. EventDispatcher.instance.off(GameEvent.EVENT_CAMERA_SHOOT_ENEMY, this.cancelRadar, this);
  46. EventDispatcher.instance.off(GameEvent.EVENT_LAST_ENEMY_KILLED, this.cancelRadar, this);
  47. }
  48. private async onRadar() {
  49. AliensAudioMgr.playOneShot(AliensAudioMgr.getMusicIdName(8), 1.0);
  50. this.node.active = true;
  51. this._shouldUpdatePosition = true;
  52. //获取相机
  53. const camera = await this.getSceneCamera();
  54. //获取目标节点
  55. this._targetNode = await this.getTargetNode();
  56. if (this._targetNode) {
  57. const battleUI = AliensGlobalInstance.instance.battleUI;
  58. const localPos = GameUtil.worldToScreenLocal(this._targetNode, battleUI, camera);
  59. // 移动雷达指示器
  60. this.node.setPosition(localPos);
  61. EventDispatcher.instance.emit(GameEvent.EVENT_CAMERA_SCREENSHOT_RADAR_LOCK, this._targetNode); // 发送事件通知相机已准备好进行截图,传递当前的 _targetNode 作为参数,用于在相机组件中获取目标节点的位置和旋转信息。
  62. }
  63. }
  64. private cancelRadar() {
  65. console.log('取消雷达');
  66. this.node.active = false;
  67. this._shouldUpdatePosition = false;
  68. }
  69. public lockPositionUpdate() {
  70. this._shouldUpdatePosition = false;
  71. }
  72. public unlockPositionUpdate() {
  73. this._shouldUpdatePosition = true;
  74. }
  75. update(deltaTime: number) {
  76. if (this._shouldUpdatePosition && this._targetNode) {
  77. const battleUI = AliensGlobalInstance.instance.battleUI;
  78. const camera = this.node.scene.getComponentInChildren(Camera);
  79. const localPos = GameUtil.worldToScreenLocal(this._targetNode, battleUI, camera);
  80. this.node.setPosition(localPos);
  81. }
  82. }
  83. private async getSceneCamera(): Promise<Camera> {
  84. return new Promise<Camera>((resolve, reject) => {
  85. const levelNode = AliensGlobalInstance.instance.levels.children[0];
  86. if (!levelNode) { return; }
  87. const camera = levelNode.getComponentInChildren(Camera)!;
  88. resolve(camera);
  89. });
  90. }
  91. //获取目标节点
  92. private async getTargetNode(): Promise<Node> {
  93. return new Promise<Node>((resolve, reject) => {
  94. const levelNode = AliensGlobalInstance.instance.levels.children[0];
  95. const et = levelNode.getChildByName('Ets');
  96. console.log(`剩余的外星人数量:${et.children.length}`);
  97. this._index++;
  98. if (this._index >= et.children.length) {
  99. this._index = 0;
  100. }
  101. resolve(et.children[this._index]);
  102. // resolve(et.children[1]);
  103. });
  104. }
  105. protected onDestroy(): void {
  106. if (this._tween) {
  107. this._tween.stop();
  108. this._tween = null;
  109. }
  110. if (this.activeNode) {
  111. const uiOpacity = this.activeNode.getComponent(UIOpacity);
  112. if (uiOpacity) {
  113. uiOpacity.opacity = 255;
  114. }
  115. }
  116. this.unregisterEvent();
  117. }
  118. }