LevelAction.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. this.addWaitCupToOutArea();
  181. }
  182. }
  183. // 添加等待区杯子到调酒区
  184. private addWaitCupToOutArea() {
  185. // 获取等待区最右侧杯子
  186. const waitCups = this.waitArea.getCups();
  187. if (waitCups.length === 0) return;
  188. const lastCup = waitCups[waitCups.length - 1];
  189. // 改变父节点到调酒区
  190. lastCup.setParent(this.outArea.node);
  191. // 从等待区移除
  192. this.waitArea.getCups().pop();
  193. // 调酒区所有杯子向右平移80
  194. this.outArea.getCups().forEach(cup => {
  195. tween(cup)
  196. .by(0.3, { position: new Vec3(80, 0, 0) }, { easing: 'sineOut' })
  197. .start();
  198. });
  199. // 等待区剩余杯子向右平移80
  200. this.waitArea.getCups().forEach(cup => {
  201. tween(cup)
  202. .by(0.3, { position: new Vec3(80, 0, 0) }, { easing: 'sineOut' })
  203. .start();
  204. });
  205. }
  206. private async pourAnimation(
  207. origin: Node,
  208. target: Node,
  209. color: WaterColors,
  210. isTempCup: boolean = false
  211. ) {
  212. // 使用正确的坐标转换
  213. const originParent = origin.parent!;
  214. const targetWorldPos = target.worldPosition;
  215. const localPos = originParent.getComponent(UITransform)!.convertToNodeSpaceAR(targetWorldPos);
  216. // 调整Y轴偏移量
  217. if (isTempCup) {
  218. localPos.y += 80; // 暂存杯偏移
  219. } else {
  220. localPos.y += 150; // 调酒杯偏移
  221. }
  222. // 只做单程移动,保留在目标位置
  223. await new Promise<void>(resolve => {
  224. tween(origin)
  225. .to(0.5, { position: localPos }) // 延长动画时间到0.5秒
  226. .call(() => resolve())
  227. .start();
  228. });
  229. }
  230. // 新增辅助方法
  231. private hideCurrentWaterLayer(originCup: OriginCup) {
  232. const activeWaters = originCup.waters.children.filter(n => n.active);
  233. if (activeWaters.length > 0) {
  234. const topIndex = activeWaters.length - 1;
  235. activeWaters[topIndex].active = false;
  236. }
  237. }
  238. private async spawnNewOriginCup(destroyedCup: Node) {
  239. // 获取被销毁杯子的初始位置
  240. const id = destroyedCup.uuid;
  241. const targetPos = this.originCupPositions.get(id) || Vec3.ZERO;
  242. // 创建新原浆杯
  243. const height = TakeGobletGlobalInstance.instance.generateOriginCupHeight();
  244. const prefab = await TakeGobletGlobalInstance.instance.loadAsyncOriginCup(height);
  245. const newCup = instantiate(prefab);
  246. // 设置初始位置(屏幕左侧)
  247. const uiTransform = this.node.getComponent(UITransform)!;
  248. newCup.setPosition(-uiTransform.width / 2, 0, 0);
  249. this.goblets.addChild(newCup);
  250. // 记录新杯子的初始位置
  251. this.originCupPositions.set(newCup.uuid, targetPos);
  252. // 移动动画到原位置
  253. tween(newCup)
  254. .to(0.5, { position: targetPos })
  255. .start();
  256. // DOTO 获取颜色配置
  257. const colors = this.getAvailableColors(5); // 获取前5个颜色
  258. this.setupOriginCupColors(newCup, colors);
  259. }
  260. // 获取可用颜色
  261. private getAvailableColors(count: number): WaterColors[] {
  262. const outCups = this.outArea.getCups();
  263. const waitCups = this.waitArea.getCups();
  264. const allCups = [...outCups, ...waitCups].slice(0, count);
  265. return allCups.map(cup => {
  266. const comp = cup.getComponent(CocktailCup);
  267. return comp ? comp.cupColor : WaterColors.Blue;
  268. });
  269. }
  270. // 设置原浆杯颜色
  271. private setupOriginCupColors(cupNode: Node, colors: WaterColors[]) {
  272. const originCup = cupNode.getComponent(OriginCup)!;
  273. const waters = originCup.waters.children;
  274. // 暂时写死5层水
  275. const waterCount = 5;
  276. for (let i = 0; i < waterCount; i++) {
  277. const waterNode = waters[i];
  278. if (!waterNode) continue;
  279. const water = waterNode.getComponent(Water)!;
  280. water.color = colors[Math.floor(Math.random() * colors.length)];
  281. waterNode.active = true;
  282. }
  283. }
  284. private handleCupDestroyed(destroyedCup: Node) {
  285. // 从outArea移除被销毁的杯子
  286. this.outArea.removeCup(destroyedCup);
  287. // 补充新杯子到等待区
  288. this.generateNewCupToWaitArea();
  289. }
  290. private async generateNewCupToWaitArea() {
  291. const configs = TakeGobletGlobalInstance.instance.getInitialCupsConfig();
  292. const randomConfig = configs[Math.floor(Math.random() * configs.length)];
  293. const prefab = await TakeGobletGlobalInstance.instance.loadAsyncCocktail(randomConfig.height);
  294. const newCup = instantiate(prefab);
  295. const cupComp = newCup.getComponent(CocktailCup)!;
  296. cupComp.cupColor = TakeGobletGlobalInstance.instance.getRandomColor();
  297. cupComp.reset();
  298. this.waitArea.addCup(newCup);
  299. }
  300. // 新增重置方法
  301. public resetLevel() {
  302. this.originCupPositions.clear();
  303. this.generateInitialCups();
  304. }
  305. }