LevelAction.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import { _decorator, BoxCollider2D, Button, CircleCollider2D, Collider2D, Component, find, instantiate, Node, NodeEventType, view } from 'cc';
  2. import { resLoader, ResLoader } from '../../core_tgx/base/ResLoader';
  3. import { CupHeight, TakeGobletGlobalInstance, WaterColors } from './TakeGobletGlobalInstance';
  4. import { OutArea } from './Component/OutArea';
  5. import { WaitArea } from './Component/WaitArea';
  6. import { CocktailCup } from './Component/CocktailCup';
  7. const { ccclass, property } = _decorator;
  8. @ccclass('LevelAction')
  9. export class LevelAction extends Component {
  10. @property(OutArea)
  11. outArea: OutArea = null!; // 直接引用OutArea组件
  12. @property(WaitArea)
  13. waitArea: WaitArea = null!; // 直接引用WaitArea组件
  14. @property(Node)
  15. tempCups: Node = null!; //临时杯
  16. @property(Node)
  17. goblets: Node = null!; //原浆区
  18. start() {
  19. this.registerListener();
  20. this.generateInitialCups(); // 自动初始化
  21. }
  22. registerListener() {
  23. }
  24. private async generateInitialCups() {
  25. const instance = TakeGobletGlobalInstance.instance;
  26. const configs = instance.getInitialCupsConfig();
  27. const allCups: Node[] = [];
  28. for (const config of configs) {
  29. for (let i = 0; i < config.count; i++) {
  30. const prefab = await instance.loadAsyncCocktail(config.height);
  31. const cupNode = instantiate(prefab);
  32. const cup = cupNode.getComponent(CocktailCup)!;
  33. // 设置颜色并重置水位
  34. cup.cupColor = instance.getRandomColor();
  35. cup.reset();
  36. allCups.push(cupNode);
  37. }
  38. }
  39. console.log('allCups: ', allCups.length);
  40. // 分配初始位置
  41. allCups.slice(0, 2).forEach(cup => this.outArea.addCup(cup));
  42. allCups.slice(2).forEach(cup => this.waitArea.addCup(cup));
  43. }
  44. // 当调酒区杯子被消除时调用
  45. public async handleCupsRemoved(count: number) {
  46. for (let i = 0; i < count; i++) {
  47. const cup = this.waitArea.takeCup();
  48. if (cup) {
  49. this.outArea.addCup(cup);
  50. }
  51. }
  52. }
  53. }