LevelAction.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. import { _decorator, BoxCollider2D, Button, CircleCollider2D, Collider2D, Component, find, instantiate, Node, NodeEventType, tween, view, Vec3, mat4, UITransform } 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. import { OriginCup } from './Component/OriginCup';
  8. import { Water } from './Component/Water';
  9. import { EventDispatcher } from '../../core_tgx/easy_ui_framework/EventDispatcher';
  10. import { GameEvent } from './Enum/GameEvent';
  11. import { TempCup } from './Component/TempCup';
  12. import { TempCups } from './Component/TempCups';
  13. const { ccclass, property } = _decorator;
  14. @ccclass('LevelAction')
  15. export class LevelAction extends Component {
  16. @property(OutArea)
  17. outArea: OutArea = null!; // 直接引用OutArea组件
  18. @property(WaitArea)
  19. waitArea: WaitArea = null!; // 直接引用WaitArea组件
  20. @property(Node)
  21. tempCups: Node = null!; //临时杯
  22. @property(Node)
  23. goblets: Node = null!; //原浆区
  24. private originCupPositions = new Map<string, Vec3>(); // 改用唯一ID记录
  25. start() {
  26. this.registerListener();
  27. this.generateInitialCups();
  28. }
  29. onDestroy() {
  30. this.unregisterListener();
  31. }
  32. registerListener() {
  33. EventDispatcher.instance.on(GameEvent.EVENT_CLICK_ORIGIN_CUP, this.handlePourOriginCup, this);
  34. EventDispatcher.instance.on(GameEvent.EVENT_ORIGIN_CUP_DESTROYED, (uuid: string) => {
  35. const cupNode = find(uuid) as Node;
  36. if (cupNode) {
  37. this.spawnNewOriginCup(cupNode);
  38. }
  39. }, this);
  40. EventDispatcher.instance.on(GameEvent.EVENT_COCKTAIL_CUP_DESTROYED, this.handleCupDestroyed, this);
  41. }
  42. unregisterListener() {
  43. EventDispatcher.instance.off(GameEvent.EVENT_CLICK_ORIGIN_CUP, this.handlePourOriginCup, this);
  44. EventDispatcher.instance.off(GameEvent.EVENT_ORIGIN_CUP_DESTROYED, this.spawnNewOriginCup, this);
  45. EventDispatcher.instance.off(GameEvent.EVENT_COCKTAIL_CUP_DESTROYED, this.handleCupDestroyed, this);
  46. }
  47. private async generateInitialCups() {
  48. const instance = TakeGobletGlobalInstance.instance;
  49. const configs = instance.getInitialCupsConfig();
  50. const allCups: Node[] = [];
  51. for (const config of configs) {
  52. for (let i = 0; i < config.count; i++) {
  53. const prefab = await instance.loadAsyncCocktail(config.height);
  54. const cupNode = instantiate(prefab);
  55. const cup = cupNode.getComponent(CocktailCup)!;
  56. // 设置颜色并重置水位
  57. cup.cupColor = instance.getRandomColor();
  58. cup.reset();
  59. allCups.push(cupNode);
  60. }
  61. }
  62. console.log('allCups: ', allCups.length);
  63. // 分配初始位置
  64. allCups.slice(0, 2).forEach(cup => this.outArea.addCup(cup));
  65. allCups.slice(2).forEach(cup => this.waitArea.addCup(cup));
  66. // 在分配完调酒杯后添加
  67. this.generateOriginCups();
  68. // 初始化暂存区
  69. this.tempCups.children.forEach(tempCupNode => {
  70. const tempCup = tempCupNode.getComponent(TempCup)!;
  71. tempCup.reset(); // 重置所有暂存杯
  72. });
  73. }
  74. // 当调酒区杯子被消除时调用
  75. public async handleCupsRemoved(count: number) {
  76. for (let i = 0; i < count; i++) {
  77. const cup = this.waitArea.takeCup();
  78. if (cup) {
  79. this.outArea.addCup(cup);
  80. }
  81. }
  82. }
  83. private generateOriginCups() {
  84. const outCups = this.outArea.getCups() as Node[];
  85. const waitCups = this.waitArea.getCups() as Node[];
  86. // DOTO 取前7个杯子颜色后期修改
  87. const allCups = [...outCups, ...waitCups].slice(0, 7);
  88. const colors = allCups.map(cup => {
  89. const comp = cup.getComponent(CocktailCup);
  90. return comp ? comp.cupColor : WaterColors.Blue;
  91. });
  92. this.goblets.children.forEach(originCupNode => {
  93. const originCup = originCupNode.getComponent(OriginCup)!;
  94. const waters = originCup.waters.children;
  95. for (let i = 0; i < originCup.cupHeight; i++) {
  96. const waterNode = waters[i];
  97. const water = waterNode.getComponent(Water);
  98. if (water) {
  99. water.initColor(colors[Math.floor(Math.random() * colors.length)]);
  100. waterNode.active = true;
  101. }
  102. }
  103. // 在生成初始原浆杯时记录位置
  104. const id = originCupNode.uuid; // 使用节点唯一ID
  105. this.originCupPositions.set(id, originCupNode.position.clone());
  106. // console.log('在生成初始原浆杯时记录位置: ', originCupNode.position);
  107. });
  108. }
  109. private findTargetCupInOutArea(color: WaterColors): { node: Node, comp: CocktailCup } | null {
  110. const validCups = this.outArea.getCups()
  111. .map(node => ({
  112. node,
  113. comp: node.getComponent(CocktailCup)!
  114. }))
  115. .filter(({ comp }) =>
  116. comp &&
  117. comp.cupColor === color &&
  118. !comp.isFull
  119. );
  120. if (validCups.length === 0) return null;
  121. const sorted = validCups.sort((a, b) => (a.comp.remainingCapacity ?? 0) - (b.comp.remainingCapacity ?? 0));
  122. return sorted[0];
  123. }
  124. public async handlePourOriginCup(originCup: OriginCup) {
  125. // 如果子节点0是底层,需要反转顺序
  126. const colors: WaterColors[] = [];
  127. for (let i = originCup.waters.children.length - 1; i >= 0; i--) {
  128. const waterNode = originCup.waters.children[i];
  129. if (waterNode.active) {
  130. const water = waterNode.getComponent(Water);
  131. colors.push(water.color);
  132. }
  133. }
  134. let hasUnprocessed = false; // 标记是否有未处理的水层
  135. for (const color of colors) {
  136. let targetNode: Node | null = this.findTargetCupInOutArea(color)?.node || null;
  137. let targetIsTemp = false;
  138. // 调酒区未找到,查找暂存区
  139. if (!targetNode) {
  140. const tempCupsComp = this.tempCups.getComponent(TempCups);
  141. if (!tempCupsComp) {
  142. console.error('TempCups component not found!');
  143. continue;
  144. }
  145. const tempCup = tempCupsComp.findAvailableTempCup();
  146. if (tempCup) {
  147. targetNode = tempCup.node;
  148. targetIsTemp = true;
  149. }
  150. }
  151. if (!targetNode) {
  152. hasUnprocessed = true;
  153. console.log(`颜色${WaterColors[color]}未找到可用杯子`);
  154. continue; // 继续尝试处理后续颜色
  155. }
  156. await this.pourAnimation(
  157. originCup.node,
  158. targetNode,
  159. color,
  160. targetIsTemp
  161. );
  162. // 更新目标杯
  163. if (targetIsTemp) {
  164. const tempCupComp = targetNode.getComponent(TempCup)!;
  165. tempCupComp.fill(color);
  166. } else {
  167. const cocktailCup = targetNode.getComponent(CocktailCup)!;
  168. cocktailCup.addLayer(color);
  169. }
  170. this.hideCurrentWaterLayer(originCup);
  171. }
  172. // 处理完所有颜色后检查剩余水层
  173. const remaining = originCup.waters.children.filter(n => n.active).length;
  174. if (hasUnprocessed || remaining > 0) {
  175. console.log("游戏结束:仍有未处理的水层");
  176. // 触发游戏结束逻辑
  177. } else {
  178. // 所有水层处理完毕,销毁原浆杯
  179. originCup.destroyOriginCup();
  180. }
  181. }
  182. private async pourAnimation(
  183. origin: Node,
  184. target: Node,
  185. color: WaterColors,
  186. isTempCup: boolean = false
  187. ) {
  188. // 使用正确的坐标转换
  189. const originParent = origin.parent!;
  190. const targetWorldPos = target.worldPosition;
  191. const localPos = originParent.getComponent(UITransform)!.convertToNodeSpaceAR(targetWorldPos);
  192. // 调整Y轴偏移量
  193. if (isTempCup) {
  194. localPos.y += 80; // 暂存杯偏移
  195. } else {
  196. localPos.y += 150; // 调酒杯偏移
  197. }
  198. // 只做单程移动,保留在目标位置
  199. await new Promise<void>(resolve => {
  200. tween(origin)
  201. .to(0.5, { position: localPos }) // 延长动画时间到0.5秒
  202. .call(() => resolve())
  203. .start();
  204. });
  205. }
  206. // 新增辅助方法
  207. private hideCurrentWaterLayer(originCup: OriginCup) {
  208. const activeWaters = originCup.waters.children.filter(n => n.active);
  209. if (activeWaters.length > 0) {
  210. const topIndex = activeWaters.length - 1;
  211. activeWaters[topIndex].active = false;
  212. }
  213. }
  214. private async spawnNewOriginCup(destroyedCup: Node) {
  215. // 获取被销毁杯子的初始位置
  216. const id = destroyedCup.uuid;
  217. const targetPos = this.originCupPositions.get(id) || Vec3.ZERO;
  218. // 创建新原浆杯
  219. const height = TakeGobletGlobalInstance.instance.generateOriginCupHeight();
  220. const prefab = await TakeGobletGlobalInstance.instance.loadAsyncOriginCup(height);
  221. const newCup = instantiate(prefab);
  222. // 设置初始位置(屏幕左侧)
  223. const uiTransform = this.node.getComponent(UITransform)!;
  224. newCup.setPosition(-uiTransform.width / 2, 0, 0);
  225. this.goblets.addChild(newCup);
  226. // 记录新杯子的初始位置
  227. this.originCupPositions.set(newCup.uuid, targetPos);
  228. // 移动动画到原位置
  229. tween(newCup)
  230. .to(0.5, { position: targetPos })
  231. .start();
  232. // DOTO 获取颜色配置
  233. const colors = this.getAvailableColors(5); // 获取前5个颜色
  234. this.setupOriginCupColors(newCup, colors);
  235. }
  236. // 获取可用颜色
  237. private getAvailableColors(count: number): WaterColors[] {
  238. const outCups = this.outArea.getCups();
  239. const waitCups = this.waitArea.getCups();
  240. const allCups = [...outCups, ...waitCups].slice(0, count);
  241. return allCups.map(cup => {
  242. const comp = cup.getComponent(CocktailCup);
  243. return comp ? comp.cupColor : WaterColors.Blue;
  244. });
  245. }
  246. // 设置原浆杯颜色
  247. private setupOriginCupColors(cupNode: Node, colors: WaterColors[]) {
  248. const originCup = cupNode.getComponent(OriginCup)!;
  249. const waters = originCup.waters.children;
  250. // 暂时写死5层水
  251. const waterCount = 5;
  252. for (let i = 0; i < waterCount; i++) {
  253. const waterNode = waters[i];
  254. if (!waterNode) continue;
  255. const water = waterNode.getComponent(Water)!;
  256. water.color = colors[Math.floor(Math.random() * colors.length)];
  257. waterNode.active = true;
  258. }
  259. }
  260. private handleCupDestroyed(destroyedCup: Node) {
  261. // 从outArea移除被销毁的杯子
  262. this.outArea.removeCup(destroyedCup);
  263. // 补充新杯子到等待区
  264. this.generateNewCupToWaitArea();
  265. }
  266. private async generateNewCupToWaitArea() {
  267. const configs = TakeGobletGlobalInstance.instance.getInitialCupsConfig();
  268. const randomConfig = configs[Math.floor(Math.random() * configs.length)];
  269. const prefab = await TakeGobletGlobalInstance.instance.loadAsyncCocktail(randomConfig.height);
  270. const newCup = instantiate(prefab);
  271. const cupComp = newCup.getComponent(CocktailCup)!;
  272. cupComp.cupColor = TakeGobletGlobalInstance.instance.getRandomColor();
  273. cupComp.reset();
  274. this.waitArea.addCup(newCup);
  275. }
  276. // 新增重置方法
  277. public resetLevel() {
  278. this.originCupPositions.clear();
  279. this.generateInitialCups();
  280. }
  281. }