RadarComponent.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. }
  36. }
  37. private cancelRadar(){
  38. console.log('取消雷达');
  39. this.node.active = false;
  40. this._shouldUpdatePosition = false;
  41. }
  42. public lockPositionUpdate() {
  43. this._shouldUpdatePosition = false;
  44. }
  45. public unlockPositionUpdate() {
  46. this._shouldUpdatePosition = true;
  47. }
  48. update(deltaTime: number) {
  49. if(this._shouldUpdatePosition && this._targetNode) {
  50. const battleUI = AliensGlobalInstance.instance.battleUI;
  51. const camera = this.node.scene.getComponentInChildren(Camera);
  52. const localPos = GameUtil.worldToScreenLocal(this._targetNode, battleUI, camera);
  53. this.node.setPosition(localPos);
  54. }
  55. }
  56. private async getSceneCamera() :Promise<Camera>{
  57. return new Promise<Camera>((resolve, reject) => {
  58. const levelNode = AliensGlobalInstance.instance.levels.children[0];
  59. if(!levelNode){return;}
  60. const camera = levelNode.getComponentInChildren(Camera)!;
  61. resolve(camera);
  62. });
  63. }
  64. //获取目标节点
  65. private async getTargetNode():Promise<Node> {
  66. return new Promise<Node>((resolve, reject) => {
  67. const levelNode = AliensGlobalInstance.instance.levels.children[0];
  68. const et = levelNode.getChildByName('et');
  69. resolve(et.children[Math.floor(Math.random() * et.children.length)]);
  70. // resolve(et.children[1]);
  71. });
  72. }
  73. protected onDestroy(): void {
  74. this.unregisterEvent();
  75. }
  76. }