RadarComponent.ts 3.5 KB

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