_xiaofang/xiaofang/Assets/Script/UI/PanelUI/SelectScenePanel.cs

658 lines
23 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using TMPro;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;
using static Unity.VisualScripting.FlowStateWidget;
public class SelectScenePanel : MonoBehaviour
{
[Header("组件")]
public GameObject scenePrefab;//滑动视图预制体
public Transform sceneList;//预制体列表
public GameObject schoolPrefab;//学校按钮预制体
public Transform schoolList;//学校预制体列表
public GameObject eventPrefab;//事件按钮预制体
public Transform eventList;//学校预制体列表
public GameObject incidentPrefab;//事件按钮预制体
public Transform incidentList;//学校预制体列表
public ToggleGroup schoolGroup;//学校ToggleGroup
public ToggleGroup sceneGroup;//场景ToggleGroup
public ToggleGroup eventGroup;//事件ToggleGroup
public ToggleGroup incidentGroup;//事件场景ToggleGroup
public GameObject scoolSelectBtn;//学校选择按钮
public GameObject sceneLable;//场景选择
public GameObject schoolChoiceLable;//场景名选择界面
public GameObject eventChoiceLable;//事件选择界面
public GameObject incidentLable;//事件场景界面
public Button randomEventBtn;//随机事件按钮
[Header("请选择演练的场景")] public Text text20008;
public Text title;//界面标题
public List<SchoolInfo> schoolInfoList = new List<SchoolInfo>();//学校信息
public List<EventInfo> eventInfoList = new List<EventInfo>();//事件信息
public List<IncidentInfo> incidentInfos = new List<IncidentInfo>();//事件图标信息
public List<SceneItem> sceneItemList = new List<SceneItem>();//场景信息
public List<Toggle> difficultyList = new List<Toggle>();//游戏难度
public Button continueBtn;
public Button submitBtn;
public List<Toggle> scenetoggleList = new List<Toggle>();
public List<Toggle> schooltoggleList = new List<Toggle>();
public List<Toggle> eventToggleList = new List<Toggle>();
public List<Toggle> evnetsceneList = new List<Toggle>();
public ToggleGroup toggleGroup;
[Header("数据")]
public DatePanel datePanel;
public Panel panel;
public int schoolId { get; set; }
public string schoolName { get; set; }
public int scnenId { get; set; }
public string sceneName { get; set; }
public int eventId { get; set; }
public string eventName { get; set; }
public int idcidentId{ get; set; }
public string idcidentName{ get; set; }
public string difficulty { get; set; }
public string isBuy { get; set; }
public int difficultyId { get; set; }
public bool isSure { get; set; } = false;//判断是否点击了提交按钮
public Dictionary<int, List<string>> difficultyToRoles = new Dictionary<int, List<string>>();
public JSONReader jsonReader;
// Start is called before the first frame update
void Start()
{
sceneLable.gameObject.SetActive(true);
schoolChoiceLable.gameObject.SetActive(true);
eventChoiceLable.gameObject.SetActive(false);
incidentLable.gameObject.SetActive(false);
InstantiateScenePrefab();
//InstantiateSchoolPrefab();
SetEventToggleOpenorClose(incidentInfos);
continueBtn.onClick.AddListener(OnClickContinueBtn);
SetFirstChoise(scenetoggleList);
SetToggleOpenorClose(sceneItemList);//未购买无法选择
SetScloolToggleOpenorClose(schoolInfoList);
}
// Update is called once per frame
void Update()
{
IsClick();
IsSubmit();
}
//动态加载场景选择预制体
public void InstantiateScenePrefab()
{
foreach (var sceneData in jsonReader.sceneDictionary)
{
GameObject slot = GameObject.Instantiate<GameObject>(scenePrefab, sceneList);
SceneItem item = slot.GetComponent<SceneItem>();
switch (sceneData.Value.Type)
{
case 1:
item.sceneName.text = "学校";
break;
case 2: // 医院
item.sceneName.text = "医院";
break;
case 3: // 建筑工地
item.sceneName.text = "建筑工地";
break;
default:
// 如果 Type 的值不在 1, 2, 3 中
Debug.LogError("未知的场景类型");
break;
}
item.sceneId = sceneData.Value.ID;
item.sceneType = sceneData.Value.Type;
if (item.IsOpen)
{
item.maskImage.gameObject.SetActive(false);
}
else
{
item.maskImage.gameObject.SetActive(true);
}
Toggle toggle = slot.GetComponent<Toggle>();
if (toggle != null)
{
toggle.group = sceneGroup;
scenetoggleList.Add(toggle); // 使用 Add() 方法添加 Toggle 元素
sceneItemList.Add(item);
}
toggle.onValueChanged.AddListener((isSelected) =>
{
if (isSelected) // 只有当Toggle被选中时才调用SelectEventBtn
{
// 清空事件列表
schoolInfoList.Clear();
foreach (Transform child in schoolList)
{
Destroy(child.gameObject);
}
this.scnenId = item.sceneId; // 获取 Text 组件的文本
this.sceneName = item.sceneName.text;
InstantiateSchoolPrefab(scnenId);
}
});
}
}
//动态加载学校选择预制体(这里面的东西需要判断场景的Id来加载对应的场景)
public void InstantiateSchoolPrefab(int id)
{
foreach (var sceneDate in jsonReader.sceneDictionary)
{
if (sceneDate.Key == id)
{
GameObject slot = GameObject.Instantiate<GameObject>(schoolPrefab, schoolList);
SchoolInfo item = slot.GetComponent<SchoolInfo>();
item.schoolName.text = sceneDate.Value.Name;
item.schoolId = sceneDate.Value.ID;
Toggle toggle = slot.GetComponent<Toggle>();
if (toggle != null)
{
// 将 Toggle 添加到 ToggleGroup 中
toggle.group = incidentGroup;
toggle.isOn = false;
schooltoggleList.Add(toggle);
schoolInfoList.Add(item);
}
else
{
break;
}
toggle.onValueChanged.AddListener((isSelected) =>
{
if (isSelected) // 只有当Toggle被选中时才调用SelectEventBtn
{
this.schoolId = item.schoolId; // 获取 Text 组件的文本
this.schoolName = item.schoolName.text;
}
});
}
}
}
//动态加载对应的事件地点
public void InstantiateIncidentPrefab(int eventId)
{
foreach (var eventData in jsonReader.incidentSiteDictionary)
{
if (eventData.Key == eventId)
{
GameObject slot = GameObject.Instantiate<GameObject>(incidentPrefab, incidentList);
IncidentInfo item = slot.GetComponent<IncidentInfo>();
item.incidentText.text = eventData.Value.Note;
item.incidentID = eventData.Value.ID;
this.idcidentId = item.incidentID;
this.idcidentName = item.incidentText.text;
Toggle toggle = slot.GetComponent<Toggle>();
if (toggle != null)
{
// 将 Toggle 添加到 ToggleGroup 中
toggle.group = incidentGroup;
toggle.isOn = false;
eventToggleList.Add(toggle);
incidentInfos.Add(item);
}
else
{
break;
}
toggle.onValueChanged.AddListener((isSelected) =>
{
if (isSelected)
{
this.idcidentName = item.incidentText.text;
this.idcidentId = item.incidentID;
datePanel.placeId = item.incidentID;
string[] difficulty = eventData.Value.Difficulty.Split('|');
string[] roleGroups = eventData.Value.Role.Split('|'); // 分割Role字段得到多个角色组
// 确保difficultyList和difficulty的长度匹配
for (int i = 0; i < difficultyList.Count && i < difficulty.Length; i++)
{
Toggle item = difficultyList[i]; // 获取对应的Toggle
DifficultyInfo difficultyInfo = item.gameObject.GetComponent<DifficultyInfo>();
if (difficultyInfo == null)
{
difficultyInfo = item.gameObject.AddComponent<DifficultyInfo>();
}
// 给每个Toggle设置不同的difficulty值
difficultyInfo.difficulty = int.Parse(difficulty[i]);
// 将难度和对应的角色存入字典
if (difficultyInfo.difficulty == int.Parse(difficulty[i]))
{
// 获取与当前难度相关的角色组(根据索引)
string[] correspondingRoles = roleGroups[i].Split(',');
// 打印或者处理角色
List<string> rolesList = new List<string>(correspondingRoles); // 转换为List
if (!difficultyToRoles.ContainsKey(difficultyInfo.difficulty))
{
difficultyToRoles.Add(difficultyInfo.difficulty, rolesList);
}
else
{
difficultyToRoles[difficultyInfo.difficulty] = rolesList; // 如果已存在,更新角色列表
}
}
}
}
});
}
}
}
//点击继续按钮后更换场景图片及名称
public void InstantiateEventPrefab(int id)
{
incidentInfos.Clear();
foreach (Transform child in incidentList)
{
Destroy(child.gameObject);
}
text20008.text = sceneName;
// 通过 id 获取对应的 sceneData
if (jsonReader.sceneDictionary.TryGetValue(id, out var sceneData))
{
// 将 IncidentType 按照 '|' 分隔成一个数组
string[] incidentIds = sceneData.IncidentType.Split('|');
// 遍历每个分隔出来的 id
foreach (string incidentIdStr in incidentIds)
{
//Debug.Log("incidentIdStr" + incidentIdStr);
// 转换字符串为 int 类型
if (int.TryParse(incidentIdStr, out int incidentId))
{
// 根据 incidentId 获取对应的事件数据
EventData eventData=jsonReader.GetEvenById(incidentId);
string[] incidentIds2 = eventData.DisasterLocation.Split('|');
GameObject slot = GameObject.Instantiate<GameObject>(eventPrefab, eventList);
EventInfo item = slot.GetComponent<EventInfo>();
item.eventId = eventData.ID;
item.eventName.text = eventData.Note;
if (item.IsOpen)
{
item.maskImage.gameObject.SetActive(false);
}
else
{
item.maskImage.gameObject.SetActive(true);
}
Toggle toggle = slot.GetComponent<Toggle>();
if (toggle != null)
{
toggle.group = eventGroup;
eventInfoList.Add(item);
evnetsceneList.Add(toggle);
}
//添加点击事件
toggle.onValueChanged.AddListener((isSelected) =>
{
if (isSelected) // 只有当Toggle被选中时才调用SelectEventBtn
{
// 清空事件列表
incidentInfos.Clear();
foreach (Transform child in incidentList)
{
Destroy(child.gameObject);
}
this.eventName = item.eventName.text; // 获取 Text 组件的文本
this.eventId = item.eventId;
EventData eventData = jsonReader.GetEvenById(this.eventId);
foreach (string disasterLocation in incidentIds2)
{
InstantiateIncidentPrefab(int.Parse(disasterLocation));
OnRandomEventClick();
}
createTemplateInfo.Instance.auth_CreateTemplate.sceneId = item.eventId.ToString();
}
});
}
else
{
Debug.LogWarning($"Invalid Incident ID format: {incidentIdStr}");
}
}
}
else
{
Debug.LogWarning($"No scene data found for ID {id}");
}
}
//事件及难度选择
public void SelectEvent()
{
EventInfo eventInfo = GetComponentInChildren<EventInfo>();
foreach (EventInfo item in eventInfoList)
{
if (item.gameObject.transform.GetComponent<Toggle>().isOn)
{
this.eventId = item.eventId;
this.eventName = item.eventName.text;
}
}
}
//随机事件
public void OnRandomEventClick()
{
// 随机选择一个事件
if (eventInfoList.Count > 1)
{
int randomIndex = Random.Range(0, eventInfoList.Count);
// 取消所有Toggle的选中状态
foreach (Toggle toggle in eventToggleList)
{
// 先检查toggle是否为null
if (toggle != null)
{
toggle.isOn = false;
}
}
Debug.Log("=++++++++++++++++++++++=" + eventToggleList[randomIndex].GetComponent<EventInfo>());
// 选中随机事件
if (eventToggleList[randomIndex] != null && eventToggleList[randomIndex].GetComponent<IncidentInfo>().isOpen != false)
{
eventToggleList[randomIndex].isOn = true;
this.eventName = eventInfoList[randomIndex].eventName.text; // 设置随机选择的事件名称
Debug.Log("已选择随机事件: " + this.eventName);
}
else
{
Debug.LogWarning("随机选择的事件 Toggle 为 null");
}
}
else
{
Debug.LogWarning("只有一个事件!!!");
randomEventBtn.gameObject.SetActive(false);
}
}
//游戏难度选择
public void SelsctDifficulty()
{
auth_createTemplate auth_CreateTemplate = new auth_createTemplate();
foreach (Toggle item in difficultyList)
{
if (item.isOn)
{
DifficultyInfo difficultyInfo = item.GetComponent<DifficultyInfo>();
this.difficultyId = difficultyInfo.difficulty;
this.difficulty = item.transform.parent.GetComponentInChildren<Text>().text;
auth_CreateTemplate.mode = this.difficulty;//给结构体“难度”赋值
}
}
}
//继续按钮,点击后上传数据
public void OnClickContinueBtn()
{
//SelectSchoolBtn();
//SelectSceneBtn();
InstantiateEventPrefab(this.scnenId);
SetFirstChoise(evnetsceneList);
if (continueBtn.interactable)
{
sceneLable.gameObject.SetActive(false);
schoolChoiceLable.gameObject.SetActive(false);
eventChoiceLable.gameObject.SetActive(true);
incidentLable.gameObject.SetActive(true);
}
else
{
Debug.Log("没有选择场景或学校,继续按钮不可用!");
}
}
//提交按钮,点击上传数据和隐藏界面
public void SubmitBtn()
{
SelsctDifficulty();
SetDataPanelInfo();
this.gameObject.SetActive(false);
panel.gameObject.SetActive(true);
DisableUIInteraction();
panel.DynamicLoadingDuty(this.difficultyId);
isSure = true;
}
//关闭按钮
public void OnClickCloseBtn()
{
// 清空事件列表
eventInfoList.Clear();
// 销毁所有已实例化的事件预制体
foreach (Transform child in eventList)
{
Destroy(child.gameObject);
}
// 如果需要,也可以清空其他相关信息
evnetsceneList.Clear();
eventId = -1;
eventName = string.Empty;
if (schoolChoiceLable.gameObject.active == false)
{
sceneLable.gameObject.SetActive(true);
schoolChoiceLable.gameObject.SetActive(true);
eventChoiceLable.gameObject.SetActive(false);
incidentLable.gameObject.SetActive(false);
}
else
{
transform.gameObject.SetActive(false);
}
}
//通过判断Toggle的IsOn是否被打开来判断继续按钮是否置灰
public void IsClick()
{
bool anysceneSelected = false;
bool anyschoolSelected = false;
bool isOk = false;
// 检查场景选择
foreach (Toggle toggle in scenetoggleList)
{
if (toggle.isOn)
{
anysceneSelected = true;
break; // 如果有一个场景 Toggle 被选中,停止检查
}
}
foreach (Toggle toggle in schooltoggleList)
{
if (toggle.isOn)
{
anyschoolSelected = true;
break; // 如果有一个场景 Toggle 被选中,停止检查
}
}
// 根据是否有 Toggle 被选中,设置 ContinueBtn 是否可交互
if (continueBtn != null&& anysceneSelected==true&& anyschoolSelected==true)
{
isOk = true;
continueBtn.interactable = isOk; // 如果有选中的 Toggle继续按钮可交互否则不可交互
}
else
{
continueBtn.interactable = isOk;
Debug.LogError("ContinueBtn 按钮组件未找到!");
}
}
//通过判断EventSceneList里面的IsOn是否打开判断提交按钮是否置灰
public void IsSubmit()
{
bool anyToggleSelected = false;
bool anyeventSelected = false;
bool isOk = false;
//Debug.Log("evnetsceneList>>>>"+ evnetsceneList.Count());
// 检查场景选择
foreach (Toggle toggle in evnetsceneList)
{
if (toggle.isOn)
{
anyToggleSelected = true;
break; // 如果有一个场景 Toggle 被选中,停止检查
}
}
//检查事件选择
foreach (Toggle toggle in eventToggleList)
{
if (toggle.isOn)
{
anyeventSelected = true;
break; // 如果有一个场景 Toggle 被选中,停止检查
}
}
// 根据是否有 Toggle 被选中,设置 ContinueBtn 是否可交互
if (submitBtn != null && anyToggleSelected == true&& anyeventSelected==true)
{
isOk = true;
submitBtn.interactable = isOk; // 如果有选中的 Toggle继续按钮可交互否则不可交互
}
else
{
submitBtn.interactable = isOk;
}
}
//默认选择第一个Toggle
public void SetFirstChoise(List<Toggle> toggles)
{
// 如果有Toggle则选中第一个
if (toggles.Count() > 0)
{
toggles[0].isOn = true;
}
}
//场景toggle
public void SetToggleOpenorClose(List<SceneItem> sceneItems)
{
foreach (SceneItem item in sceneItems)
{
if (item.IsOpen == false)
{
item.GetComponent<Toggle>().interactable = false;
if (item.GetComponent<Toggle>().isOn)
{
item.GetComponent<Toggle>().isOn = false;
}
}
else
{
item.GetComponent<Toggle>().interactable = true;
}
}
}
//学校Toggle
public void SetScloolToggleOpenorClose(List<SchoolInfo> sceneItems)
{
foreach (SchoolInfo item in sceneItems)
{
if (item.isOpen == false)
{
item.GetComponent<Toggle>().interactable = true;
if (item.GetComponent<Toggle>().isOn)
{
item.GetComponent<Toggle>().isOn = false;
}
}
else
{
item.GetComponent<Toggle>().interactable = true;
}
}
}
//事件Toggle
public void SetEventToggleOpenorClose(List<IncidentInfo> sceneItems)
{
foreach (IncidentInfo item in sceneItems)
{
if (item.isOpen == false)
{
item.GetComponent<Toggle>().interactable = true;
if (item.GetComponent<Toggle>().isOn)
{
item.GetComponent<Toggle>().isOn = false;
}
}
else
{
item.GetComponent<Toggle>().interactable = true;
}
}
}
//设置日期界面的数据
public void SetDataPanelInfo()
{
datePanel.SchoolText.text = sceneName;
datePanel.PlaceText.text = idcidentName;
}
//按钮禁用
public void DisableUIInteraction()
{
// 禁用场景选择中的所有Toggle按钮
foreach (Toggle toggle in scenetoggleList)
{
if (toggle != null)
{
toggle.interactable = false;
}
}
// 禁用学校选择中的所有Toggle按钮
foreach (Toggle toggle in schooltoggleList)
{
if (toggle != null)
{
toggle.interactable = false;
}
}
// 禁用事件选择中的所有Toggle按钮
foreach (Toggle toggle in eventToggleList)
{
if (toggle != null)
{
toggle.interactable = false;
}
}
// 禁用事件场景选择中的所有Toggle按钮
foreach (Toggle toggle in evnetsceneList)
{
if (toggle != null)
{
toggle.interactable = false;
}
}
// 禁用继续按钮
continueBtn.interactable = false;
// 禁用提交按钮
submitBtn.interactable = false;
}
}