From d5e313e43db41990f716efa87176936097cf40e8 Mon Sep 17 00:00:00 2001 From: lq <3298408835@qq.com> Date: Tue, 17 Dec 2024 17:04:12 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Script/UI/PanelUI/EvacuationPanel.cs | 47 +++++++++---------- xiaofang/Assets/Script/UI/PersonnelItem.cs | 1 + 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/xiaofang/Assets/Script/UI/PanelUI/EvacuationPanel.cs b/xiaofang/Assets/Script/UI/PanelUI/EvacuationPanel.cs index fdf93478..82a5b759 100644 --- a/xiaofang/Assets/Script/UI/PanelUI/EvacuationPanel.cs +++ b/xiaofang/Assets/Script/UI/PanelUI/EvacuationPanel.cs @@ -49,16 +49,13 @@ public class EvacuationPanel : MonoBehaviour [Header("Npc类型")] public string npcType; public string roleid; + public int isHere; [Header("可分配列表")] List nonZeroAreas = new List(); - Dictionary personnelItemsDict = new Dictionary();//储存分配结果 - //private HashSet excludedAreas = new HashSet(); // 被清空的区域 ID public SelectScenePanel selectScene; private HashSet clearedAreas = new HashSet(); - // 保存分配结果的字典 - public Dictionary allocatedPeople = new Dictionary(); public List clearRegion=new List();//被清除的区域 // Start is called before the first frame update void Start() @@ -76,13 +73,14 @@ public class EvacuationPanel : MonoBehaviour void Update() { CheckInput(StuCountInputField.text); - IsRedistribution(); + //IsRedistribution(); IsOpen(selectScene.difficultyId); } //上传数据 public void totalSubmit() { NpcList nPC = new NpcList(); + nPC.npcId = npcType; nPC.areaId = roleid; createTemplateInfo.Instance.auth_CreateTemplate.npcList = new List(); @@ -209,7 +207,6 @@ public class EvacuationPanel : MonoBehaviour return finalDistribution; } - //设置左侧场景显示2.0 public void SetScene() { @@ -243,10 +240,12 @@ public class EvacuationPanel : MonoBehaviour // 如果被点击的区域已经有人员 if (personnelItem.NpcNum > 0) { + redistributeBtn.interactable=true; redistributeBtn.onClick.AddListener(() => { // 清除选定区域的人员数据 RedistributeLogic(personnelItem.sceneId, personnelItem.NpcNum); + redistributeBtn.interactable = false ; }); } // 清空现有场景下的所有人员UI @@ -354,7 +353,7 @@ public class EvacuationPanel : MonoBehaviour return; } - // 3. 准备有效分配区域列表(剩余的 personnelItems) + // 3. 筛选有效的分配区域(剩余的 personnelItems) List validAreas = personnelItems.Where(item => item.NpcNum >= 0).ToList(); if (validAreas.Count == 0) @@ -363,30 +362,30 @@ public class EvacuationPanel : MonoBehaviour return; } - // 4. 平均分配人数到有效区域 - int remainingNpc = totalRedistributePeople; + // 4. 随机分配人数到有效区域 + System.Random random = new System.Random(); // 随机数生成器 Dictionary redistributionResult = new Dictionary(); + // 初始化分配结果 foreach (var item in validAreas) { - redistributionResult[item.sceneId] = 0; // 初始化分配数量 + redistributionResult[item.sceneId] = 0; } + // 随机分配 NPC + int remainingNpc = totalRedistributePeople; + while (remainingNpc > 0) { - foreach (var item in validAreas) - { - if (remainingNpc > 0) - { - redistributionResult[item.sceneId]++; - remainingNpc--; - } - else - { - break; - } - } + // 随机选择一个区域 + int randomIndex = random.Next(0, validAreas.Count); + PersonnelItem targetItem = validAreas[randomIndex]; + + // 更新分配结果 + redistributionResult[targetItem.sceneId]++; + remainingNpc--; } + // 5. 更新有效区域的 UI 显示和数据 foreach (var kvp in redistributionResult) { @@ -396,12 +395,12 @@ public class EvacuationPanel : MonoBehaviour targetItem.NpcNum += kvp.Value; // 更新 NPC 数量 targetItem.SetInfo(targetItem.NpcNum.ToString()); // 更新 UI 显示 targetItem.personnelImage.gameObject.SetActive(true); // 确保图标可见 - - Debug.Log($"Redistributed {kvp.Value} NPCs to Scene ID: {kvp.Key}"); + Debug.Log($"Randomly redistributed {kvp.Value} NPCs to Scene ID: {kvp.Key}"); } } } + //设置文字颜色 public void OnSceneItemClicked(GameObject clickedItem, Color color, GameObject select) { diff --git a/xiaofang/Assets/Script/UI/PersonnelItem.cs b/xiaofang/Assets/Script/UI/PersonnelItem.cs index c6607950..71aced06 100644 --- a/xiaofang/Assets/Script/UI/PersonnelItem.cs +++ b/xiaofang/Assets/Script/UI/PersonnelItem.cs @@ -11,6 +11,7 @@ public class PersonnelItem : MonoBehaviour public int sceneId; public int Num = 0; public int NpcNum = 0; + public int isHere; // Start is called before the first frame update void Start() {