ScreenShotComponent.ts 5.1 KB

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