1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- import { _decorator, Camera, Component, Node, UITransform, Vec3, view } from 'cc';
- import { EventDispatcher } from 'db://assets/core_tgx/easy_ui_framework/EventDispatcher';
- import { GameEvent } from '../Enum/GameEvent';
- import { AliensGlobalInstance } from '../AliensGlobalInstance';
- import { GameUtil } from '../GameUtil';
- 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;
- //获取相机
- const camera = await this.getSceneCamera();
- //获取目标节点
- this._targetNode = await this.getTargetNode();
-
- if(this._targetNode){
- const battleUI = AliensGlobalInstance.instance.battleUI;
- const localPos = GameUtil.worldToScreenLocal(this._targetNode,battleUI,camera);
-
- // 移动雷达指示器
- this.node.setPosition(localPos);
- }
- }
- private async getSceneCamera() :Promise<Camera>{
- return new Promise<Camera>((resolve, reject) => {
- const levelNode = AliensGlobalInstance.instance.levels.children[0];
- if(!levelNode){return;}
- const camera = levelNode.getComponentInChildren(Camera)!;
- resolve(camera);
- });
- }
- //获取目标节点
- 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)]);
- resolve(et.children[1]);
- });
- }
- protected onDestroy(): void {
- this.unregisterEvent();
- }
- }
|