OutArea.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import { _decorator, Component, Node, tween, Vec3 } from 'cc';
  2. const { ccclass, property, executeInEditMode } = _decorator;
  3. @ccclass('OutArea')
  4. @executeInEditMode
  5. export class OutArea extends Component {
  6. @property(Node)
  7. outNodes: Node = null!;
  8. // 直接使用outNodes的子节点管理
  9. get cups() {
  10. return this.outNodes.children;
  11. }
  12. start() {
  13. }
  14. update(deltaTime: number) {
  15. }
  16. // 排列杯子
  17. arrangeCups() {
  18. const startX = 40; //水杯的宽度一半
  19. const spacing = 80;
  20. this.cups.forEach((cup, index) => {
  21. cup.setPosition(startX + index * spacing, 0, 0);
  22. });
  23. }
  24. addCup(cup: Node) {
  25. cup.setParent(this.outNodes);
  26. this.arrangeCups();
  27. }
  28. removeCup(cup: Node) {
  29. cup.removeFromParent();
  30. this.arrangeCups();
  31. }
  32. getCups() {
  33. return this.cups;
  34. }
  35. // 添加带动画排列方法
  36. arrangeCupsWithAnimation() {
  37. const startX = -200; // 从左侧开始
  38. const spacing = 120; // 正间距向右排列
  39. this.cups.forEach((cup, index) => {
  40. const targetPos = new Vec3(startX + index * spacing, 0, 0);
  41. if (!cup.position.equals(targetPos)) {
  42. tween(cup)
  43. .to(0.3, { position: targetPos }, { easing: 'sineOut' })
  44. .start();
  45. }
  46. });
  47. }
  48. }