CheckpointGuideViewComp.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import { Camera, Tween, Vec3, _decorator, director, tween } from "cc";
  2. import { ecs } from "../../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS";
  3. import { CCComp } from "../../../../../extensions/oops-plugin-framework/assets/module/common/CCComp";
  4. import { smc } from "../../common/SingletonModuleComp";
  5. import { Puppet } from "../../puppet/puppet";
  6. import { oops } from "../../../../../extensions/oops-plugin-framework/assets/core/Oops";
  7. import { UIID } from "../../common/config/GameUIConfig";
  8. import { GameEvent } from "../../common/config/GameEvent";
  9. const { ccclass, property } = _decorator;
  10. /** 角色信息界面 */
  11. @ccclass('CheckpointGuideViewComp')
  12. @ecs.register('CheckpointGuideView', false)
  13. export class CheckpointGuideViewComp extends CCComp {
  14. private btn_gesture
  15. private step = 0;
  16. @property(Camera)
  17. public mainCamera: Camera = null; // 主摄像机
  18. onAdded(args: any) {
  19. console.log(args);
  20. this.on(GameEvent.PuppetClick, this.CheckGuide, this)
  21. const cameras = director.getScene().getComponentsInChildren(Camera);
  22. this.mainCamera = cameras.find(camera => camera.node.name === 'MainCamera');
  23. }
  24. onLoad() {
  25. this.btn_gesture = this.node.getChildByPath("btn_gesture")
  26. this.btn_gesture.active = false
  27. this.step++
  28. this.showGuideStep(this.step)
  29. }
  30. runAnimation() {
  31. let startPosition = this.btn_gesture.position.clone(); // 初始位置
  32. let endPosition = new Vec3(startPosition.x, startPosition.y + 10, startPosition.z); // 目标位置,这里以向上移动100个单位为例
  33. Tween.stopAllByTarget(this.btn_gesture); // 停止该节点上的所有其他Tween
  34. // 创建一个循环的上下移动动画
  35. tween(this.btn_gesture)
  36. .to(1, { position: endPosition }, { easing: 'sineInOut' })
  37. .to(1, { position: startPosition }, { easing: 'sineInOut' })
  38. .union()
  39. .repeatForever()
  40. .start();
  41. }
  42. showGuideStep(index: number) {
  43. if (this.step <= 3) {
  44. const checkpoint = smc.initialize.account.checkpoint
  45. const grid = checkpoint.CheckpointModel.grids[0][index - 1]
  46. if (grid) {
  47. const puppet = checkpoint.CheckpointModel.cells[0][index - 1] as Puppet
  48. if (puppet) {
  49. const pos = grid.GridView.node.position.clone()
  50. this.showGesture(pos)
  51. } else {
  52. this.step++
  53. this.showGuideStep(this.step);
  54. }
  55. }
  56. } else {
  57. // 引导完成后隐藏引导层
  58. oops.gui.remove(UIID.Guide)
  59. }
  60. }
  61. CheckGuide(event: string, args: any): void {
  62. switch (event) {
  63. case GameEvent.PuppetClick:
  64. const [x, y] = args
  65. if (y + 1 === this.step) {
  66. this.step++
  67. this.showGuideStep(this.step)
  68. }
  69. break;
  70. default:
  71. break;
  72. }
  73. }
  74. showGesture(position: Vec3): void {
  75. this.btn_gesture.active = true
  76. const worldPosition = position; // 获取3D目标的世界坐标
  77. const screenPosition = new Vec3();
  78. this.mainCamera.convertToUINode(worldPosition, this.btn_gesture.parent, screenPosition); // 转换坐标
  79. this.btn_gesture.setPosition(screenPosition.x, screenPosition.y, 0); // 更新手势Sprite位置
  80. this.runAnimation()
  81. }
  82. reset() {
  83. this.off(GameEvent.PuppetClick)
  84. this.node.destroy();
  85. }
  86. }