WXGame/box1/Assets/Script/UI/LevelPanel.cs
2024-11-26 15:44:50 +08:00

226 lines
6.3 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using cfg;
using TMPro;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;
public class LevelPanel : MonoBehaviour
{
public int ChapterID => chapterId;
private int chapterId;
public int LevelID => levelID;
private int levelID;
[SerializeField] private Button startGameButton;
[SerializeField] private Button upButton;
[SerializeField] private Button dowButton;
[SerializeField] private Button mainButton;
[SerializeField] private Button homeButton;
[SerializeField] private Image homeImage;
[SerializeField] private List<LevelButton> tourObjects;
[SerializeField] private List<TaskItem> _list;
[SerializeField] private List<Image> starList;
[SerializeField] private TextMeshProUGUI leveName;
[SerializeField] private TextMeshProUGUI levelText;
private void OnEnable()
{
startGameButton.onClick.AddListener(StartGame);
upButton.onClick.AddListener(UpData);
dowButton.onClick.AddListener(DownData);
homeButton.onClick.AddListener(HomeEvent);
}
private void OnDisable()
{
startGameButton.onClick.RemoveListener(StartGame);
upButton.onClick.RemoveListener(UpData);
dowButton.onClick.RemoveListener(DownData);
homeButton.onClick.RemoveListener(HomeEvent);
}
private void HomeEvent()
{
MainPanel.ins.HomePanel();
}
private void UpData()
{
var levelData = JsonTab.Instance.tables.Chapter.Get(chapterId);
int index = levelData.Chapternext;
if (index-1<0)
{
BoxDebug.Log("已经最上级界面");
return;
}
foreach (var bc in JsonTab.Instance.tables.Chapter.DataList)
{
if (levelData.Chapternext-1==bc.Chapternext)
{
StartPanel(bc.ID);
return;
}
}
}
private void MainPanelOpen()
{
}
public void HomePanelOpen()
{
}
private void DownData()
{
int index = 0;
for (var i = 0; i < JsonTab.Instance.tables.Chapter.DataList.Count; i++)
{
if (JsonTab.Instance.tables.Chapter.DataList[i].ID==levelID)
{
index = i;
break;
}
}
if (index+1>=JsonTab.Instance.tables.Chapter.DataList.Count)
{
BoxDebug.Log("已经最后页面");
return;
}
foreach (var bc in JsonTab.Instance.tables.Chapter.DataList[index+1].Contain)
{
var data= JsonTab.Instance.tables.Level.Get(bc);
if (DataManager.LevelOpen(data.Front))
{
StartPanel(JsonTab.Instance.tables.Chapter.DataList[index+1].ID);
return;
}
}
BoxDebug.Log("未解锁");
}
public void StartPanel(int id)
{
chapterId = id;
if (id==0)
{
id = 11001;
}
// var bo=DataManager.LevelOpen(id);
var levelData = JsonTab.Instance.tables.Chapter.Get(id);
levelText.text = JsonTab.Instance.tables.DisplayTxt.Get(levelData.Chaptername).Text;
foreach (var levelButton in tourObjects)
{
levelButton._panel = this;
levelButton.Reset();
}
int index = 0;
foreach (var data in levelData.Contain)
{
tourObjects[index].SetLevelData(data);
var _blevelData= JsonTab.Instance.tables.Level.Get(data);
if (DataManager.LevelOpen(_blevelData.Front))
{
levelID = data;
}
index++;
}
ResetButton();
SetTaskDisplay(levelID);
var bdata= JsonTab.Instance.tables.Level.Get(levelID);
homeImage.sprite = Resources.Load<Sprite>("Map/"+bdata.Icon);
upButton.gameObject.SetActive(levelData.Chapternext!=1);
BChapter nextDatas = null;
foreach (var bChapter in JsonTab.Instance.tables.Chapter.DataList.Where(bChapter => bChapter.Chaptername==levelData.Chaptername+1))
{
nextDatas = bChapter;
}
if (nextDatas==null)
{
dowButton.gameObject.SetActive(false);
}
else
{
foreach (var ctx in nextDatas.Contain)
{
var _blevelData= JsonTab.Instance.tables.Level.Get(ctx);
if (DataManager.LevelOpen(_blevelData.Front))
{
dowButton.gameObject.SetActive(true);
return;
}
}
dowButton.gameObject.SetActive(false);
}
}
private void ResetButton()
{
foreach (var button in tourObjects)
{
button.SelelObj();
}
}
public void SaveLevelID(int id)
{
levelID = id;
ResetButton();
SetTaskDisplay(id);
}
public void SetTaskDisplay(int id)
{
var data=JsonTab.Instance.tables.Level.Get(id);
leveName.text = JsonTab.Instance.tables.DisplayTxt.Get(data.Name).Text;
_list[0].InitData(id, data.Maintasks);
starList[0].color=DataManager.GetTask(id, data.Maintasks)? Color.white:Color.gray;
for (int i = 1; i < _list.Count; i++)
{
_list[i].gameObject.SetActive(false);
starList[i].gameObject.SetActive(false);
}
for (int i = 0; i < data.Additionaltasks.Count; i++)
{
_list[i+1].gameObject.SetActive(true);
starList[i+1].gameObject.SetActive(true);
starList[i+1].color=DataManager.GetTask(id, data.Additionaltasks[i])? Color.white:Color.gray;
_list[i+1].InitData(id,data.Additionaltasks[i]);
}
homeImage.sprite = Resources.Load<Sprite>("Map/" + data.Icon);
}
public void StartGame()
{
GameManager.ins.ResetLevel(levelID, () =>
{
MainPanel.ins.StartPanelEvent();
gameObject.SetActive(false);
GameManager.ins.soundSystem.EventAudio();
});
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}