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类型")]
public string npcType;
public string roleid;
public int isHere;
[Header("可分配列表")]
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;
private HashSet<int> clearedAreas = new HashSet<int>();
// 保存分配结果的字典
public Dictionary<int, int> allocatedPeople = new Dictionary<int, int>();
public List<int> clearRegion=new List<int>();//被清除的区域
// 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<NpcList>();
@ -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<PersonnelItem> 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<int, int> redistributionResult = new Dictionary<int, int>();
// 初始化分配结果
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]++;
// 随机选择一个区域
int randomIndex = random.Next(0, validAreas.Count);
PersonnelItem targetItem = validAreas[randomIndex];
// 更新分配结果
redistributionResult[targetItem.sceneId]++;
remainingNpc--;
}
else
{
break;
}
}
}
// 5. 更新有效区域的 UI 显示和数据
foreach (var kvp in redistributionResult)
{
@ -396,11 +395,11 @@ public class EvacuationPanel : MonoBehaviour
targetItem.NpcNum += kvp.Value; // 更新 NPC 数量
targetItem.SetInfo(targetItem.NpcNum.ToString()); // 更新 UI 显示
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)

View File

@ -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()
{