RadarComponent.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import { _decorator, Camera, Component, Node, 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. //渲染的目标节点
  10. private _targetNode: Node = null!;
  11. private _shouldUpdatePosition = true;
  12. private _index = 0;
  13. protected onLoad(): void {
  14. this.registerEvent();
  15. }
  16. private registerEvent(){
  17. EventDispatcher.instance.on(GameEvent.EVENT_CAMERA_SCREENSHOT_RADAR,this.onRadar,this);
  18. EventDispatcher.instance.on(GameEvent.EVENT_CAMERA_SHOOT_ENEMY,this.cancelRadar,this);
  19. EventDispatcher.instance.on(GameEvent.EVENT_LAST_ENEMY_KILLED,this.cancelRadar,this);
  20. }
  21. private unregisterEvent(){
  22. EventDispatcher.instance.off(GameEvent.EVENT_CAMERA_SCREENSHOT_RADAR,this.onRadar,this);
  23. EventDispatcher.instance.off(GameEvent.EVENT_CAMERA_SHOOT_ENEMY,this.cancelRadar,this);
  24. EventDispatcher.instance.off(GameEvent.EVENT_LAST_ENEMY_KILLED,this.cancelRadar,this);
  25. }
  26. private async onRadar(){
  27. this.node.active = true;
  28. this._shouldUpdatePosition = true;
  29. //获取相机
  30. const camera = await this.getSceneCamera();
  31. //获取目标节点
  32. this._targetNode = await this.getTargetNode();
  33. if(this._targetNode){
  34. const battleUI = AliensGlobalInstance.instance.battleUI;
  35. const localPos = GameUtil.worldToScreenLocal(this._targetNode,battleUI,camera);
  36. // 移动雷达指示器
  37. this.node.setPosition(localPos);
  38. EventDispatcher.instance.emit(GameEvent.EVENT_CAMERA_SCREENSHOT_RADAR_LOCK,this._targetNode); // 发送事件通知相机已准备好进行截图,传递当前的 _targetNode 作为参数,用于在相机组件中获取目标节点的位置和旋转信息。
  39. }
  40. }
  41. private cancelRadar(){
  42. console.log('取消雷达');
  43. this.node.active = false;
  44. this._shouldUpdatePosition = false;
  45. }
  46. public lockPositionUpdate() {
  47. this._shouldUpdatePosition = false;
  48. }
  49. public unlockPositionUpdate() {
  50. this._shouldUpdatePosition = true;
  51. }
  52. update(deltaTime: number) {
  53. if(this._shouldUpdatePosition && this._targetNode) {
  54. const battleUI = AliensGlobalInstance.instance.battleUI;
  55. const camera = this.node.scene.getComponentInChildren(Camera);
  56. const localPos = GameUtil.worldToScreenLocal(this._targetNode, battleUI, camera);
  57. this.node.setPosition(localPos);
  58. }
  59. }
  60. private async getSceneCamera() :Promise<Camera>{
  61. return new Promise<Camera>((resolve, reject) => {
  62. const levelNode = AliensGlobalInstance.instance.levels.children[0];
  63. if(!levelNode){return;}
  64. const camera = levelNode.getComponentInChildren(Camera)!;
  65. resolve(camera);
  66. });
  67. }
  68. //获取目标节点
  69. private async getTargetNode():Promise<Node> {
  70. return new Promise<Node>((resolve, reject) => {
  71. const levelNode = AliensGlobalInstance.instance.levels.children[0];
  72. const et = levelNode.getChildByName('et');
  73. this._index++;
  74. if(this._index >= et.children.length){
  75. this._index = 0;
  76. }
  77. resolve(et.children[this._index]);
  78. // resolve(et.children[1]);
  79. });
  80. }
  81. protected onDestroy(): void {
  82. this.unregisterEvent();
  83. }
  84. }