NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Gameplay/UI/VehicleCultivate/VehicleCultivateManager.cs

883 lines
28 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;
using cfg.VehicleCfg;
using cfg.VihecleCultivateCfg;
using Framework;
using Gameplay;
using Gameplay.Net;
using PhxhSDK;
using System.Collections.Generic;
public class VehicleCultivateManager : Singlenton<VehicleCultivateManager>, IInitable
{
private List<VehicleTreeNode> vehicleTreeNodes = new List<VehicleTreeNode>();
public List<VehicleTreeNode> VehicleTrees
{
get { return vehicleTreeNodes; }
}
Dictionary<long, VehicleTreeNode> vehicleNodesMap = new Dictionary<long, VehicleTreeNode>();
private List<VehicleComponentTreeNode> vehicleComponentTreeNodes = new List<VehicleComponentTreeNode>();
public List<VehicleComponentTreeNode> VehicleComponentTrees
{
get { return vehicleComponentTreeNodes; }
}
Dictionary<long, VehicleComponentTreeNode> vehicleComponentNodesMap = new Dictionary<long, VehicleComponentTreeNode>();
#region IInitable
public void Init()
{
vehicleTreeNodes.Clear();
vehicleNodesMap.Clear();
vehicleComponentTreeNodes.Clear();
vehicleComponentNodesMap.Clear();
EventManager.Instance.Register(EventManager.EventName.AllConfigLoad, OnAllConfigLoad);
//LoadVehicleTreeNode();
}
public void Release()
{
vehicleTreeNodes.Clear();
vehicleNodesMap.Clear();
vehicleComponentTreeNodes.Clear();
vehicleComponentNodesMap.Clear();
EventManager.Instance.Unregister(EventManager.EventName.AllConfigLoad, OnAllConfigLoad);
}
private void OnAllConfigLoad()
{
var vehicleCfg = TableManager.Instance.Tables.VehicleConfig;
var componentCfg = TableManager.Instance.Tables.VehicleComponent;
var conditionCfg = TableManager.Instance.Tables.ConditionConfig;
LoadVehicleTreeNode(vehicleCfg, componentCfg, conditionCfg, out List<VehicleTreeNode> vehicles, out Dictionary<long, VehicleTreeNode> vehiclesMap,
out List<VehicleComponentTreeNode> components, out Dictionary<long, VehicleComponentTreeNode> componentsMap);
vehicleTreeNodes = vehicles;
vehicleNodesMap = vehiclesMap;
vehicleComponentTreeNodes = components;
vehicleComponentNodesMap = componentsMap;
}
#endregion
public TreeNodeBase Get(long id)
{
if (vehicleNodesMap.TryGetValue(id, out var node))
{
return node;
}
if(vehicleComponentNodesMap.TryGetValue(id, out var componentNode))
{
return componentNode;
}
DebugUtil.LogError("VehicleCultivateManager.Get: id not found: {0}", id);
return null;
}
/// <summary>
/// 将载具树和部件树加载到内存
/// </summary>
/// <param name="vehicles"></param>
/// <param name="vehiclesMap"></param>
/// <param name="components"></param>
/// <param name="componentsMap"></param>
public static void LoadVehicleTreeNode(VehicleConfig vehicleCfg, VehicleComponent componentCfg, ConditionConfig condition, out List<VehicleTreeNode> vehicles, out Dictionary<long, VehicleTreeNode> vehiclesMap,
out List<VehicleComponentTreeNode> components, out Dictionary<long, VehicleComponentTreeNode> componentsMap)
{
vehicles = new();
vehiclesMap = new();
components = new();
componentsMap = new();
var Table = vehicleCfg;//TableManager.Instance.Tables.VehicleConfig;
for (int i = 0; i < Table.DataList.Count; i++)
{
VehicleTreeNode node = new VehicleTreeNode();
node.ID = Table.DataList[i].ID;
//node.Type = VehicleTreeNodeType.Vehicle;
node.ParentID = new();
foreach(var unlockID in Table.DataList[i].UnlockID)
{
if (condition.DataMap.TryGetValue(unlockID, out var unlockCfg))
{
node.ParentID.Add(unlockCfg.Param1);
}
}
vehicles.Add(node);
vehiclesMap.Add(Table.DataList[i].ID, node);
}
var TableComponent = componentCfg;//TableManager.Instance.Tables.VehicleComponent;
for (int i = 0; i < TableComponent.DataList.Count; i++)
{
VehicleComponentTreeNode node = new VehicleComponentTreeNode();
node.ID = TableComponent.DataList[i].ComponentID;
//node.Type = VehicleTreeNodeType.Component;
node.ParentID = new List<long>();
if (TableComponent.DataList[i].ParentComponentID != 0)
{
node.ParentID.Add(TableComponent.DataList[i].ParentComponentID);
}
node.VehicleID = TableComponent.DataList[i].VehicleID;
components.Add(node);
componentsMap.Add(TableComponent.DataList[i].ComponentID, node);
}
foreach (var node in components)
{
//将部件节点添加到各节点的子节点列表
if (node.ParentID.Count == 0)
{
//组件直接与载具关联
if (vehiclesMap.TryGetValue(node.VehicleID, out VehicleTreeNode vehicle))//Get(node.VehicleID) as VehicleTreeNode;
vehicle?.ComponentChildren.Add(node.ID);
}
else
{
//父节点是组件
foreach (var id in node.ParentID)
{
if (componentsMap.TryGetValue(id, out VehicleComponentTreeNode component))//var component = Get(node.ParentID) as VehicleComponentTreeNode;
component?.ComponentChildren.Add(node.ID);
}
}
}
foreach (var node in vehicles)
{
//载具节点添加到各节点的子节点列表
if (node.ParentID.Count != 0)
{
if (Table.GetOrDefault((int)node.ID)?.UnlockID.Length != 0)
{
foreach (var id in node.ParentID)
{
if (vehiclesMap.TryGetValue(id, out VehicleTreeNode vehicle))
vehicle?.VehicleChildren.Add(node.ID);
if (componentsMap.TryGetValue(id, out VehicleComponentTreeNode component))
component?.VehicleChildren.Add(node.ID);
}
}
}
}
}
/// <summary>
/// 获取载具森林
/// </summary>
/// <returns></returns>
public VehicleTreeNode GetVehicleForest(cfg.VihecleCultivateCfg.VehicleType vehicleType)
{
VehicleTreeNode root = new VehicleTreeNode();
root.ID = 0;
foreach (var node in vehicleTreeNodes)
{
var vehicleCfg = TableManager.Instance.Tables.VehicleConfig.GetOrDefault((int)node.ID);
if (vehicleCfg == null)
{
DebugUtil.LogError("VehicleCultivateManager.GetVehicleForest: vehicleCfg is null");
continue;
}
else if (vehicleType != VehicleType.None && vehicleCfg.VehicleType != vehicleType)
{
continue;
}
if (vehicleCfg.UnlockID.Length == 0)//如果前置类型为空 则为无前置根节点
{
root.VehicleChildren.Add(node.ID);
}
else if (vehicleCfg.UnlockID.Length > 0)//如果有前置解锁ID,则不是根节点
{
continue;
}
else
{
//TODO 其他方式解锁载具判断
DebugUtil.LogWarning("载具解锁判断未实现");
root.VehicleChildren.Add(node.ID);
}
}
return root;
}
/// <summary>
/// 根据载具ID获取局部科技树内的子孙节点
/// </summary>
/// <param name="ID"></param>
/// <returns></returns>
public List<TreeNodeBase> GetPartialTree(long ID)
{
List<TreeNodeBase> retList = new List<TreeNodeBase>();
Queue<TreeNodeBase> queue = new Queue<TreeNodeBase>();
TreeNodeBase root = Get(ID);
if (root != null)
{
queue.Enqueue(root);
bool isRoot = true;
while (queue.Count > 0)
{
TreeNodeBase node = queue.Dequeue();
retList.Add(node);
if (node is VehicleTreeNode && !isRoot)
continue;
isRoot = false;
List<TreeNodeBase> children = node.GetChildNodes();
foreach (var child in children)
{
queue.Enqueue(child);
}
}
}
return retList;
}
/// <summary>
/// 查找载具升级树中的子节点载具
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public List<VehicleTreeNode> GetVehicleChildren(long id)
{
List<VehicleTreeNode> retList = new List<VehicleTreeNode>();
VehicleTreeNode root = Get(id) as VehicleTreeNode;
if (root != null)
{
retList.AddRange(root.VehicleChildren.ConvertAll(x => Get(x) as VehicleTreeNode));
}
else
{
DebugUtil.LogError("VehicleCultivateManager.GetVehicleChildren: root is null");
return retList;
}
Queue<VehicleComponentTreeNode> queue = new Queue<VehicleComponentTreeNode>();
foreach (var child in root.ComponentChildren)
{
queue.Enqueue(Get(child) as VehicleComponentTreeNode);
}
while (queue.Count > 0)
{
var node = queue.Dequeue();
if (node is VehicleComponentTreeNode componentNode)
{
foreach (var child in componentNode.ComponentChildren)
{
queue.Enqueue(Get(child) as VehicleComponentTreeNode);
}
foreach (var child in componentNode.VehicleChildren)
{
retList.Add(Get(child) as VehicleTreeNode);
}
}
else
{
//retList.Add(node as VehicleTreeNode);
DebugUtil.LogError("VehicleCultivateManager.GetVehicleChildren: node is null");
}
}
return retList;
}
/// <summary>
/// 查找载具升级树中的父节点载具
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public VehicleTreeNode GetVehicleParent(long id)
{
VehicleTreeNode node = Get(id) as VehicleTreeNode;
if (node != null)
{
var parent = node.GetDirectVehicleParentNode() as VehicleTreeNode;
return parent;
}
return null;
}
/// <summary>
/// 根据ID判断升级节点状态
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public TreeNodeState GetTreeNodeState(long id)
{
var node = Get(id);
if (node.IsUpgraded())
{
return TreeNodeState.Upgraded;
}
else
{
if (node is VehicleTreeNode vehicleNode)
{
if (TableManager.Instance.Tables.VehicleConfig.GetOrDefault((int)id)?.UnlockID.Length > 0)
{
foreach (var parentID in vehicleNode.ParentID)
{
var parent = Get(parentID);
if (!parent.IsUpgraded())
{
return TreeNodeState.Locked;
}
}
return TreeNodeState.Upgradable;
}
else
{
//TODO 其他方式解锁载具判断
DebugUtil.LogWarning("载具解锁判断未实现");
return TreeNodeState.Upgradable;
}
}
else if (node is VehicleComponentTreeNode componentNode)
{
foreach (var compParentID in componentNode.ParentID)
{
var parentNode = Get(compParentID);
if (!parentNode.IsUpgraded())
{
return TreeNodeState.Locked;
}
}
return TreeNodeState.Upgradable;
}
else
{
DebugUtil.LogError("VehicleCultivateManager.GetTreeNodeState: node is null");
return TreeNodeState.None;
}
}
}
}
public class VehicleTreeNode : TreeNodeBase
{
/// <summary>
/// 前置解锁ID
/// </summary>
public override List<long> ParentID { get => base.ParentID; set => base.ParentID = value; }
public override List<TreeNodeBase> GetChildNodes()
{
List<TreeNodeBase> ret = new();
ret.AddRange(VehicleChildren.ConvertAll(x => VehicleCultivateManager.Instance.Get(x)));
ret.AddRange(ComponentChildren.ConvertAll(x => VehicleCultivateManager.Instance.Get(x)));
return ret;
}
/// <summary>
/// 获取载具父节点
/// </summary>
/// <returns></returns>
public override TreeNodeBase GetDirectVehicleParentNode()
{
if (ParentID.Count > 0 && TableManager.Instance.Tables.VehicleConfig.GetOrDefault((int)ID)?.UnlockID.Length > 0)
{
var parentID = ParentID[0];//当前需求内部件仅有一个载具父节点
var componentNode = VehicleCultivateManager.Instance.Get(parentID);
if (componentNode != null)
{
return componentNode.GetDirectVehicleParentNode();
}
else
{
DebugUtil.LogError("VehicleTreeNode.GetParentNode: componentNode is null");
return null;
}
}
else
{
return null;
}
}
public override bool IsUpgraded()
{
//载具是否强化直接查找PartCharacter内是否有该载具的强化数据
var data = Actor.Instance.Character.GetVehicleDevelopment(ID);
if(data != null)
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// 计算当前载具属性
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public VehicleAttribute CalculateVehicleAttribute()
{
VehicleTreeNode root = this;
Queue<TreeNodeBase> queue = new Queue<TreeNodeBase>();
bool isRoot = true;
VehicleAttribute ret = new VehicleAttribute();
while (queue.Count > 0)
{
var node = queue.Dequeue();
if (node is VehicleTreeNode vehicleNode && isRoot)//如果是载具根节点,获取基础属性
{
isRoot = false;
var cfg = TableManager.Instance.Tables.VehicleConfig.GetOrDefault((int)vehicleNode.ID);
if (cfg != null)
{
ret.Add(new VehicleAttribute(cfg));
}
foreach (var child in vehicleNode.VehicleChildren)
{
queue.Enqueue(VehicleCultivateManager.Instance.Get(child));
}
}
else if (node is VehicleComponentTreeNode componentNode && node.IsUpgraded())//如果是强化过的部件,加上部件属性
{
var cfg = TableManager.Instance.Tables.VehicleComponent.GetOrDefault((int)componentNode.ID);
if (cfg != null)
{
ret.Add(new VehicleAttribute(cfg));
}
foreach (var child in componentNode.ComponentChildren)
{
queue.Enqueue(VehicleCultivateManager.Instance.Get(child));
}
}
}
//TODO 如果有其他属性加成,再加上
return ret;
}
public override ItemCounts GetUpgradeCost()
{
var cfg = TableManager.Instance.Tables.VehicleConfig.GetOrDefault((int)ID);
if (cfg != null)
{
ItemCounts ret = new ItemCounts();
foreach (var item in cfg.UnlockCost)
{
ret.Ids.Add((uint)item.Id);
ret.Counts.Add((uint)item.Count);
}
return ret;
}
else
{
DebugUtil.LogError("Get Vehicle cfg is null,id: " + ID);
return null;
}
}
}
public class VehicleComponentTreeNode : TreeNodeBase
{
/// <summary>
/// 父部件ID
/// </summary>
public override List<long> ParentID { get => base.ParentID; set => base.ParentID = value; }
/// <summary>
/// 配置表内组件所属载具ID
/// </summary>
public long VehicleID { get; set; }
public override List<TreeNodeBase> GetChildNodes()
{
List<TreeNodeBase> ret = new();
ret.AddRange(ComponentChildren.ConvertAll(x => VehicleCultivateManager.Instance.Get(x)));
ret.AddRange(VehicleChildren.ConvertAll(x => VehicleCultivateManager.Instance.Get(x)));
return ret;
}
/// <summary>
/// 获取载具父节点
/// </summary>
/// <returns></returns>
public override TreeNodeBase GetDirectVehicleParentNode()
{
if (ParentID.Count == 0)//直接与载具关联
{
var parentID = VehicleID;
return VehicleCultivateManager.Instance.Get(parentID);
}
else//与部件关联
{
var parentID = ParentID[0];//当前需求内部件仅有一个载具父节点
//仅选择一条路线
var parentComponentNode = VehicleCultivateManager.Instance.Get(parentID);
if (parentComponentNode != null)//获取部件的父节点
{
return parentComponentNode.GetDirectVehicleParentNode();
}
else
{
DebugUtil.LogError("VehicleComponentTreeNode.GetParentNode: parentComponentNode is null");
var parentVehicleNode = VehicleCultivateManager.Instance.Get(VehicleID);
return parentVehicleNode;//返回载具节点
}
}
}
public override ItemCounts GetUpgradeCost()
{
var cfg = TableManager.Instance.Tables.VehicleComponent.GetOrDefault(ID);
if (cfg != null)
{
ItemCounts ret = new ItemCounts();
foreach(var item in cfg.UpgradeCost)
{
ret.Ids.Add((uint)item.Id);
ret.Counts.Add((uint)item.Count);
}
return ret;
}
else
{
DebugUtil.LogError("Get VehicleComponent cfg is null,id: " + ID);
return null;
}
}
public override bool IsUpgraded()
{
//载具部件检查需要去找到对应的载具节点下是否有该强化部件
var vehicleNode = GetDirectVehicleParentNode() as VehicleTreeNode;
if(vehicleNode != null)
{
var ID = vehicleNode.ID;
var data = Actor.Instance.Character.GetVehicleDevelopment(ID);
if (data != null)
{
return data.UpgradedComponents.Contains((uint)this.ID);
}
else
{
//未找到强化载具数据 默认未强化
return false;
}
}
else
{
DebugUtil.LogError("VehicleComponentTreeNode.IsUpgraded: vehicleNode is null");
return false;
}
}
}
//public enum VehicleTreeNodeType
//{
// Vehicle,
// Component
//}
public enum TreeNodeState
{
None,//无
Upgradable,//可升级
Upgraded,//已升级
Locked//未解锁
}
/// <summary>
/// 载具属性 (升级相关
/// </summary>
public class VehicleAttribute
{
/// <summary>
/// 防御力
/// </summary>
public int Defense;
/// <summary>
/// 血量
/// </summary>
public int Hp;
/// <summary>
/// 伤害
/// </summary>
public int Damage;
/// <summary>
/// 换弹时间
/// </summary>
public float ReloadTime;
/// <summary>
/// 射速
/// </summary>
public float FireRate;
/// <summary>
/// 士气压制
/// </summary>
public int MoraleDamage;
/// <summary>
/// 对人伤害
/// </summary>
public float ScaleForHuman;
/// <summary>
/// 对载具伤害
/// </summary>
public float ScaleForVehicle;
/// <summary>
/// 暴击率
/// </summary>
public float CriticalRate;
/// <summary>
/// 暴击伤害
/// </summary>
public float CriticalDamage;
public VehicleAttribute()
{
Defense = 0;
Hp = 0;
Damage = 0;
ReloadTime = 0;
FireRate = 0;
MoraleDamage = 0;
ScaleForHuman = 0;
ScaleForVehicle = 0;
CriticalRate = 0;
CriticalDamage = 0;
}
public VehicleAttribute(DataVehicleComponent cfg)
{
Damage = 0;
Defense = cfg.Defense;
Hp = cfg.HP;
ReloadTime = 0;
FireRate = 0;
MoraleDamage = 0;
ScaleForHuman = 0;
ScaleForVehicle = 0;
CriticalRate = 0;
CriticalDamage = 0;
}
public VehicleAttribute(DataVehicle cfg)
{
Damage = 0;
Defense = cfg.Defense;
Hp = cfg.HP;
ReloadTime = 0;
FireRate = 0;
MoraleDamage = 0;
ScaleForHuman = 0;
ScaleForVehicle = 0;
CriticalRate = cfg.CriticalRate;
CriticalDamage = cfg.CriticalScale;
}
public void Add(VehicleAttribute attribute)
{
Defense += attribute.Defense;
Hp += attribute.Hp;
Damage += attribute.Damage;
ReloadTime += attribute.ReloadTime;
FireRate += attribute.FireRate;
MoraleDamage += attribute.MoraleDamage;
ScaleForHuman += attribute.ScaleForHuman;
ScaleForVehicle += attribute.ScaleForVehicle;
CriticalRate += attribute.CriticalRate;
CriticalDamage += attribute.CriticalDamage;
}
}
public abstract class TreeNodeBase
{
public long ID { get; set; }
//public VehicleTreeNodeType Type { get; set; }
public virtual List<long> ParentID { get; set; }
/// <summary>
/// 载具树子节点
/// </summary>
public List<long> VehicleChildren = new List<long>();
/// <summary>
/// 部件树子节点
/// </summary>
public List<long> ComponentChildren = new List<long>();
public abstract List<TreeNodeBase> GetChildNodes();
/// <summary>
/// 获取最近载具父节点
/// </summary>
/// <returns></returns>
public abstract TreeNodeBase GetDirectVehicleParentNode();
/// <summary>
/// 节点是否已升级
/// </summary>
/// <returns></returns>
public abstract bool IsUpgraded();
public abstract ItemCounts GetUpgradeCost();
/// <summary>
/// 升级,升级沿途所有未升级的载具和部件
/// </summary>
public virtual bool Upgrade()
{
var path = GetUpgradePath();
//TODO 判断升级材料是否足够
ItemCounts cost = new ItemCounts();
if (path.Count > 0)
{
var head = path[0];
List<long> vehicleNodes = new();
List<long> componentNodes = new();
for (int i = 0; i < path.Count; i++)
{
if (path[i] is VehicleTreeNode vehicleNode)
{
vehicleNodes.Add(vehicleNode.ID);
var cfg = TableManager.Instance.Tables.VehicleConfig.GetOrDefault((int)vehicleNode.ID);
if (cfg != null)
{
foreach (var item in cfg.UnlockCost)
{
if (cost.Ids.Contains((uint)item.Id))
{
var index = cost.Ids.IndexOf((uint)item.Id);
cost.Counts[index] += (uint)item.Count;
}
else
{
cost.Ids.Add((uint)item.Id);
cost.Counts.Add((uint)item.Count);
}
}
}
}
else if (path[i] is VehicleComponentTreeNode componentNode)
{
componentNodes.Add(componentNode.ID);
var cfg = TableManager.Instance.Tables.VehicleComponent.GetOrDefault((int)componentNode.ID);
if (cfg != null)
{
foreach (var item in cfg.UpgradeCost)
{
if (cost.Ids.Contains((uint)item.Id))
{
var index = cost.Ids.IndexOf((uint)item.Id);
cost.Counts[index] += (uint)item.Count;
}
else
{
cost.Ids.Add((uint)item.Id);
cost.Counts.Add((uint)item.Count);
}
}
}
}
}
for (int index = 0; index < cost.Counts.Count; index++)
{
var itemCount = CategoryManager.Instance.GetItemCount((int)cost.Ids[index]);
if (itemCount < cost.Counts[index])
{
DebugUtil.Log("这里需要配置本地化文本");
CommonUtils.ShowMessageTips("材料不足");
return false;
}
}
NetHelper.VehicleUpgrade(vehicleIDs: vehicleNodes, componentIDs: componentNodes, type: (uint)(head is VehicleTreeNode ? 1 : 2));
return true;
}
return false;
}
/// <summary>
/// 获取升级路径
/// </summary>
/// <param name="path"></param>
public List<TreeNodeBase> GetUpgradePath()
{
List<TreeNodeBase> path = new();
Queue<TreeNodeBase> queue = new Queue<TreeNodeBase>();
queue.Enqueue(this);
while (queue.Count > 0)
{
var point = queue.Dequeue();
if (point.IsUpgraded())//如果已经升级则退出
{
continue;
}
path.Add(point);
TreeNodeBase parent;
if (point is VehicleTreeNode vehicleNode)
{
foreach(var id in vehicleNode.ParentID)
{
parent = VehicleCultivateManager.Instance.Get(id);
if(parent != null)
{
queue.Enqueue(parent);
}
}
}
else if (point is VehicleComponentTreeNode componentNode)
{
if (componentNode.ParentID.Count == 0)
{
parent = VehicleCultivateManager.Instance.Get(componentNode.VehicleID);
}
else
{
foreach (var id in componentNode.ParentID)
{
parent = VehicleCultivateManager.Instance.Get(id);
if (parent != null)
{
queue.Enqueue(parent);
}
}
}
}
else
{
DebugUtil.Log("Vehicle GetUpgradePath: point is null");
continue;
}
}
if (path.Count > 1)
{
path.Reverse();
}
DebugUtil.Log("Vehicle GetUpgradePath: " + path.Count);
return path;
}
}