Ver Fonte

根据配置 初始化

woso_javan há 2 meses atrás
pai
commit
f9a3513da2

+ 62 - 1
assets/module_basic/config/ai_config.json

@@ -1 +1,62 @@
-{"1":{"text":"乱跑","range":100,"move_judge":90,"move_time":[3,6],"escape_time":[3,6],"pursuit_1":30,"pursuit_2":30,"pursuit_time":[3,6],"weight":20},"2":{"text":"苟","range":100,"move_judge":50,"move_time":[3,6],"escape_time":[3,6],"pursuit_1":10,"pursuit_2":10,"pursuit_time":[3,6],"weight":10},"3":{"text":"疯狗","range":100,"move_judge":10,"move_time":[3,6],"escape_time":[3,6],"pursuit_1":90,"pursuit_2":50,"pursuit_time":[3,6],"weight":5}}
+{
+    "1": {
+        "text": "乱跑",
+        "range": 100,
+        "move_judge": 90,
+        "move_time": [
+            3,
+            6
+        ],
+        "escape_time": [
+            3,
+            6
+        ],
+        "pursuit_1": 30,
+        "pursuit_2": 30,
+        "pursuit_time": [
+            3,
+            6
+        ],
+        "weight": 20
+    },
+    "2": {
+        "text": "苟",
+        "range": 100,
+        "move_judge": 50,
+        "move_time": [
+            3,
+            6
+        ],
+        "escape_time": [
+            3,
+            6
+        ],
+        "pursuit_1": 10,
+        "pursuit_2": 10,
+        "pursuit_time": [
+            3,
+            6
+        ],
+        "weight": 10
+    },
+    "3": {
+        "text": "疯狗",
+        "range": 100,
+        "move_judge": 10,
+        "move_time": [
+            3,
+            6
+        ],
+        "escape_time": [
+            3,
+            6
+        ],
+        "pursuit_1": 90,
+        "pursuit_2": 50,
+        "pursuit_time": [
+            3,
+            6
+        ],
+        "weight": 5
+    }
+}

+ 1 - 0
assets/module_storm_sunder/RoosterStormSunder.ts

@@ -28,6 +28,7 @@ export class RoosterStormSunder extends Component {
 
     async initialize() {
         PlayerMgr.inst.addAIPlayers();
+        PlayerMgr.inst.weightGeneateAIConfig();
         StormSunderGlobalInstance.instance.initUI();
     }
 

+ 19 - 12
assets/module_storm_sunder/Script/Component/TornadoAIComponent.ts

@@ -21,13 +21,14 @@ export class TornadoAIComponent extends TornadoComponent {
     moveDuration: number = 3; //移动行为持续时间(秒)
     escapeDuration: number = 5; //逃离行为持续时间(秒)
     chaseDuration: number = 20; //追击行为持续时间(秒)
-    chaseAIProbability: number = 1; //是否追击 AI 的概率 1是100%
-    chasePlayerProbability: number = 1; //是否追击玩家的概率
+    chaseAIProbability: number = 0; //是否追击 AI 的概率
+    chasePlayerProbability: number = 0; //是否追击玩家的概率
 
     isChasing: boolean = false; // 是否追击中
     isEscaping: boolean = false; // 是否逃跑中
     targetNode: Node | null = null; // 目标
 
+    moveProbability: number = 0.5; //移动概率
     behaviorType: BehaviorType = BehaviorType.Move; // 行为类型
 
     protected async start() {
@@ -42,16 +43,25 @@ export class TornadoAIComponent extends TornadoComponent {
     }
 
     private initAIPlayer() {
-        this.playerInfo.nickName = 'asfasf'
+        const aiConfig = PlayerMgr.inst.getRandomAIConfig();
+        console.log(aiConfig);
+        //text:名称 range:检测半径 move_juge:移动概率 move_time:移动时间 escape_time:逃跑时间 pursuit_1:追击玩家概率 pursuit_2:追击AI概率 pursuit_time:追击时间
+        const { text, range, move_juge, move_time, escape_time, pursuit_1, pursuit_2, pursuit_time } = aiConfig.data;
+        this.playerInfo.nickName = text;
         this.playerInfo.level = 2;
         this.currentLv = this.playerInfo.level;
 
+        this.moveDuration = Math.floor(move_time[0] + Math.random() * (move_time[1] - move_time[0] + 1));
+        this.escapeDuration = Math.floor(escape_time[0] + Math.random() * (escape_time[1] - escape_time[0] + 1));
+        this.chaseDuration = Math.floor(pursuit_time[0] + Math.random() * (pursuit_time[1] - pursuit_time[0] + 1));
+        this.chaseAIProbability = pursuit_1;
+        this.chasePlayerProbability = pursuit_2;
+        this.moveProbability = move_juge;
+
         this.nextExp = this.attributeBonusMgr.getExpNeed(this.currentLv + 1);
         this.attack = this.attributeBonusMgr.getStormSunderAttack(this.currentLv, true);
         this.speed = this.attributeBonusMgr.getStormSunderSpeed(this.currentLv, true);
 
-        this.attack = 10000; //测试
-
         console.log(`AI 攻击力:${this.attack} 速度:${this.speed} 下一级经验:${this.nextExp}`)
     }
 
@@ -59,10 +69,8 @@ export class TornadoAIComponent extends TornadoComponent {
     private decideAction() {
         if (this.playerStatus == PlayerStatus.DIE) return; // AI 死亡时不执行行为
 
-        // DOTO 目前固定 100% 选择移动行为(未来可修改概率)
-        const actionType = this.behaviorType;
-
-        if (actionType === BehaviorType.Move) {
+        const move = Math.random() * 100 < this.moveProbability;
+        if (move) {
             this.randomMove();
         } else {
             const closestItem = PropMgr.inst.getNearestProp(this.node);
@@ -128,7 +136,6 @@ export class TornadoAIComponent extends TornadoComponent {
 
     protected addExpByKill() {
         super.addExpByKill(true);
-
         console.log(`AI 当前经验:${this.currentExp}`);
     }
 
@@ -149,13 +156,13 @@ export class TornadoAIComponent extends TornadoComponent {
                 this.escapeFrom(targetTornado.node);
             } else if (targetLv < this.currentLv) {
                 // 目标等级比自己低 → 先判断是否追击
-                if (Math.random() < this.chaseAIProbability) {
+                if (Math.random() * 100 < this.chaseAIProbability) {
                     if (isTargetAI) {
                         // 目标是 AI,直接追击
                         this.chaseTarget(targetTornado.node);
                     } else {
                         // 目标是玩家,进一步判断是否追击
-                        if (Math.random() < this.chasePlayerProbability) {
+                        if (Math.random() * 100 < this.chasePlayerProbability) {
                             this.chaseTarget(targetTornado.node);
                         } else {
                             // 不追击,则保持当前行为到持续时间结束

+ 46 - 8
assets/module_storm_sunder/Script/Manager/PlayerMgr.ts

@@ -29,7 +29,8 @@ export class PlayerMgr {
     public createAIPlayerCount: number = 3;//创建AI玩家数量
     public aiConfigCount: number = 3;//AI配置数量
 
-    public pickAiMap: Map<number, Tableai_config> = new Map();//权重随机的AI
+    private aiIndex: number = 0;//AI索引
+    private pickAiMap: Map<number, Tableai_config> = new Map();//权重随机的AI
 
     //创建AI玩家
     public async genareatorAIPlayer(): Promise<void> {
@@ -81,16 +82,53 @@ export class PlayerMgr {
             }
         }
 
-        console.log("AI玩家配置数据:", this.aiPlayersConfig);
+        // console.log("AI玩家配置数据:", this.aiPlayersConfig);
     }
 
-    /** 根据权重计算 排序储存*/
-    weightGeneateAIConfig(): Tableai_config[] {
-        const result: Tableai_config[] = [];
-        for (let i = 0; i < this.aiConfigCount; i++) {
-            console.log(this.aiPlayersConfig[i].data.weight);
+    /**
+     * 根据权重生成AI配置
+     */
+    weightGeneateAIConfig(): void {
+        // 获取所有AI配置
+        const aiConfigs = Array.from(this.aiPlayersConfig.values());
+
+        // 按权重从大到小排序
+        aiConfigs.sort((a, b) => b.weight - a.weight);
+
+        let accumulatedWeight = 0;
+        this.pickAiMap.clear();
+
+        // 计算权重并存储到pickAiMap
+        for (const config of aiConfigs) {
+            const weight = config.weight / 100; // 转换为百分比 (10 -> 0.1, 20 -> 0.2)
+            accumulatedWeight += weight;
+
+            // 存储累积权重和对应配置
+            this.pickAiMap.set(accumulatedWeight, config);
         }
-        return result;
+
+        console.log(this.pickAiMap);
+    }
+
+    /**
+     * 根据索引获取AI配置
+     */
+    getRandomAIConfig(): any {
+        // 检查是否超出创建数量
+        if (this.aiIndex >= this.createAIPlayerCount) {
+            console.log('AI创建数量大于配置表数量');
+            return null;
+        }
+
+        // 从pickAiMap中获取对应索引的配置
+        const config = Array.from(this.pickAiMap.values())[this.aiIndex];
+        this.aiIndex++; // 索引递增
+        return config;
+    }
+
+    /** 重置AI索引*/
+    public resetAIIndex(): void {
+        this.aiIndex = 0;
     }
 
 }