328 lines
9.7 KiB
C#
328 lines
9.7 KiB
C#
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using System.Xml.Serialization;
|
||
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
using static System.Collections.Specialized.BitVector32;
|
||
using static UnityEditor.Progress;
|
||
|
||
public class EvacuationPanel : MonoBehaviour
|
||
{
|
||
public Dictionary<string, int> distributePeople = new Dictionary<string, int>();
|
||
public List<PersonnelItem> personnelItems;
|
||
|
||
public Transform personnelContent;
|
||
public Panel panel;
|
||
public GameObject personnelPrefabs;
|
||
public GameObject classPrefab;
|
||
public Transform content;
|
||
public Text topText;
|
||
|
||
public List<ClassItem> classItemList = new List<ClassItem>();
|
||
public List<ClassMate> classMateList = new List<ClassMate>();
|
||
|
||
[Header("学生数量")]
|
||
public InputField StuCountInputField;
|
||
public Button CountsubmitBtn;
|
||
public Sprite rsprite;
|
||
public Sprite fsprite;
|
||
public GameObject JuesechoicePop;
|
||
|
||
public JueseChoicePop jc = new JueseChoicePop();
|
||
public JSONReader js;
|
||
|
||
[Header("总按钮")]
|
||
public Button redistributeBtn;
|
||
public Button submitBtn;
|
||
|
||
[Header("Npc数量")]
|
||
public int npcNum;
|
||
|
||
[Header("Npc类型")]
|
||
public string npcType;
|
||
public string roleid;
|
||
|
||
// Start is called before the first frame update
|
||
void Start()
|
||
{
|
||
SetNpcType();
|
||
jc = JuesechoicePop.GetComponent<JueseChoicePop>();
|
||
|
||
redistributeBtn.onClick.AddListener(ClearData);
|
||
CountsubmitBtn.onClick.AddListener(Submit);
|
||
submitBtn.onClick.AddListener(totalSubmit);
|
||
|
||
StuCountInputField.onEndEdit.AddListener(CheckInput);
|
||
SetClass();
|
||
}
|
||
|
||
|
||
public void totalSubmit()
|
||
{
|
||
Debug.Log(createTemplateInfo.Instance.auth_CreateTemplate + "===============");
|
||
NpcList nPC = new NpcList();
|
||
nPC.userId = npcType;
|
||
nPC.roleId = roleid;
|
||
createTemplateInfo.Instance.auth_CreateTemplate.npcList = new List<NpcList>();
|
||
createTemplateInfo.Instance.auth_CreateTemplate.npcList.Add(nPC);
|
||
//foreach(NpcList item in createTemplateInfo.Instance.auth_CreateTemplate.npcList)
|
||
//{
|
||
// Debug.Log("npcType>>>>>>>>>>>>>>>>>" + item.userId);
|
||
// Debug.Log("roleid>>>>>>>>>>>>>>>>>" + item.roleId);
|
||
//}
|
||
}
|
||
|
||
public void SetNpcType()
|
||
{
|
||
foreach (var scene in panel.sceneDataDictionary)
|
||
{
|
||
Debug.Log("+++++++++++");
|
||
foreach (var npcData in js.locationDictionary)
|
||
{
|
||
// 解析角色限制字段
|
||
string roleLimit = npcData.Value.NpcRatio;
|
||
|
||
if(npcData.Value.NpcRatio=="-1")
|
||
{
|
||
continue;
|
||
}
|
||
else
|
||
{
|
||
// 先按“,”分隔
|
||
string[] roleLimitSections = roleLimit.Split(',');
|
||
if (scene.Key == npcData.Value.Note)
|
||
{
|
||
// 只有当 scene.Key 和 npcData.Value.Note 匹配时才执行
|
||
this.npcType = roleLimitSections[1];
|
||
roleid = roleLimitSections[0];
|
||
}
|
||
}
|
||
|
||
}
|
||
}
|
||
|
||
}
|
||
|
||
//private void Submit()
|
||
//{
|
||
// //int count = int.Parse(StuCountInputField.text);//总人数
|
||
// //StuCountInputField.text = "";
|
||
// //int initialPeople = panel.sceneDataDictionary.Keys.Count* panel.sceneDataDictionary.Values.Count;//已经分了的人
|
||
// //string[] sceneNames = { };
|
||
// //for(int i = 0; i< panel.sceneDataDictionary.Keys.Count;i++)//拿场景名称
|
||
// //{
|
||
// // foreach(var item in panel.sceneDataDictionary.Keys)
|
||
// // {
|
||
// // sceneNames[i] = item;
|
||
// // }
|
||
// //}
|
||
|
||
// //distributePeople = DistributePeople(sceneNames, initialPeople, count);
|
||
|
||
// //foreach(var item in distributePeople)
|
||
// //{
|
||
// // Debug.Log(item.Value);
|
||
// //}
|
||
|
||
// int count = int.Parse(StuCountInputField.text); // 总人数
|
||
// StuCountInputField.text = "";
|
||
|
||
// // 已经分配的人员数量
|
||
// int initialPeople = panel.sceneDataDictionary.Keys.Count * panel.sceneDataDictionary.Values.Count;
|
||
|
||
// // 动态创建sceneNames数组,大小为场景数量
|
||
// string[] sceneNames = new string[panel.sceneDataDictionary.Keys.Count];
|
||
|
||
// // 遍历场景数据字典并填充sceneNames数组
|
||
// int i = 0;
|
||
// foreach (var item in panel.sceneDataDictionary.Keys)
|
||
// {
|
||
// sceneNames[i] = item;
|
||
// i++;
|
||
// }
|
||
|
||
// // 调用分配人员函数
|
||
// distributePeople = DistributePeople(sceneNames, initialPeople, count);
|
||
|
||
// // 输出分配结果
|
||
// foreach (var item in distributePeople)
|
||
// {
|
||
// //Debug.Log(item.Value);
|
||
|
||
// foreach(var a in personnelItems)
|
||
// {
|
||
// a.SetInfo(item.Value.ToString());
|
||
// }
|
||
// }
|
||
|
||
//}
|
||
|
||
private void Submit()
|
||
{
|
||
// 获取输入的总人数
|
||
int count = int.Parse(StuCountInputField.text); // 总人数
|
||
StuCountInputField.text = "";
|
||
|
||
// 计算已分配的人员数量
|
||
int initialPeople = 0;
|
||
foreach (var item in panel.sceneDataDictionary.Values)
|
||
{
|
||
initialPeople += item.Count; // 假设每个场景的Value是一个List或类似的结构,其中包含该场景下的所有人员
|
||
}
|
||
|
||
// 动态创建sceneNames数组,大小为场景数量
|
||
string[] sceneNames = new string[panel.sceneDataDictionary.Keys.Count];
|
||
|
||
// 填充sceneNames数组
|
||
int i = 0;
|
||
foreach (var item in panel.sceneDataDictionary.Keys)
|
||
{
|
||
sceneNames[i] = item;
|
||
i++;
|
||
}
|
||
|
||
// 调用分配人员函数(假设该函数处理人员分配的逻辑)
|
||
distributePeople = DistributePeople(sceneNames, initialPeople, count);
|
||
|
||
// 输出分配结果
|
||
foreach (var item in distributePeople)
|
||
{
|
||
// 假设 personnelItems 是存储UI元素的列表
|
||
foreach (var a in personnelItems)
|
||
{
|
||
a.SetInfo(item.Value.ToString());
|
||
}
|
||
}
|
||
}
|
||
|
||
public static Dictionary<string, int> DistributePeople(string[] scenes, int initialPeople, int totalPeople)
|
||
{
|
||
// 1. 计算已分配的人员数
|
||
int totalAssigned = initialPeople;
|
||
|
||
//foreach (var scene in scenes)
|
||
//{
|
||
// if (initialPeople.ContainsKey(scene))
|
||
// {
|
||
// totalAssigned += initialPeople[scene];
|
||
// }
|
||
//}
|
||
|
||
// 2. 计算剩余人员数
|
||
int remainingPeople = totalPeople - totalAssigned;
|
||
|
||
// 3. 将剩余人员平均分配到所有场景中
|
||
Dictionary<string, int> finalDistribution = new Dictionary<string, int>(initialPeople);
|
||
int peoplePerScene = remainingPeople / scenes.Length;
|
||
int extraPeople = remainingPeople % scenes.Length; // 计算余数
|
||
|
||
// 4. 每个场景分配均等的人员
|
||
foreach (var scene in scenes)
|
||
{
|
||
if (finalDistribution.ContainsKey(scene))
|
||
{
|
||
finalDistribution[scene] += peoplePerScene; // 均分的人员
|
||
}
|
||
else
|
||
{
|
||
finalDistribution[scene] = peoplePerScene; // 新场景初始化
|
||
}
|
||
}
|
||
|
||
// 5. 随机分配余数人员
|
||
System.Random rand = new System.Random();
|
||
for (int i = 0; i < extraPeople; i++)
|
||
{
|
||
string randomScene = scenes[rand.Next(scenes.Length)];
|
||
finalDistribution[randomScene]++;
|
||
}
|
||
|
||
return finalDistribution;
|
||
}
|
||
|
||
|
||
private void ClearData()
|
||
{
|
||
jc.classItem.isSet = false;
|
||
foreach (var item in classItemList)
|
||
{
|
||
item.isSet = false;
|
||
item.setClassItem("");
|
||
}
|
||
|
||
jc.classMate.isBeSet = false;
|
||
|
||
foreach(var item in jc.classMateList)
|
||
{
|
||
item.isBeSet = false;
|
||
item.setName();
|
||
}
|
||
|
||
|
||
}
|
||
|
||
// Update is called once per frame
|
||
void Update()
|
||
{
|
||
CheckInput(StuCountInputField.text);
|
||
}
|
||
public void SetPersonnel()
|
||
{
|
||
foreach (Transform child in personnelContent)
|
||
{
|
||
Destroy(child.gameObject);
|
||
}
|
||
foreach (var sceneEntry in panel.sceneDataDictionary)
|
||
{
|
||
GameObject item = GameObject.Instantiate<GameObject>(personnelPrefabs, personnelContent);
|
||
PersonnelItem personnelItem=item.GetComponent<PersonnelItem>();
|
||
personnelItems.Add(personnelItem);
|
||
personnelItem.sceneText.text = sceneEntry.Key;
|
||
personnelItem.personnelNum.text = sceneEntry.Value.Count.ToString();
|
||
}
|
||
}
|
||
//判断输入框的数字是否大于1
|
||
void CheckInput(string input)
|
||
{
|
||
// 尝试将输入转换为数字
|
||
if (float.TryParse(input, out float result))
|
||
{
|
||
// 判断数字是否大于1
|
||
if (result > 1)
|
||
{
|
||
CountsubmitBtn.GetComponent<Image>().sprite = rsprite;
|
||
CountsubmitBtn.onClick.AddListener(Countsubmit);
|
||
|
||
}
|
||
}
|
||
}
|
||
public void Countsubmit()
|
||
{
|
||
//按钮置灰
|
||
CountsubmitBtn.GetComponent<Image>().sprite = fsprite;
|
||
StuCountInputField.text = string.Empty;
|
||
//按配置对输入的Npc数量进行 分配
|
||
|
||
//取消监听
|
||
CountsubmitBtn.onClick.RemoveAllListeners();
|
||
}
|
||
//场景ID进行配置
|
||
public void SettopText()
|
||
{
|
||
topText.text = "以下班级需要设定班主任老师:";
|
||
topText.text = "以下楼层需要设定搜救老师:";
|
||
}
|
||
//实例化ClassItem
|
||
public void SetClass()
|
||
{
|
||
for (int i = 0; i < 10; i++)
|
||
{
|
||
GameObject item = GameObject.Instantiate<GameObject>(classPrefab, content);
|
||
ClassItem classItem = item.GetComponent<ClassItem>();
|
||
classItem.JuesechoicePop = JuesechoicePop;
|
||
classItemList.Add(classItem);
|
||
}
|
||
}
|
||
}
|