RadarComponent.ts 3.4 KB

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