RadarComponent.ts 4.7 KB

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