1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- import { _decorator, Component, Node, Vec3 } from 'cc';
- import { EventDispatcher } from 'db://assets/core_tgx/easy_ui_framework/EventDispatcher';
- import { GameEvent } from '../Enum/GameEvent';
- import { AliensGlobalInstance } from '../AliensGlobalInstance';
- const { ccclass, property } = _decorator;
- @ccclass('RadarComponent')
- export class RadarComponent extends Component {
- //渲染的目标节点
- private _targetNode: Node = null!;
- start() {
- this.registerEvent();
- }
- private registerEvent(){
- EventDispatcher.instance.on(GameEvent.EVENT_CAMERA_SCREENSHOT_RADAR,this.onRadar,this);
- }
- private unregisterEvent(){
- EventDispatcher.instance.off(GameEvent.EVENT_CAMERA_SCREENSHOT_RADAR,this.onRadar,this);
- }
- private async onRadar(){
- this.node.active = true;
- //获取目标节点
- this._targetNode = await this.getTargetNode();
-
- // 添加雷达显示逻辑
- if(this._targetNode){
- // 计算目标节点在3D世界中的位置
- const targetPos = this._targetNode.worldPosition;
-
- // 将3D坐标转换为2D雷达坐标 (500x500范围内)
- const radarPos = new Vec3(
- (targetPos.x / 10) * 250 + 250, // 将x坐标映射到0-500范围
- (targetPos.z / 10) * 250 + 250, // 将z坐标映射到0-500范围
- 0
- );
-
- // 限制在雷达范围内
- radarPos.x = Math.max(0, Math.min(500, radarPos.x));
- radarPos.y = Math.max(0, Math.min(500, radarPos.y));
-
- // 移动雷达指示器到目标位置
- this.node.setPosition(Vec3.ZERO);
- }
- }
- // ... 其他代码保持不变 ...
- //获取目标节点
- private async getTargetNode():Promise<Node> {
- return new Promise<Node>((resolve, reject) => {
- const levelNode = AliensGlobalInstance.instance.levels.children[0];
- const et = levelNode.getChildByName('et');
- resolve(et.children[Math.floor(Math.random() * et.children.length)]);
- });
- }
- protected onDestroy(): void {
- this.unregisterEvent();
- }
- }
|