2025-05-18 01:04:31 +08:00

379 lines
8.3 KiB
C#

// ***********************************************************************
// Assembly : Game
// Author : Kimch
// Created : 2020-09-08
// Description :
// Last Modified By :
// Last Modified On :
// ***********************************************************************
// <copyright file= "MapPanel" company="Kimch"></copyright>
// <summary></summary>
// ***********************************************************************
using PureMVC.Interfaces;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
namespace G.UI
{
/// <summary>
/// 主线地图
/// </summary>
partial class MapPanel : KUIWidget
{
private class ChapterInfo
{
public readonly ItemChapter item;
public Rect rect;
public ChapterWidget widget;
public ChapterInfo(ItemChapter chapter)
{
this.item = chapter;
}
public void SetRect(Rect rect)
{
var cwRect = widget.GetComponent<RectTransform>();
cwRect.anchoredPosition = rect.position;
cwRect.sizeDelta = rect.size;
this.rect = rect;
}
public void SetIndex(int index, int maxIndex)
{
widget.SetIndex(index, maxIndex);
}
public void ShowChapter()
{
if (widget.data == null)
{
widget.SetData(item);
}
}
public bool IsLockChapter(int maxUnlockLevelId)
{
return maxUnlockLevelId < item.levels[0];
}
public bool IsNeedRefresh(int maxUnlockLevelId)
{
return item.levels[0] <= maxUnlockLevelId && item.levels[item.levels.Length - 1] >= (maxUnlockLevelId - 1);
}
}
#region Field
#pragma warning disable CS0649 // 从未对字段赋值,字段将一直保持其默认值 null
[KUIFlag]
ScrollRect _goMap;
[KUIFlag]
KUIList __listChapter;
[KUIFlag]
KUIList __listPlaces;
[KUIFlag]
Button _btnTips;
[KUIFlag]
TextMeshProUGUI _tmpTips;
[KUIFlag]
Transform _goBattleFx;
#pragma warning restore CS0649 // 字段已被赋值,但从未使用过它的值
private int _curChapterIndex;
private int _maxChapterIndex;
private int _curShowChapter;
private int _curShowLevel;
private ChapterInfo[] _chapterInfos;
private int _height;
#endregion
#region Method
public override void Refresh()
{
CheckLevel();
int maxShowChapter = LevelProxy.Instance.currentChapterId + 1;
if (_chapterInfos == null)
{
//__listMaps.Clear();
//_mapItems.Clear();
var chapters = ItemProxy.Instance.GetStaticItems<ItemChapter>();
_curChapterIndex = 0;
_maxChapterIndex = chapters.Count - 1;
//
_chapterInfos = new ChapterInfo[chapters.Count];
//
for (int i = 0; i <= _maxChapterIndex; i++)
{
_chapterInfos[i] = new ChapterInfo(chapters[i])
{
widget = __listChapter.GetItem<ChapterWidget>()
};
}
float height = 0f;
for (int i = 0; i <= _maxChapterIndex; i++)
{
var chapterInfo = _chapterInfos[i];
var rect = GlobalUtils.GetRect(chapterInfo.item.mapPos);
rect.x = 0;
rect.y = height;
rect.width = 0;
height += rect.height;
chapterInfo.SetRect(rect);
chapterInfo.SetIndex(i, _maxChapterIndex);
if (i < maxShowChapter)
chapterInfo.ShowChapter();
}
_height = (int)height;
_curShowLevel = LevelProxy.Instance.currentUnlockedLevel;
}
else
{
//
for (int i = 0; i <= _maxChapterIndex; i++)
{
if (i < maxShowChapter)
{
_chapterInfos[i].ShowChapter();
}
}
var maxUnlockLevel = LevelProxy.Instance.currentUnlockedLevel;
if (_curShowLevel < maxUnlockLevel)
{
_curShowLevel = maxUnlockLevel;
for (int i = _chapterInfos.Length - 1; i >= 0; i--)
{
if (_chapterInfos[i].IsNeedRefresh(maxUnlockLevel))
_chapterInfos[i].widget.Refresh();
}
}
else if (_curShowLevel > maxUnlockLevel)
{
_curShowLevel = maxUnlockLevel;
for (int i = _chapterInfos.Length - 1; i >= 0; i--)
{
_chapterInfos[i].widget.Refresh();
}
}
}
CheckChapter(maxShowChapter);
PostNotification(GlobalDefine.EVENT_WINDOW_OPENED, 2, "");
}
private void CheckChapter(int maxShowChapter)
{
if (_curShowChapter == maxShowChapter)
{
return;
}
_curShowChapter = maxShowChapter;
float height = _height;
int index = maxShowChapter - 1;
if (index < _chapterInfos.Length)
height = _chapterInfos[index].rect.y + _chapterInfos[index].rect.height;
__listChapter.GetComponent<RectTransform>().sizeDelta = new Vector2(0, height);
SetVerticalNormalizedPosition(1f);
}
/// <summary>
///
/// </summary>
private void CheckLevel()
{
GlobalVar.ShowUnlockSystemLevelId = 0;
var levels = ItemProxy.Instance.GetStaticItems<ItemLevel>();
for (int i = 0; i < levels.Count; i++)
{
var level = levels[i];
if (!level.isCompleted && level.unlockIcon != null && level.unlockIcon.Length > 0)
{
GlobalVar.ShowUnlockSystemLevelId = level.id;
break;
}
}
}
private void ShowChapter(int index)
{
//var ci = _chapterInfos[index];
//if (ci.widget)
//{
// var cwRect = ci.widget.GetComponent<RectTransform>();
// cwRect.anchoredPosition = ci.rect.position;
// cwRect.sizeDelta = ci.rect.size;
// ci.widget.SetIndex(index, _maxChapterIndex);
// ci.widget.SetData(ci.item);
//}
}
private void HideChapter(int index)
{
}
/// <summary>
///
/// </summary>
/// <param name="value"></param>
private void SetVerticalNormalizedPosition(float value)
{
if (_goMap.verticalNormalizedPosition != value)
{
_goMap.verticalNormalizedPosition = value;
}
}
/// <summary>
///
/// </summary>
/// <param name="id"></param>
public void ScrollMap(int id)
{
int index = id - 1;
var chapters = ItemProxy.Instance.GetStaticItems<ItemChapter>();
int max = chapters.Count;
}
private IEnumerator ScrollMapCO(RectTransform rect)
{
yield return null;
var root = __listChapter.GetComponent<RectTransform>();
var bounds = RectTransformUtility.CalculateRelativeRectTransformBounds(root, rect);
SetVerticalNormalizedPosition(Mathf.Clamp01((bounds.center.y - Screen.height / 2 + 180) / root.sizeDelta.y));
}
private void OnMapScroll(Vector2 vector)
{
if (vector.y < 0f)
{
SetVerticalNormalizedPosition(0f);
vector.y = 0f;
}
for (int i = 0; i < _chapterInfos.Length; i++)
{
var rect = _chapterInfos[i].rect;
if (vector.y * _height < rect.y + rect.height)
{
break;
}
}
for (int i = 0; i < _chapterInfos.Length; i++)
{
_chapterInfos[i].widget.SetScroll(vector.y);
}
}
private void OnTipsBtnClick()
{
_btnTips.gameObject.SetActive(false);
}
private void ShowTips(string tips)
{
_btnTips.gameObject.SetActive(true);
_tmpTips.text = tips;
}
#endregion
#region Unity
// Use this for initialization
private void Awake()
{
this.SetViewData();
__listChapter.AddTemplate<ChapterWidget>(true);
_goMap.onValueChanged.AddListener(this.OnMapScroll);
_btnTips.onClick.AddListener(this.OnTipsBtnClick);
}
bool _started;
private void Start()
{
_started = true;
this.Refresh();
}
private void OnEnable()
{
RegisterThis();
if (_started)
this.Refresh();
}
private void OnDisable()
{
UnregisterThis();
}
#endregion
#region Meditor
readonly int[] _listNotificationInterests = new int[]
{
GlobalDefine.EVENT_LEVEL_INFO_CHANGED,
GlobalDefine.EVENT_LEVEL_ITEM_SHOW,
GlobalDefine.EVENT_LEVEL_TIPS,
};
public override IList<int> ListNotificationInterests()
{
return _listNotificationInterests;
}
public override void HandleNotification(INotification notification)
{
if (notification.Name == GlobalDefine.EVENT_LEVEL_INFO_CHANGED)
{
this.Refresh();
}
else if (notification.Name == GlobalDefine.EVENT_LEVEL_ITEM_SHOW)
{
StartCoroutine(ScrollMapCO((RectTransform)notification.Body));
//ScrollMap((RectTransform)notification.Body);
_goBattleFx.SetParent((RectTransform)notification.Body, false);
if (notification.Type == "minor")
_goBattleFx.localPosition = new Vector3(8f, -70f, 0f);
else
_goBattleFx.localPosition = new Vector3(-162f, 146f, 0f);
}
else if (notification.Name == GlobalDefine.EVENT_LEVEL_TIPS)
{
ShowTips(notification.Body as string);
}
}
#endregion
}
}