GameStartMove.ts 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. import { _decorator, Component, Node, Vec3, tween, Tween } from 'cc';
  2. const { ccclass, property } = _decorator;
  3. /**
  4. * GameStartMove
  5. *
  6. * This component is expected to be attached to the main camera node.
  7. * It provides high-level animation methods for block selection mode transitions:
  8. * 1. enterBlockSelectionMode() – complete animation for entering block selection (camera down + UI slide up)
  9. * 2. exitBlockSelectionMode() – complete animation for exiting block selection (camera up + UI slide down)
  10. *
  11. * Also provides low-level methods for specific use cases:
  12. * - moveDownInstant() – instantly move the camera down by a fixed offset
  13. * - moveUpSmooth() – move the camera back up with a smooth tween animation
  14. * - slideUpFromBottom() – slide UI up from bottom
  15. * - slideDibanDownAndHide() – slide UI down and hide
  16. */
  17. @ccclass('GameStartMove')
  18. export class GameStartMove extends Component {
  19. /** The camera node to move. Defaults to the node the script is attached to. */
  20. @property({
  21. type: Node,
  22. tooltip: 'Camera node to move. Leave empty to use the current node.'
  23. })
  24. public cameraNode: Node = null;
  25. /** Offset (in pixels/units) for one step of movement. */
  26. @property({ tooltip: 'Vertical offset for the camera movement.' })
  27. public offset: number = 182;
  28. /** Tween duration for the smooth move-up animation. */
  29. @property({ tooltip: 'Duration for the smooth move-up tween (seconds).' })
  30. public moveDuration: number = 0.4;
  31. private _originalPos: Vec3 = new Vec3();
  32. private _originalDibanPos: Vec3 = new Vec3();
  33. onLoad () {
  34. // Use self node if cameraNode not assigned via the editor.
  35. if (!this.cameraNode) {
  36. this.cameraNode = this.node;
  37. }
  38. // Save initial position so we can always restore relative to it.
  39. this._originalPos.set(this.cameraNode.position);
  40. // Save diban original position if available
  41. if (this.dibanNode) {
  42. this._originalDibanPos.set(this.dibanNode.position);
  43. }
  44. }
  45. /**
  46. * Instantly move the camera down by `offset` units.
  47. */
  48. public moveDownInstant () {
  49. if (!this.cameraNode) return;
  50. const pos = this.cameraNode.position.clone();
  51. pos.y -= this.offset;
  52. this.cameraNode.setPosition(pos);
  53. }
  54. /**
  55. * Smoothly move the camera back up by `offset` units using a tween.
  56. */
  57. public moveUpSmooth () {
  58. if (!this.cameraNode) return;
  59. const startPos = this.cameraNode.position.clone();
  60. const targetPos = new Vec3(startPos.x, startPos.y + this.offset, startPos.z);
  61. // Stop any running tweens on this node to avoid conflicting animations.
  62. Tween.stopAllByTarget(this.cameraNode);
  63. tween(this.cameraNode)
  64. .to(this.moveDuration, { position: targetPos }, { easing: 'quadOut' })
  65. .start();
  66. }
  67. /* ========= BlockSelectionUI 动画 ========= */
  68. /** BlockSelectionUI 根节点(外部在编辑器拖拽赋值) */
  69. @property({ type: Node, tooltip: 'BlockSelectionUI 根节点' })
  70. public blockSelectionUI: Node = null;
  71. /** BlockSelectionUI 中需要下滑的 diban 节点(外部拖拽赋值) */
  72. @property({ type: Node, tooltip: 'BlockSelectionUI 中的 diban 节点' })
  73. public dibanNode: Node = null;
  74. /**
  75. * 下滑 diban 并在动画结束后隐藏 BlockSelectionUI。
  76. * @param distance 向下移动距离,默认 300
  77. * @param duration 动画时长,默认 0.3s
  78. */
  79. public slideDibanDownAndHide(distance: number = 300, duration: number = 0.3) {
  80. console.log('GameStartMove.slideDibanDownAndHide 开始执行');
  81. if (!this.dibanNode || !this.blockSelectionUI) {
  82. console.log('GameStartMove.slideDibanDownAndHide 条件检查失败:', {
  83. dibanNode: !!this.dibanNode,
  84. blockSelectionUI: !!this.blockSelectionUI
  85. });
  86. return;
  87. }
  88. // 使用存储的原始位置,如果没有存储则使用当前位置
  89. let originalPos: Vec3;
  90. if (this._originalDibanPos.equals(Vec3.ZERO)) {
  91. // 如果没有存储原始位置,使用当前位置并存储它
  92. originalPos = this.dibanNode.position.clone();
  93. this._originalDibanPos = originalPos.clone();
  94. console.log('GameStartMove.slideDibanDownAndHide 首次使用,存储原始位置:', this._originalDibanPos);
  95. } else {
  96. // 使用存储的原始位置
  97. originalPos = this._originalDibanPos.clone();
  98. console.log('GameStartMove.slideDibanDownAndHide 使用存储的原始位置:', originalPos);
  99. }
  100. // 获取当前位置作为动画起始位置
  101. const currentPos = this.dibanNode.position.clone();
  102. console.log('GameStartMove.slideDibanDownAndHide 当前位置:', currentPos);
  103. // 停止现有 tween
  104. Tween.stopAllByTarget(this.dibanNode);
  105. const targetPos = new Vec3(currentPos.x, currentPos.y - distance, currentPos.z);
  106. console.log('GameStartMove.slideDibanDownAndHide 目标位置:', targetPos);
  107. tween(this.dibanNode)
  108. .to(duration, { position: targetPos }, { easing: 'quadIn' })
  109. .call(() => {
  110. console.log('GameStartMove.slideDibanDownAndHide 动画完成回调');
  111. // 先隐藏BlockSelectionUI,然后立即重置diban位置到存储的原始位置
  112. // 这样用户就看不到位置重置的过程
  113. this.blockSelectionUI.active = false;
  114. this.dibanNode.setPosition(originalPos);
  115. console.log('GameStartMove.slideDibanDownAndHide UI已隐藏,位置已重置到:', originalPos);
  116. })
  117. .start();
  118. console.log('GameStartMove.slideDibanDownAndHide 动画已启动');
  119. }
  120. /**
  121. * 从底部上滑显示 diban
  122. * @param distance 从下方移动的距离,默认 300
  123. * @param duration 动画时长,默认 0.3s
  124. */
  125. public slideUpFromBottom(distance: number = 300, duration: number = 0.3) {
  126. console.log('GameStartMove.slideUpFromBottom 开始执行');
  127. if (!this.dibanNode || !this.blockSelectionUI) {
  128. console.log('GameStartMove.slideUpFromBottom 条件检查失败:', {
  129. dibanNode: !!this.dibanNode,
  130. blockSelectionUI: !!this.blockSelectionUI
  131. });
  132. return;
  133. }
  134. // 确保BlockSelectionUI可见
  135. this.blockSelectionUI.active = true;
  136. // 使用存储的原始位置作为目标位置
  137. let targetPos: Vec3;
  138. if (this._originalDibanPos.equals(Vec3.ZERO)) {
  139. // 如果没有存储原始位置,使用当前位置并存储它
  140. targetPos = this.dibanNode.position.clone();
  141. this._originalDibanPos = targetPos.clone();
  142. console.log('GameStartMove.slideUpFromBottom 首次使用,存储原始位置:', this._originalDibanPos);
  143. } else {
  144. // 使用存储的原始位置
  145. targetPos = this._originalDibanPos.clone();
  146. console.log('GameStartMove.slideUpFromBottom 使用存储的原始位置:', targetPos);
  147. }
  148. // 设置初始位置(在目标位置下方)
  149. const startPos = new Vec3(targetPos.x, targetPos.y - distance, targetPos.z);
  150. this.dibanNode.setPosition(startPos);
  151. console.log('GameStartMove.slideUpFromBottom 起始位置:', startPos);
  152. // 停止现有 tween
  153. Tween.stopAllByTarget(this.dibanNode);
  154. // 播放上滑动画到目标位置
  155. tween(this.dibanNode)
  156. .to(duration, { position: targetPos }, { easing: 'quadOut' })
  157. .call(() => {
  158. console.log('GameStartMove.slideUpFromBottom 动画完成');
  159. })
  160. .start();
  161. console.log('GameStartMove.slideUpFromBottom 动画已启动');
  162. }
  163. /**
  164. * 更新保存的diban原始位置(当diban节点在运行时改变时调用)
  165. */
  166. public updateDibanOriginalPosition() {
  167. if (this.dibanNode) {
  168. this._originalDibanPos.set(this.dibanNode.position);
  169. console.log('GameStartMove.updateDibanOriginalPosition 存储原始位置:', this._originalDibanPos);
  170. } else {
  171. console.log('GameStartMove.updateDibanOriginalPosition dibanNode为空,无法存储位置');
  172. }
  173. }
  174. /* ========= 高级动画方法 ========= */
  175. /**
  176. * 进入方块选择模式的完整动画
  177. * 包含摄像机下移和UI上滑入场
  178. * @param distance UI移动距离,默认 300
  179. * @param duration UI动画时长,默认 0.3s
  180. */
  181. public enterBlockSelectionMode(distance: number = 300, duration: number = 0.3) {
  182. console.log('GameStartMove.enterBlockSelectionMode 开始执行进入方块选择模式动画');
  183. // 摄像机瞬间下移
  184. this.moveDownInstant();
  185. // 方块选择UI从底部上滑入场
  186. this.slideUpFromBottom(distance, duration);
  187. console.log('GameStartMove.enterBlockSelectionMode 动画已启动');
  188. }
  189. /**
  190. * 退出方块选择模式的完整动画
  191. * 包含摄像机上移和UI下滑隐藏
  192. * @param distance UI移动距离,默认 300
  193. * @param duration UI动画时长,默认 0.3s
  194. */
  195. public exitBlockSelectionMode(distance: number = 300, duration: number = 0.3) {
  196. console.log('GameStartMove.exitBlockSelectionMode 开始执行退出方块选择模式动画');
  197. // 摄像机平滑上移
  198. this.moveUpSmooth();
  199. // 方块选择UI下滑并隐藏
  200. this.slideDibanDownAndHide(distance, duration);
  201. console.log('GameStartMove.exitBlockSelectionMode 动画已启动');
  202. }
  203. }