This commit is contained in:
lq 2024-12-17 17:04:12 +08:00
parent e50bf861f4
commit d5e313e43d
2 changed files with 24 additions and 24 deletions

View File

@ -49,16 +49,13 @@ public class EvacuationPanel : MonoBehaviour
[Header("Npc类型")] [Header("Npc类型")]
public string npcType; public string npcType;
public string roleid; public string roleid;
public int isHere;
[Header("可分配列表")] [Header("可分配列表")]
List<int> nonZeroAreas = new List<int>(); List<int> nonZeroAreas = new List<int>();
Dictionary<int, PersonnelItem> personnelItemsDict = new Dictionary<int, PersonnelItem>();//储存分配结果
//private HashSet<int> excludedAreas = new HashSet<int>(); // 被清空的区域 ID
public SelectScenePanel selectScene; public SelectScenePanel selectScene;
private HashSet<int> clearedAreas = new HashSet<int>(); private HashSet<int> clearedAreas = new HashSet<int>();
// 保存分配结果的字典
public Dictionary<int, int> allocatedPeople = new Dictionary<int, int>();
public List<int> clearRegion=new List<int>();//被清除的区域 public List<int> clearRegion=new List<int>();//被清除的区域
// Start is called before the first frame update // Start is called before the first frame update
void Start() void Start()
@ -76,13 +73,14 @@ public class EvacuationPanel : MonoBehaviour
void Update() void Update()
{ {
CheckInput(StuCountInputField.text); CheckInput(StuCountInputField.text);
IsRedistribution(); //IsRedistribution();
IsOpen(selectScene.difficultyId); IsOpen(selectScene.difficultyId);
} }
//上传数据 //上传数据
public void totalSubmit() public void totalSubmit()
{ {
NpcList nPC = new NpcList(); NpcList nPC = new NpcList();
nPC.npcId = npcType; nPC.npcId = npcType;
nPC.areaId = roleid; nPC.areaId = roleid;
createTemplateInfo.Instance.auth_CreateTemplate.npcList = new List<NpcList>(); createTemplateInfo.Instance.auth_CreateTemplate.npcList = new List<NpcList>();
@ -209,7 +207,6 @@ public class EvacuationPanel : MonoBehaviour
return finalDistribution; return finalDistribution;
} }
//设置左侧场景显示2.0 //设置左侧场景显示2.0
public void SetScene() public void SetScene()
{ {
@ -243,10 +240,12 @@ public class EvacuationPanel : MonoBehaviour
// 如果被点击的区域已经有人员 // 如果被点击的区域已经有人员
if (personnelItem.NpcNum > 0) if (personnelItem.NpcNum > 0)
{ {
redistributeBtn.interactable=true;
redistributeBtn.onClick.AddListener(() => redistributeBtn.onClick.AddListener(() =>
{ {
// 清除选定区域的人员数据 // 清除选定区域的人员数据
RedistributeLogic(personnelItem.sceneId, personnelItem.NpcNum); RedistributeLogic(personnelItem.sceneId, personnelItem.NpcNum);
redistributeBtn.interactable = false ;
}); });
} }
// 清空现有场景下的所有人员UI // 清空现有场景下的所有人员UI
@ -354,7 +353,7 @@ public class EvacuationPanel : MonoBehaviour
return; return;
} }
// 3. 准备有效分配区域列表(剩余的 personnelItems // 3. 筛选有效的分配区域(剩余的 personnelItems
List<PersonnelItem> validAreas = personnelItems.Where(item => item.NpcNum >= 0).ToList(); List<PersonnelItem> validAreas = personnelItems.Where(item => item.NpcNum >= 0).ToList();
if (validAreas.Count == 0) if (validAreas.Count == 0)
@ -363,30 +362,30 @@ public class EvacuationPanel : MonoBehaviour
return; return;
} }
// 4. 平均分配人数到有效区域 // 4. 随机分配人数到有效区域
int remainingNpc = totalRedistributePeople; System.Random random = new System.Random(); // 随机数生成器
Dictionary<int, int> redistributionResult = new Dictionary<int, int>(); Dictionary<int, int> redistributionResult = new Dictionary<int, int>();
// 初始化分配结果
foreach (var item in validAreas) foreach (var item in validAreas)
{ {
redistributionResult[item.sceneId] = 0; // 初始化分配数量 redistributionResult[item.sceneId] = 0;
} }
// 随机分配 NPC
int remainingNpc = totalRedistributePeople;
while (remainingNpc > 0) while (remainingNpc > 0)
{ {
foreach (var item in validAreas) // 随机选择一个区域
{ int randomIndex = random.Next(0, validAreas.Count);
if (remainingNpc > 0) PersonnelItem targetItem = validAreas[randomIndex];
{
redistributionResult[item.sceneId]++; // 更新分配结果
remainingNpc--; redistributionResult[targetItem.sceneId]++;
} remainingNpc--;
else
{
break;
}
}
} }
// 5. 更新有效区域的 UI 显示和数据 // 5. 更新有效区域的 UI 显示和数据
foreach (var kvp in redistributionResult) foreach (var kvp in redistributionResult)
{ {
@ -396,12 +395,12 @@ public class EvacuationPanel : MonoBehaviour
targetItem.NpcNum += kvp.Value; // 更新 NPC 数量 targetItem.NpcNum += kvp.Value; // 更新 NPC 数量
targetItem.SetInfo(targetItem.NpcNum.ToString()); // 更新 UI 显示 targetItem.SetInfo(targetItem.NpcNum.ToString()); // 更新 UI 显示
targetItem.personnelImage.gameObject.SetActive(true); // 确保图标可见 targetItem.personnelImage.gameObject.SetActive(true); // 确保图标可见
Debug.Log($"Randomly redistributed {kvp.Value} NPCs to Scene ID: {kvp.Key}");
Debug.Log($"Redistributed {kvp.Value} NPCs to Scene ID: {kvp.Key}");
} }
} }
} }
//设置文字颜色 //设置文字颜色
public void OnSceneItemClicked(GameObject clickedItem, Color color, GameObject select) public void OnSceneItemClicked(GameObject clickedItem, Color color, GameObject select)
{ {

View File

@ -11,6 +11,7 @@ public class PersonnelItem : MonoBehaviour
public int sceneId; public int sceneId;
public int Num = 0; public int Num = 0;
public int NpcNum = 0; public int NpcNum = 0;
public int isHere;
// Start is called before the first frame update // Start is called before the first frame update
void Start() void Start()
{ {