WXGame/box1/Assets/Script/UI/LevelPanel.cs

389 lines
11 KiB
C#
Raw Normal View History

2024-11-26 15:44:50 +08:00
using System;
2024-11-08 18:21:41 +08:00
using System.Collections;
using System.Collections.Generic;
2024-12-18 11:12:52 +08:00
using System.IO;
2024-11-26 15:44:50 +08:00
using System.Linq;
using cfg;
2024-11-29 21:37:01 +08:00
using DG.Tweening;
2024-11-26 15:44:50 +08:00
using TMPro;
2024-12-18 11:12:52 +08:00
using TTSDK;
2024-11-26 15:44:50 +08:00
using Unity.VisualScripting;
2024-11-08 18:21:41 +08:00
using UnityEngine;
2024-11-26 15:44:50 +08:00
using UnityEngine.UI;
2024-11-08 18:21:41 +08:00
public class LevelPanel : MonoBehaviour
{
2024-11-26 15:44:50 +08:00
public int ChapterID => chapterId;
private int chapterId;
public int LevelID => levelID;
2024-11-08 18:21:41 +08:00
private int levelID;
2024-12-18 11:12:52 +08:00
[SerializeField] private Text name;
[SerializeField] private Image playerImage;
public Sprite myWXPic;
2024-11-26 15:44:50 +08:00
[SerializeField] private Button startGameButton;
[SerializeField] private Button upButton;
[SerializeField] private Button dowButton;
[SerializeField] private Button mainButton;
[SerializeField] private Button homeButton;
[SerializeField] private Image homeImage;
2024-11-08 18:21:41 +08:00
[SerializeField] private List<LevelButton> tourObjects;
2024-11-26 15:44:50 +08:00
[SerializeField] private List<TaskItem> _list;
[SerializeField] private List<Image> starList;
[SerializeField] private TextMeshProUGUI leveName;
[SerializeField] private TextMeshProUGUI levelText;
2024-11-29 21:37:01 +08:00
[SerializeField] private AnimationCurve ani;
[SerializeField] private GameObject wait;
2024-12-18 11:12:52 +08:00
[SerializeField] private GameObject bginner;
[SerializeField] private GameObject loading;
2024-11-29 21:37:01 +08:00
[SerializeField] private Transform spiriStart;
[SerializeField] private Transform spiriEnd;
[SerializeField] private Transform spiriAni;
[SerializeField] private Slider _slider;
2024-12-17 13:37:43 +08:00
[SerializeField] private TextMeshProUGUI physical;
2024-11-29 21:37:01 +08:00
private int aniIndex;
2024-11-26 15:44:50 +08:00
private void OnEnable()
{
startGameButton.onClick.AddListener(StartGame);
upButton.onClick.AddListener(UpData);
dowButton.onClick.AddListener(DownData);
homeButton.onClick.AddListener(HomeEvent);
2024-12-17 13:37:43 +08:00
DataManager.AddPhysical();
2024-11-29 21:37:01 +08:00
2024-11-26 15:44:50 +08:00
}
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;
}
}
2024-12-17 13:37:43 +08:00
2024-11-26 15:44:50 +08:00
}
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("未解锁");
}
2024-11-08 18:21:41 +08:00
public void StartPanel(int id)
{
2024-12-18 11:12:52 +08:00
if (GameManager.ins.firstPlay)
{
loading.gameObject.SetActive(true);
StartCoroutine(loadWait());
}
else
{
loading.gameObject.SetActive(false);
}
2024-11-08 18:21:41 +08:00
if (id==0)
{
id = 11001;
}
2024-11-29 21:37:01 +08:00
chapterId = id;
DataManager.chapter = id;
2024-11-08 18:21:41 +08:00
// var bo=DataManager.LevelOpen(id);
var levelData = JsonTab.Instance.tables.Chapter.Get(id);
2024-11-26 15:44:50 +08:00
levelText.text = JsonTab.Instance.tables.DisplayTxt.Get(levelData.Chaptername).Text;
2024-11-08 18:21:41 +08:00
foreach (var levelButton in tourObjects)
{
2024-11-21 09:35:48 +08:00
levelButton._panel = this;
2024-11-08 18:21:41 +08:00
levelButton.Reset();
}
int index = 0;
foreach (var data in levelData.Contain)
{
tourObjects[index].SetLevelData(data);
2024-11-26 15:44:50 +08:00
var _blevelData= JsonTab.Instance.tables.Level.Get(data);
if (DataManager.LevelOpen(_blevelData.Front))
{
levelID = data;
}
2024-11-08 18:21:41 +08:00
index++;
}
2024-11-26 15:44:50 +08:00
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);
}
2024-11-29 21:37:01 +08:00
homeImage.GetComponent<RectTransform>().DOLocalMove(new Vector3(0,10,0),5f,true).SetEase(ani).SetLoops(-1);
wait.SetActive(false);
spiriAni.localPosition = new Vector3(1000, 1000,0);
aniIndex = 0;
2024-12-18 11:12:52 +08:00
if (DataManager.GetInt("startbginner")!=1)
{
bginner.gameObject.SetActive(true);
DataManager.SetInt("startbginner", 1);
}
else
{
bginner.gameObject.SetActive(false);
}
TT.GetUserInfo(
(ref TTUserInfo scUserInfo) =>
{
name.text = scUserInfo.nickName;
SetAsyncImage(scUserInfo.avatarUrl, playerImage);
BoxDebug.Log($"TestGetUserInfo info: {scUserInfo.ToString()}");
},
(errMsg) =>
{
BoxDebug.Log($"TestGetUserInfo fail: {errMsg}");
});
2024-11-26 15:44:50 +08:00
}
2024-12-18 11:12:52 +08:00
IEnumerator loadWait()
{
yield return new WaitForSeconds(1f);
loading.gameObject.SetActive(false);
GameManager.ins.firstPlay = false;
}
public void SetAsyncImage(string url, Image image)
{
//开始下载图片前将UITexture的主图片设置为占位图
if (myWXPic != null)
{
image.sprite = myWXPic;
return;
}
//判断是否是第一次加载这张图片
if (!File.Exists(path + url.GetHashCode()))
{
//如果之前不存在缓存文件
StartCoroutine(DownloadImage(url, image));
}
else
{
StartCoroutine(LoadLocalImage(url, image));
}
}
IEnumerator DownloadImage(string url, Image image)
{
Debug.Log("downloading new image:" + path + url.GetHashCode());//url转换HD5作为名字
WWW www = new WWW(url);
yield return www;
Texture2D tex2d = www.texture;
//将图片保存至缓存路径
byte[] pngData = tex2d.EncodeToPNG(); //将材质压缩成byte流
File.WriteAllBytes(path + url.GetHashCode(), pngData); //然后保存到本地
Sprite m_sprite = Sprite.Create(tex2d, new Rect(0, 0, tex2d.width, tex2d.height), new Vector2(0, 0));
image.sprite = m_sprite;
myWXPic = m_sprite;
}
IEnumerator LoadLocalImage(string url, Image image)
{
string filePath = "file:///" + path + url.GetHashCode();
Debug.Log("getting local image:" + filePath);
WWW www = new WWW(filePath);
yield return www;
Texture2D texture = www.texture;
Sprite m_sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0, 0));
image.sprite = m_sprite;
myWXPic = m_sprite;
}
public string path
{
get
{
//pc,ios //android :jar:file//
return Application.persistentDataPath + "/ImageCache/";
}
}
2024-11-26 15:44:50 +08:00
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);
2024-11-29 21:37:01 +08:00
if (DataManager.GetTask(id, data.Maintasks))
{
starList[0].color = Color.white;
}
else
{
starList[0].color = Color.gray;
}
2024-11-26 15:44:50 +08:00
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);
2024-11-29 21:37:01 +08:00
if (DataManager.GetTask(id, data.Additionaltasks[i]))
{
starList[i+1].color=Color.white;
}
else
{
starList[i+1].color=Color.gray;
}
2024-11-26 15:44:50 +08:00
_list[i+1].InitData(id,data.Additionaltasks[i]);
}
homeImage.sprite = Resources.Load<Sprite>("Map/" + data.Icon);
}
public void StartGame()
{
2024-12-17 13:37:43 +08:00
if (DataManager.GetPhysical()>=5)
2024-11-29 21:37:01 +08:00
{
2024-12-17 13:37:43 +08:00
DataManager.RemovePhysical(5);
wait.SetActive(true);
spiriAni.position = spiriStart.position;
spiriAni.DOMove(spiriEnd.position, 2f).OnComplete(() =>
2024-11-29 21:37:01 +08:00
{
2024-12-17 13:37:43 +08:00
aniIndex++;
if (aniIndex>=2)
{
MainPanel.ins.StartPanelEvent();
GameManager.ins.soundSystem.EventAudio();
wait.SetActive(false);
}
});
DataManager.chapter = chapterId;
2024-12-18 11:12:52 +08:00
switch (levelID)
{
case 110012:
TT.ReportAnalytics<string>("10104");
break;
case 110013:
TT.ReportAnalytics<string>("10106");
break;
case 110015:
TT.ReportAnalytics<string>("10108");
break;
}
2024-12-17 13:37:43 +08:00
GameManager.ins.ResetLevel(levelID, () =>
2024-11-29 21:37:01 +08:00
{
2024-12-17 13:37:43 +08:00
aniIndex++;
if (aniIndex>=2)
{
MainPanel.ins.StartPanelEvent();
GameManager.ins.soundSystem.EventAudio();
wait.SetActive(false);
}
});
}
2024-11-08 18:21:41 +08:00
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
2024-12-17 13:37:43 +08:00
physical.text = DataManager.GetPhysical().ToString();
2024-11-08 18:21:41 +08:00
}
}