woso_javan преди 3 месеца
родител
ревизия
f1295afa09

+ 1 - 5
assets/module_take_goblet/RoosterTakeGoblet.ts

@@ -43,11 +43,7 @@ export class RoosterTakeGoblet extends Component {
     private async onClickRefresh(): Promise<void> {
         TakeGobletAudioMgr.playOneShot(TakeGobletAudioMgr.getMusicIdName(3), 1.0);
         Tween.stopAll();
-        LevelManager.instance.clearLevelData();
-        const { level } = LevelManager.instance.levelModel;
-        LevelManager.instance.loadLevel(level);
-
-        await GameUtil.delay(0.2);
+        EventDispatcher.instance.emit(GameEvent.EVENT_BATTLE_FAIL_LEVEL_RESET);
     }
 
     private onClickSet(): void {

+ 15 - 4
assets/module_take_goblet/Script/LevelAction.ts

@@ -11,6 +11,7 @@ import { GameEvent } from './Enum/GameEvent';
 import { TempCup } from './Component/TempCup';
 import { TempCups } from './Component/TempCups';
 import { tgxUITips } from '../../core_tgx/tgx';
+import { LevelManager } from './Manager/LevelMgr';
 const { ccclass, property } = _decorator;
 
 @ccclass('LevelAction')
@@ -37,7 +38,6 @@ export class LevelAction extends Component {
     }
 
     start() {
-        this.originCupPositions.clear();
         this.generateInitialCups();
         this.registerListener();
     }
@@ -77,13 +77,24 @@ export class LevelAction extends Component {
                 const cupNode = instantiate(prefab);
                 const cup = cupNode.getComponent(CocktailCup)!;
 
-                // 设置颜色并重置水位
-                cup.cupColor = instance.getRandomColor();
                 cup.reset();
                 allCups.push(cupNode);
             }
         }
 
+        const levelColors = LevelManager.instance.levelModel.levelColors;
+        const rule = instance.cocktailCupRule();
+        const [x, y] = rule;
+        const colorPool = levelColors.slice(0, y);
+
+        // 为所有杯子分配颜色,并控制颜色种类不超过y种
+        allCups.forEach(cup => {
+            const cupComp = cup.getComponent(CocktailCup)!;
+            // 从颜色池随机选择一个颜色
+            const randomColor = colorPool[Math.floor(Math.random() * colorPool.length)];
+            cupComp.cupColor = randomColor;
+        });
+
         console.log('allCups: ', allCups.length);
         // 分配初始位置
         allCups.slice(0, 2).forEach(cup => this.outArea.addCup(cup));
@@ -142,7 +153,7 @@ export class LevelAction extends Component {
             // 在生成初始原浆杯时记录位置
             const id = originCupNode.uuid; // 使用节点唯一ID
             this.originCupPositions.set(id, originCupNode.position.clone());
-            console.log('在生成初始原浆杯时记录id : ', id, originCupNode.position);
+            // console.log('在生成初始原浆杯时记录id : ', id, originCupNode.position);
         });
     }
 

+ 5 - 1
assets/module_take_goblet/Script/Manager/LevelMgr.ts

@@ -31,7 +31,7 @@ export class LevelManager {
     }
 
     private registerEvent(): void {
-        // EventDispatcher.instance.on(GameEvent.EVENT_BATTLE_SUCCESS_LEVEL_UP, this.levelUpHandler, this);
+        EventDispatcher.instance.on(GameEvent.EVENT_BATTLE_SUCCESS_LEVEL_UP, this.levelUpHandler, this);
         EventDispatcher.instance.on(GameEvent.EVENT_BATTLE_FAIL_LEVEL_RESET, this.restartLevelHandler, this);
     }
 
@@ -63,11 +63,15 @@ export class LevelManager {
     async levelUpHandler() {
         this.clearLevelData();
         this.upgradeLevel();
+        this.levelModel.initLevelColors();
         await this.gameStart();
     }
 
     async restartLevelHandler() {
         this.clearLevelData();
+        this.levelModel.initLevelColors();
+
+        console.log('重新开始关卡 levelColors: ', this.levelModel.levelColors);
         await this.gameStart();
     }
 

+ 16 - 0
assets/module_take_goblet/Script/Model/LevelModel.ts

@@ -2,6 +2,7 @@ import { JsonUtil } from "db://assets/core_tgx/base/utils/JsonUtil";
 import { Tablelevels_config } from "../../../module_basic/table/Tablelevels_config";
 import { GlobalConfig } from "../../../start/Config/GlobalConfig";
 import { sys } from "cc";
+import { WaterColors } from "../TakeGobletGlobalInstance";
 
 
 export enum TYPE_GAME_STATE {
@@ -17,6 +18,7 @@ export enum TYPE_GAME_STATE {
 export class LevelModel {
     public levelConfig: Tablelevels_config;
 
+    public levelColors: WaterColors[] = [];
     /** 当前关卡等级*/
     public level: number = 1;
     /** 保存可随机的关卡*/
@@ -51,6 +53,19 @@ export class LevelModel {
             }
         };
         this.levelConfig.init(this.level);
+        this.initLevelColors();
+    }
+
+    public initLevelColors() {
+        const count = this.levelConfig.color; //获取关卡颜色数
+        this.levelColors = [];
+        const allColors = Object.values(WaterColors).filter(v => !isNaN(Number(v))) as WaterColors[];
+        // Fisher-Yates 洗牌算法
+        for (let i = allColors.length - 1; i > 0; i--) {
+            const j = Math.floor(Math.random() * (i + 1));
+            [allColors[i], allColors[j]] = [allColors[j], allColors[i]];
+        }
+        this.levelColors.push(...allColors.slice(0, count))
     }
 
     /** 可随机的关卡合集*/
@@ -67,6 +82,7 @@ export class LevelModel {
 
     /** 清除关卡数据*/
     clearLevel() {
+        this.levelColors = [];
         this.isWin = false;
         this.isEnd = false;
     }

+ 16 - 14
assets/module_take_goblet/Script/TakeGobletGlobalInstance.ts

@@ -1,6 +1,7 @@
 import { _decorator, assetManager, Component, Node, Prefab } from 'cc';
 import { resLoader } from '../../core_tgx/base/ResLoader';
 import { ResourcePool } from './ResourcePool';
+import { LevelManager } from './Manager/LevelMgr';
 
 const { ccclass, property } = _decorator;
 
@@ -38,12 +39,6 @@ export class TakeGobletGlobalInstance {
         })
     }
 
-    /** 生成原浆杯高度*/
-    public generateOriginCupHeight(): CupHeight {
-        //DOTO 配置中获取概率
-        return CupHeight.Three;
-    }
-
     /** 异步加载调酒杯预设*/
     async loadAsyncOriginCup(height: CupHeight): Promise<Prefab> {
         return new Promise((resolve, reject) => {
@@ -66,21 +61,28 @@ export class TakeGobletGlobalInstance {
         })
     }
 
+    /** 生成原浆杯高度*/
+    public generateOriginCupHeight(): CupHeight {
+        //DOTO 配置中获取概率
+        return CupHeight.Three;
+    }
+
     public levels: Node = null;
 
-    // 初始化调酒杯配置
     getInitialCupsConfig(): { height: CupHeight; count: number }[] {
+        const { levelConfig } = LevelManager.instance.levelModel;
+        const wineglass = levelConfig.wineglass;
         return [
-            { height: CupHeight.Two, count: 3 },
-            { height: CupHeight.Three, count: 5 },
-            { height: CupHeight.Four, count: 7 }
+            { height: CupHeight.Two, count: wineglass[0] },
+            { height: CupHeight.Three, count: wineglass[1] },
+            { height: CupHeight.Four, count: wineglass[2] }
         ];
     }
 
-    // 生成随机颜色数组
-    public getRandomColor(): WaterColors {
-        const colors = Object.values(WaterColors).filter(v => !isNaN(Number(v))) as WaterColors[];
-        return colors[Math.floor(Math.random() * colors.length)];
+    //获取调酒杯规则
+    cocktailCupRule(): number[] {
+        const { wineglass_color } = LevelManager.instance.levelModel.levelConfig;
+        return wineglass_color;
     }
 }