199 lines
5.4 KiB
C#
199 lines
5.4 KiB
C#
using Cal;
|
|
using ET;
|
|
using MongoDB.Bson.Serialization.Attributes;
|
|
using Sirenix.OdinInspector;
|
|
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using XNode;
|
|
|
|
[CreateAssetMenu]
|
|
public class SkillNodeGraph : NodeGraph
|
|
{
|
|
[BsonIgnore]
|
|
public int Id
|
|
{
|
|
get
|
|
{
|
|
int _skillId = Root.skillLogic.skillId;
|
|
if(_skillId == 0)
|
|
{
|
|
int.TryParse(name, out _skillId);
|
|
if (_skillId == 0)
|
|
{
|
|
Log.Error($"skillId ==0 when name = {name}");
|
|
}
|
|
}
|
|
return _skillId;
|
|
}
|
|
}
|
|
private RootNode root;
|
|
[BsonIgnore]
|
|
[ShowInInspector]
|
|
public RootNode Root=>root=root??nodes.Find(t => t is RootNode).As<RootNode>();
|
|
|
|
public int maxModifierId = 11;
|
|
public int maxUniqeId;
|
|
|
|
[SerializeField]
|
|
private Dictionary<int, SkillNode> nodeDic = new Dictionary<int, SkillNode>();
|
|
private Dictionary<int, SkillNode> NodeDic
|
|
{
|
|
get
|
|
{
|
|
if (nodeDic.Count == 0)
|
|
{
|
|
foreach (SkillNode node in nodes)
|
|
{
|
|
nodeDic.Add(node.uniqeId, node);
|
|
}
|
|
}
|
|
return nodeDic;
|
|
}
|
|
}
|
|
|
|
Vector2 pos = new Vector2(-600,-300);
|
|
int count = 0;
|
|
private void OnEnable()
|
|
{
|
|
activeGraph = this;
|
|
}
|
|
|
|
public Vector2 GetEmptyNodePos()
|
|
{
|
|
pos += new Vector2(300, 0);
|
|
if (count++ > 4)
|
|
{
|
|
count = 0;
|
|
pos += new Vector2(0, 600);
|
|
pos.x = 0;
|
|
}
|
|
return pos;
|
|
}
|
|
public SkillNode GetNode(int uniqeId)
|
|
{
|
|
if(!NodeDic.TryGetValue(uniqeId,out var skillNode))
|
|
Log.Error($"node is not in dic where id = {uniqeId}");
|
|
return skillNode;
|
|
}
|
|
public Node AddNodeWithId(Type type,int id)
|
|
{
|
|
Node.graphHotfix = this;
|
|
SkillNode node = ScriptableObject.CreateInstance(type) as SkillNode;
|
|
node.graph = this;
|
|
node.skillGraph = this;
|
|
nodes.Add(node);
|
|
node.uniqeId = id;
|
|
if (NodeDic.ContainsKey(node.uniqeId))
|
|
{
|
|
Log.Error($"dic has the id = {node.uniqeId}");
|
|
return node;
|
|
}
|
|
NodeDic.Add(node.uniqeId, node);
|
|
return node;
|
|
}
|
|
|
|
/// <summary> Add a node to the graph by type </summary>
|
|
public override Node AddNode(Type type)
|
|
{
|
|
Node.graphHotfix = this;
|
|
SkillNode node = ScriptableObject.CreateInstance(type) as SkillNode;
|
|
node.graph = this;
|
|
node.skillGraph = this;
|
|
nodes.Add(node);
|
|
node.uniqeId = maxUniqeId++;
|
|
if (NodeDic.ContainsKey(node.uniqeId))
|
|
{
|
|
Log.Error($"dic has the id = {node.uniqeId}");
|
|
return node;
|
|
}
|
|
NodeDic.Add(node.uniqeId, node);
|
|
return node;
|
|
}
|
|
|
|
/// <summary> Creates a copy of the original node in the graph </summary>
|
|
public override Node CopyNode(Node original)
|
|
{
|
|
Node.graphHotfix = this;
|
|
SkillNode node = ScriptableObject.Instantiate(original) as SkillNode;
|
|
node.graph = this;
|
|
node.skillGraph = this;
|
|
node.ClearConnections();
|
|
nodes.Add(node as SkillNode);
|
|
node.uniqeId = maxUniqeId++;
|
|
if (NodeDic.ContainsKey(node.uniqeId))
|
|
{
|
|
Log.Error($"dic has the id = {node.uniqeId}");
|
|
return node;
|
|
}
|
|
NodeDic.Add(node.uniqeId, node);
|
|
return node;
|
|
}
|
|
|
|
/// <summary> Safely remove a node and all its connections </summary>
|
|
/// <param name="node"> The node to remove </param>
|
|
public override void RemoveNode(Node node)
|
|
{
|
|
node.ClearConnections();
|
|
nodes.Remove(node as SkillNode);
|
|
nodeDic.Remove(node.As<SkillNode>().uniqeId);
|
|
if (Application.isPlaying) Destroy(node);
|
|
}
|
|
public void RemoveNodeWhitoutDic(Node node)
|
|
{
|
|
node.ClearConnections();
|
|
nodes.Remove(node as SkillNode);
|
|
if (Application.isPlaying) Destroy(node);
|
|
}
|
|
|
|
/// <summary> Remove all nodes and connections from the graph </summary>
|
|
public override void Clear()
|
|
{
|
|
if (Application.isPlaying)
|
|
{
|
|
for (int i = 0; i < nodes.Count; i++)
|
|
{
|
|
Destroy(nodes[i]);
|
|
}
|
|
}
|
|
nodes.Clear();
|
|
nodeDic.Clear();
|
|
}
|
|
|
|
/// <summary> Create a new deep copy of this graph </summary>
|
|
public override XNode.NodeGraph Copy()
|
|
{
|
|
// Instantiate a new nodegraph instance
|
|
SkillNodeGraph graph = Instantiate(this);
|
|
// Instantiate all nodes inside the graph
|
|
for (int i = 0; i < nodes.Count; i++)
|
|
{
|
|
if (nodes[i] == null) continue;
|
|
Node.graphHotfix = graph;
|
|
SkillNode node = Instantiate(nodes[i]) as SkillNode;
|
|
node.graph = graph;
|
|
node.skillGraph = graph;
|
|
graph.nodes[i] = node;
|
|
}
|
|
|
|
// Redirect all connections
|
|
for (int i = 0; i < graph.nodes.Count; i++)
|
|
{
|
|
if (graph.nodes[i] == null) continue;
|
|
foreach (NodePort port in graph.nodes[i].Ports)
|
|
{
|
|
port.Redirect(nodes.ConvertAll(a=>a as Node), graph.nodes);
|
|
}
|
|
}
|
|
|
|
return graph;
|
|
}
|
|
|
|
protected override void OnDestroy()
|
|
{
|
|
// Remove all nodes prior to graph destruction
|
|
Clear();
|
|
}
|
|
|
|
} |