CharacterMovementManager.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. import { _decorator, Component, Node, Button, Vec3, tween, Tween } from 'cc';
  2. import { DialogueManager } from './DialogueManager';
  3. const { ccclass, property } = _decorator;
  4. @ccclass('CharacterMovementManager')
  5. export class CharacterMovementManager extends Component {
  6. @property({
  7. type: DialogueManager,
  8. tooltip: '对话框管理器'
  9. })
  10. dialogueManager: DialogueManager = null;
  11. @property({
  12. type: Node,
  13. tooltip: '人物节点'
  14. })
  15. characterNode: Node = null;
  16. @property({
  17. type: Button,
  18. tooltip: '放行按钮(向右移动)'
  19. })
  20. letPassButton: Button = null;
  21. @property({
  22. type: Button,
  23. tooltip: '赶走按钮(向左移动)'
  24. })
  25. dismissButton: Button = null;
  26. @property({
  27. tooltip: '人物移动动画持续时间(秒)'
  28. })
  29. moveDuration: number = 1.0;
  30. @property({
  31. tooltip: '人物移动距离(像素)'
  32. })
  33. moveDistance: number = 300;
  34. private initialPosition: Vec3 = null;
  35. private currentAnimation: Tween<Node> = null;
  36. start() {
  37. // 记录人物初始位置
  38. if (this.characterNode) {
  39. this.initialPosition = this.characterNode.position.clone();
  40. }
  41. // 设置按钮事件监听
  42. if (this.letPassButton) {
  43. this.letPassButton.node.on(Button.EventType.CLICK, this.moveCharacterRight, this);
  44. }
  45. if (this.dismissButton) {
  46. this.dismissButton.node.on(Button.EventType.CLICK, this.moveCharacterLeft, this);
  47. }
  48. }
  49. /**
  50. * 人物移动到右侧(放行)
  51. */
  52. public moveCharacterRight(): void {
  53. if (!this.characterNode || !this.initialPosition) return;
  54. // 隐藏对话框
  55. if (this.dialogueManager) {
  56. this.dialogueManager.hideDialogue();
  57. }
  58. // 停止当前动画
  59. if (this.currentAnimation) {
  60. this.currentAnimation.stop();
  61. }
  62. // 目标位置:向右移动
  63. const targetPos = new Vec3(
  64. this.initialPosition.x + this.moveDistance,
  65. this.initialPosition.y,
  66. this.initialPosition.z
  67. );
  68. // 创建动画
  69. this.currentAnimation = tween(this.characterNode)
  70. .to(this.moveDuration, { position: targetPos }, { easing: 'cubicOut' })
  71. .call(() => {
  72. // 动画完成回调
  73. this.currentAnimation = null;
  74. })
  75. .start();
  76. }
  77. /**
  78. * 人物移动到左侧(赶走)
  79. */
  80. public moveCharacterLeft(): void {
  81. if (!this.characterNode || !this.initialPosition) return;
  82. // 隐藏对话框
  83. if (this.dialogueManager) {
  84. this.dialogueManager.hideDialogue();
  85. }
  86. // 停止当前动画
  87. if (this.currentAnimation) {
  88. this.currentAnimation.stop();
  89. }
  90. // 目标位置:向左移动
  91. const targetPos = new Vec3(
  92. this.initialPosition.x - this.moveDistance,
  93. this.initialPosition.y,
  94. this.initialPosition.z
  95. );
  96. // 创建动画
  97. this.currentAnimation = tween(this.characterNode)
  98. .to(this.moveDuration, { position: targetPos }, { easing: 'cubicOut' })
  99. .call(() => {
  100. // 动画完成回调
  101. this.currentAnimation = null;
  102. })
  103. .start();
  104. }
  105. /**
  106. * 新人物从左向右进入
  107. */
  108. public characterEnter(): void {
  109. if (!this.characterNode || !this.initialPosition) return;
  110. // 隐藏对话框
  111. if (this.dialogueManager) {
  112. this.dialogueManager.hideDialogue();
  113. }
  114. // 停止当前动画
  115. if (this.currentAnimation) {
  116. this.currentAnimation.stop();
  117. }
  118. // 设置起始位置(在左侧)
  119. const startPos = new Vec3(
  120. this.initialPosition.x - this.moveDistance,
  121. this.initialPosition.y,
  122. this.initialPosition.z
  123. );
  124. this.characterNode.position = startPos;
  125. // 创建动画,移动到初始位置
  126. this.currentAnimation = tween(this.characterNode)
  127. .to(this.moveDuration, { position: this.initialPosition }, { easing: 'cubicOut' })
  128. .call(() => {
  129. // 动画完成回调
  130. this.currentAnimation = null;
  131. })
  132. .start();
  133. }
  134. /**
  135. * 重置人物位置到初始位置
  136. */
  137. public resetCharacterPosition(): void {
  138. if (this.characterNode && this.initialPosition) {
  139. // 停止当前动画
  140. if (this.currentAnimation) {
  141. this.currentAnimation.stop();
  142. this.currentAnimation = null;
  143. }
  144. // 直接设置到初始位置
  145. this.characterNode.position = this.initialPosition.clone();
  146. }
  147. }
  148. onDestroy() {
  149. // 移除按钮事件监听
  150. if (this.letPassButton) {
  151. this.letPassButton.node.off(Button.EventType.CLICK, this.moveCharacterRight, this);
  152. }
  153. if (this.dismissButton) {
  154. this.dismissButton.node.off(Button.EventType.CLICK, this.moveCharacterLeft, this);
  155. }
  156. }
  157. }