NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Gameplay/UI/RedPoint/RedPointManager.cs

146 lines
3.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using cfg.RedPoint;
using Framework;
using System;
using System.Collections.Generic;
using PhxhSDK;
using UnityEngine;
using Debug = DebugUtil;
public partial class RedPointManager : Singlenton<RedPointManager>, IInitable
{
const string RED_POINT_NODE_TYPE_PREFIX = "RedPointNode";
public const int RED_POINT_NODE_ROOT_FLAG = 0;
private List<RedPointNode> _treeRoots = new();
private Dictionary<int, RedPointNode> _nodeDic = new();
public void Init()
{
RegisterEvent();
}
public void Release()
{
UnRegisterEvent();
DisposeAllNodes();
_treeRoots = null;
_nodeDic = null;
}
private void RegisterEvent()
{
EventManager.Instance.Register(EventManager.EventName.AllConfigLoad, OnConfigLoad);
}
private void UnRegisterEvent()
{
EventManager.Instance.Unregister(EventManager.EventName.AllConfigLoad, OnConfigLoad);
}
private void OnConfigLoad()
{
ConstructTreeFromConfig();
InitAllNodes();
}
private void ConstructTreeFromConfig()
{
ConstructAllNode();
ConstructNodeRelationship();
}
private void ConstructAllNode()
{
var config = TableManager.Instance.Tables.RedPointConfig.DataMap;
foreach (var data in config.Values)
{
var id = data.Id;
var node = GetANode(id, data.Type);
if (node != null)
{
_nodeDic.Add(id, node);
if (data.ParentID == RED_POINT_NODE_ROOT_FLAG)
{
_treeRoots.Add(node);
}
}
else
{
Debug.LogError($"转换Node失败,nodeID:{data.Id}");
}
}
}
private void ConstructNodeRelationship()
{
var config = TableManager.Instance.Tables.RedPointConfig.DataMap;
foreach (var data in config.Values)
{
if (data.ParentID != RED_POINT_NODE_ROOT_FLAG)
{
var node = GetNodeByID(data.Id);
if (node != null)
{
var parentNode = GetNodeByID(data.ParentID);
if (parentNode != null)
{
parentNode.AddChild(node);
node.parent = parentNode;
}
}
}
}
}
private void InitAllNodes()
{
foreach (var node in _nodeDic.Values)
{
node.Init();
}
}
private void DisposeAllNodes()
{
foreach (var node in _nodeDic.Values)
{
node.Dispose();
}
}
private RedPointNode GetANode(int id, NodeType nodeType, object param = null)
{
if (nodeType == NodeType.InValid)
{
Debug.LogError($"RedPoint Type非法请修改配置id:{id}");
return null;
}
RedPointNode result = null;
var nodeTypeStr = RED_POINT_NODE_TYPE_PREFIX + nodeType.ToString();
Type type = CommonUtilsFramework.GamePlayAssembly.GetType(nodeTypeStr);
if (type != null)
{
result = Activator.CreateInstance(type, id) as RedPointNode;
if (result != null)
result.param = param;
}
else
{
Debug.LogError($"枚举NodeType转Type失败请检查是否有对应类型!nodeType:{nodeType.ToString()}");
}
return result;
}
#region API
public RedPointNode GetNodeByID(int nodeID)
{
return _nodeDic.GetValueOrDefault(nodeID, null);
}
#endregion
}