RadarComponent.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. start() {
  12. this.registerEvent();
  13. }
  14. private registerEvent(){
  15. EventDispatcher.instance.on(GameEvent.EVENT_CAMERA_SCREENSHOT_RADAR,this.onRadar,this);
  16. }
  17. private unregisterEvent(){
  18. EventDispatcher.instance.off(GameEvent.EVENT_CAMERA_SCREENSHOT_RADAR,this.onRadar,this);
  19. }
  20. private async onRadar(){
  21. this.node.active = true;
  22. //获取相机
  23. const camera = await this.getSceneCamera();
  24. //获取目标节点
  25. this._targetNode = await this.getTargetNode();
  26. if(this._targetNode){
  27. const battleUI = AliensGlobalInstance.instance.battleUI;
  28. const localPos = GameUtil.worldToScreenLocal(this._targetNode,battleUI,camera);
  29. // 移动雷达指示器
  30. this.node.setPosition(localPos);
  31. }
  32. }
  33. private async getSceneCamera() :Promise<Camera>{
  34. return new Promise<Camera>((resolve, reject) => {
  35. const levelNode = AliensGlobalInstance.instance.levels.children[0];
  36. if(!levelNode){return;}
  37. const camera = levelNode.getComponentInChildren(Camera)!;
  38. resolve(camera);
  39. });
  40. }
  41. //获取目标节点
  42. private async getTargetNode():Promise<Node> {
  43. return new Promise<Node>((resolve, reject) => {
  44. const levelNode = AliensGlobalInstance.instance.levels.children[0];
  45. const et = levelNode.getChildByName('et');
  46. // resolve(et.children[Math.floor(Math.random() * et.children.length)]);
  47. resolve(et.children[1]);
  48. });
  49. }
  50. protected onDestroy(): void {
  51. this.unregisterEvent();
  52. }
  53. }