WXGame/box1/Assets/Script/UI/LevelPanel.cs
2024-12-21 14:11:17 +08:00

396 lines
12 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;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using cfg;
using DG.Tweening;
using TMPro;
using TTSDK;
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 Text name;
[SerializeField] private Image playerImage;
public Sprite myWXPic;
[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;
[SerializeField] private AnimationCurve ani;
[SerializeField] private GameObject wait;
[SerializeField] private GameObject bginner;
[SerializeField] private GameObject loading;
[SerializeField] private Transform spiriStart;
[SerializeField] private Transform spiriEnd;
[SerializeField] private Transform spiriAni;
[SerializeField] private TextMeshProUGUI physical;
private int aniIndex;
private void OnEnable()
{
startGameButton.onClick.AddListener(StartGame);
upButton.onClick.AddListener(UpData);
dowButton.onClick.AddListener(DownData);
homeButton.onClick.AddListener(HomeEvent);
DataManager.AddPhysical();
}
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)
{
if (GameManager.ins.firstPlay)
{
loading.gameObject.SetActive(true);
StartCoroutine(loadWait());
}
else
{
loading.gameObject.SetActive(false);
}
if (id==0)
{
id = 11001;
}
chapterId = id;
DataManager.chapter = id;
// 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);
}
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;
if (DataManager.GetInt("startbginner")!=1)
{
bginner.gameObject.SetActive(true);
DataManager.SetInt("startbginner", 1);
}
else
{
bginner.gameObject.SetActive(false);
}
TT.Login((code, anonymousCode, isLogin) =>
{
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}");
});
},
(msg) => { });
}
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/";
}
}
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);
if (DataManager.GetTask(id, data.Maintasks))
{
starList[0].color = Color.white;
}
else
{
starList[0].color = 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);
if (DataManager.GetTask(id, data.Additionaltasks[i]))
{
starList[i+1].color=Color.white;
}
else
{
starList[i+1].color=Color.gray;
}
_list[i+1].InitData(id,data.Additionaltasks[i]);
}
homeImage.sprite = Resources.Load<Sprite>("Map/" + data.Icon);
}
public void StartGame()
{
if (DataManager.GetPhysical()>=5)
{
DataManager.RemovePhysical(5);
wait.SetActive(true);
spiriAni.position = spiriStart.position;
spiriAni.DOMove(spiriEnd.position, 2f).OnComplete(() =>
{
aniIndex++;
if (aniIndex>=2)
{
MainPanel.ins.StartPanelEvent();
GameManager.ins.soundSystem.EventAudio();
wait.SetActive(false);
}
});
DataManager.chapter = chapterId;
switch (levelID)
{
case 110012:
TT.ReportAnalytics<string>("a10104");
DotData.ins.SendEvent("a10104","");
break;
case 110013:
TT.ReportAnalytics<string>("a10106");
DotData.ins.SendEvent("a10106","");
break;
case 110015:
TT.ReportAnalytics<string>("a10108");
DotData.ins.SendEvent("a10108","");
break;
}
GameManager.ins.ResetLevel(levelID, () =>
{
aniIndex++;
if (aniIndex>=2)
{
MainPanel.ins.StartPanelEvent();
GameManager.ins.soundSystem.EventAudio();
wait.SetActive(false);
}
});
bginner.gameObject.SetActive(false);
}
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
physical.text = DataManager.GetPhysical().ToString();
}
}