ScreenShotComponent.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import { _decorator, Camera, Component, Node, Quat, RenderTexture, Sprite, SpriteFrame, Vec3 } from 'cc';
  2. import { AliensGlobalInstance } from '../AliensGlobalInstance';
  3. import { EventDispatcher } from 'db://assets/core_tgx/easy_ui_framework/EventDispatcher';
  4. import { GameEvent } from '../Enum/GameEvent';
  5. import { GameUtil } from '../GameUtil';
  6. import { tgxUITips } from 'db://assets/core_tgx/tgx';
  7. const { ccclass, property } = _decorator;
  8. /** 截图组件脚本*/
  9. @ccclass('ScreenShotComponent')
  10. export class ScreenShotComponent extends Component {
  11. @property(Sprite)
  12. public sprite: Sprite = null!;
  13. _renderTex: RenderTexture | null = null;
  14. private _originalTargetTexture: RenderTexture | null = null;
  15. private _originalCameraPosition: Vec3 = new Vec3();
  16. // private _originalCameraRotation: Vec3 = new Vec3();
  17. private _originalCameraRotation: Quat = new Quat();
  18. private _shouldFlipImage: boolean = true;
  19. private _index:number = 0;
  20. //渲染的目标节点
  21. private _targetNode: Node = null!;
  22. protected onLoad(): void {
  23. this.registerEvent();
  24. }
  25. private registerEvent(){
  26. EventDispatcher.instance.on(GameEvent.EVENT_CAMERA_SCREENSHOT,this.screenShot,this);
  27. EventDispatcher.instance.on(GameEvent.EVENT_CAMERA_SHOOT_ENEMY,this.shootEnemy,this);
  28. }
  29. private unregisterEvent(){
  30. EventDispatcher.instance.off(GameEvent.EVENT_CAMERA_SCREENSHOT,this.screenShot,this);
  31. EventDispatcher.instance.off(GameEvent.EVENT_CAMERA_SHOOT_ENEMY,this.shootEnemy,this);
  32. }
  33. //截图
  34. public async screenShot() {
  35. this.node.active = true;
  36. const camera = await this.getSceneCamera();
  37. this._targetNode = await this.getTargetNode();
  38. if (!this._targetNode) return;
  39. // 创建新的RenderTexture
  40. const renderTex = new RenderTexture();
  41. renderTex.reset({
  42. width: 150,
  43. height: 110,
  44. });
  45. // 创建新的SpriteFrame并设置翻转
  46. const newSpriteFrame = new SpriteFrame();
  47. newSpriteFrame.texture = renderTex;
  48. // console.log(`this._shouldFlipImage:${this._shouldFlipImage}`);
  49. newSpriteFrame.flipUVY = this._shouldFlipImage; // 使用SpriteFrame的翻转功能
  50. // 保存原始相机状态
  51. this._originalTargetTexture = camera.targetTexture;
  52. camera.node.getWorldPosition(this._originalCameraPosition);
  53. camera.node.getWorldRotation(this._originalCameraRotation);
  54. // 设置相机位置和朝向
  55. const targetPos = this._targetNode.worldPosition.clone();
  56. const cameraPos = targetPos.add(new Vec3(0, 0, 10));
  57. camera.node.setWorldPosition(cameraPos);
  58. camera.node.lookAt(targetPos, Vec3.UP);
  59. camera.targetTexture = renderTex;
  60. // 等待一帧确保渲染完成
  61. await new Promise(resolve => this.scheduleOnce(resolve, 0));
  62. // 更新Sprite显示
  63. this.sprite.spriteFrame = newSpriteFrame;
  64. this.sprite.markForUpdateRenderData(true);
  65. // 恢复相机状态
  66. camera.targetTexture = this._originalTargetTexture;
  67. camera.node.setWorldPosition(this._originalCameraPosition);
  68. camera.node.setRotation(this._originalCameraRotation);
  69. }
  70. private _shootCount: number = 0;
  71. //击杀了场景怪物 隐藏侦探节点
  72. private shootEnemy(enemy:Node){
  73. if(!this.node.active || !this._targetNode) return;
  74. if(enemy == this._targetNode){
  75. this.scheduleOnce(() => {
  76. tgxUITips.show('击杀的怪物是侦探上的!');
  77. this._shootCount++; // 增加计数
  78. if(this._shootCount > 0){
  79. this._shouldFlipImage = false;
  80. }
  81. this.node.active = false;
  82. },1);
  83. }
  84. }
  85. //获取场景相机
  86. private async getSceneCamera() :Promise<Camera>{
  87. return new Promise<Camera>((resolve, reject) => {
  88. const levelNode = AliensGlobalInstance.instance.levels.children[0];
  89. if(!levelNode){return;}
  90. const camera = levelNode.getComponentInChildren(Camera)!;
  91. resolve(camera);
  92. });
  93. }
  94. //获取目标节点
  95. private async getTargetNode():Promise<Node> {
  96. return new Promise<Node>((resolve, reject) => {
  97. const levelNode = AliensGlobalInstance.instance.levels.children[0];
  98. const et = levelNode.getChildByName('et');
  99. this._index++;
  100. if(this._index >= et.children.length){
  101. this._index = 0;
  102. }
  103. resolve(et.children[this._index]);
  104. });
  105. }
  106. //更新相机最新的位置和旋转角度
  107. public saveCameraState(pos: Vec3, rotation: Vec3) {
  108. this._originalCameraPosition = pos;
  109. // 将Vec3欧拉角转换为Quat
  110. const quat = new Quat();
  111. Quat.fromEuler(quat, rotation.x, rotation.y, rotation.z);
  112. this._originalCameraRotation.set(quat);
  113. }
  114. protected onDestroy(): void {
  115. this.unregisterEvent();
  116. }
  117. }