RadarComponent.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { _decorator, Component, Node, Vec3 } 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. const { ccclass, property } = _decorator;
  6. @ccclass('RadarComponent')
  7. export class RadarComponent extends Component {
  8. //渲染的目标节点
  9. private _targetNode: Node = null!;
  10. start() {
  11. this.registerEvent();
  12. }
  13. private registerEvent(){
  14. EventDispatcher.instance.on(GameEvent.EVENT_CAMERA_SCREENSHOT_RADAR,this.onRadar,this);
  15. }
  16. private unregisterEvent(){
  17. EventDispatcher.instance.off(GameEvent.EVENT_CAMERA_SCREENSHOT_RADAR,this.onRadar,this);
  18. }
  19. private async onRadar(){
  20. this.node.active = true;
  21. //获取目标节点
  22. this._targetNode = await this.getTargetNode();
  23. // 添加雷达显示逻辑
  24. if(this._targetNode){
  25. // 计算目标节点在3D世界中的位置
  26. const targetPos = this._targetNode.worldPosition;
  27. // 将3D坐标转换为2D雷达坐标 (500x500范围内)
  28. const radarPos = new Vec3(
  29. (targetPos.x / 10) * 250 + 250, // 将x坐标映射到0-500范围
  30. (targetPos.z / 10) * 250 + 250, // 将z坐标映射到0-500范围
  31. 0
  32. );
  33. // 限制在雷达范围内
  34. radarPos.x = Math.max(0, Math.min(500, radarPos.x));
  35. radarPos.y = Math.max(0, Math.min(500, radarPos.y));
  36. // 移动雷达指示器到目标位置
  37. this.node.setPosition(Vec3.ZERO);
  38. }
  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. });
  48. }
  49. protected onDestroy(): void {
  50. this.unregisterEvent();
  51. }
  52. }