//========ID================================ /// <summary> /// 通过ID获取节点引用 /// </summary> /// <param name="id"></param> /// <returns></returns> public NodeModifier GetNextNodeByID(int id) { if (_id == id) { return(this); } for (int i = 0; i < _nextNodeList.Count; i++) { NodeModifier node = _nextNodeList[i]; if (node == null) { continue; } //if (IsParent(node)) continue; if (node.Parent != this) { continue; } if (CompareNodeID(node, id)) { return(node); } NodeModifier node2 = node.GetNextNodeByID(id); if (node2 != null) { return(node2); } } return(null); }
/// <summary> /// 添加一个子节点 /// </summary> /// <param name="next"></param> public void AddNextNode(NodeModifier next) { if (!_nextNodeList.Contains(next)) { _nextNodeList.Add(next); } }
private bool CompareNodeID(NodeModifier node, int id) { if (node == null) { return(false); } return(node._id == id); }
public override void GetNextNodes(List <NodeModifier> nodes) { NodeModifier node = _nextNodeList.Find(n => n.Parent == this); if (node != null) { nodes.Add(node); } }
/// <summary> /// 删除其父节点 /// </summary> public void DeleteParent() { if (_lastNode == null) { return; } _lastNode.Remove(this); _lastNode = null; DeleteSingleConnectChildNode(); }
//Static============================================= //以下静态方法将会同时处理设置中的各项引用、依赖关系 //<<<=============================================== public static void Delete(NodeModifier node) { NodeModifier[] nodes = node.NextNodes; for (int i = 0; i < nodes.Length; i++) { node.Remove(nodes[i]); } node.DeleteParent(); node.RemoveFromContent(); }
public static bool SetParent(NodeModifier node, NodeModifier parentNode) { if (node.CanSetParent(parentNode)) { node.RemoveFromContent(); node.DeleteParent(); node.SetParent(parentNode); return(true); } return(false); }
/// <summary> /// 是否属于自身的父节点 /// </summary> /// <param name="node">可能是父节点的节点</param> /// <returns></returns> public bool IsParent(NodeModifier node) { if (_lastNode == null) { return(false); } if (_lastNode == node) { return(true); } return(_lastNode.IsParent(node)); }
/// <summary> /// 父节点转化为Layer,即层级数(用于保存) /// </summary> /// <param name="parent"></param> /// <param name="layer"></param> /// <returns></returns> public int ParentToLayer(NodeModifier parent, int layer = 0) { layer++; if (this == parent) { return(layer); } if (_lastNode == null) { return(0); } return(_lastNode.ParentToLayer(parent, layer)); }
/// <summary> /// 当添加至Story中时,将作为MIssion节点的任务描述类,修改为上述设置类型 /// 若新类型与旧类型不一致,将导致数据丢失 /// </summary> /// <param name="node"></param> protected override void OnAddedContentNode(NodeModifier node) { base.OnAddedContentNode(node); Mission mission = node as Mission; if (mission != null) { if (mission.MissionDescription == null || mission.MissionDescription.GetType().FullName != _missionDescriptionType) { mission.MissionDescription = ReflectionHelper.CreateInstance <MissionDescription>(_missionDescriptionType); } } }
/// <summary> /// 将节点添加至该容器。注意:不会更改节点本身容器数据! /// 使用节点SetContent代替 /// </summary> /// <param name="node"></param> /// <returns></returns> public bool AddContentNode(NodeModifier node) { if (_contenNodeList.Contains(node) || node == null) return false; //不允许已运行节点的子节点添加 //for (int i = 0; i < _contenNodeList.Count; i++) //{ // if (_contenNodeList[i].IsChild(node)) return false; //} _contenNodeList.Add(node); OnAddedContentNode(node); //node.SetContent(this); return true; }
public virtual void Deserialize(BinaryReader r) { UnityEngine.JsonUtility.FromJsonOverwrite(r.ReadString(), this); int count = r.ReadInt32(); for (int i = 0; i < count; i++) { //int layer = r.ReadInt32(); int id = r.ReadInt32(); bool isSingleNode = r.ReadBoolean(); NodeModifier node; if (isSingleNode) { node = GetContent().GetNodeByID(id);// GetTopParent().GetNextNodeByID(id);//LayerToParent(layer); if (node != null) { _nextNodeList.Add(node); } else { GetContent().OnNodeLoaded += (con) => { NodeModifier child = con.GetNodeByID(id); if (child != null) { _nextNodeList.Add(child); } } }; } else { string fullName = r.ReadString(); node = ReflectionHelper.CreateInstance <NodeModifier>(fullName); if (node == null) { #if UNITY_EDITOR UnityEngine.Debug.LogError("Error: The Mission Node [" + fullName + "] Was Lost!"); #endif //return; node = ReflectionHelper.CreateInstance <NodeModifier>("CryStory.Runtime._MissingNode"); } SetParent(node, this); node.Deserialize(r); } } OnLoaded(r); }
/// <summary> /// 若为容器,获取该容器中指定ID节点 /// </summary> /// <param name="id"></param> /// <returns></returns> public NodeModifier GetNodeByID(int id) { for (int i = 0; i < _contenNodeList.Count; i++) { if (_contenNodeList[i]._id == id) { return(_contenNodeList[i]); } NodeModifier node = _contenNodeList[i].GetNextNodeByID(id); if (node != null) { return(node); } } return(null); }
/// <summary> /// 将节点添加至该容器。注意:不会更改节点本身容器数据! /// 使用节点SetContent代替 /// </summary> /// <param name="node"></param> /// <returns></returns> public bool AddContentNode(NodeModifier node) { if (_contenNodeList.Contains(node) || node == null) { return(false); } //不允许已运行节点的子节点添加 //for (int i = 0; i < _contenNodeList.Count; i++) //{ // if (_contenNodeList[i].IsChild(node)) return false; //} _contenNodeList.Add(node); OnAddedContentNode(node); //node.SetContent(this); return(true); }
/// <summary> /// 是否可以将指定节点设置为自身的父节点 /// </summary> /// <param name="node"></param> /// <returns></returns> public bool CanSetParent(NodeModifier node) { if (node == this) { return(false); } if (node.IsChild(this, false)) { return(false); } if (node.IsParent(this)) { return(false); } return(true); }
protected void RemoveChild(NodeContent content, NodeModifier node) { if (content.RemoveContenNode(node)) { node.ForceStop(); } NodeModifier[] nodes = node.NextNodes; for (int i = 0; i < nodes.Length; i++) { if (nodes[i].Parent != node) { continue; } RemoveChild(content, nodes[i]); } }
//Save protected override void OnSaved(BinaryWriter w) { base.OnSaved(w); bool running = _running;//_tempNodeList != null; //if (running) running = _tempNodeList.Length > 0; w.Write(running); if (running) { //Save Oringin Node //处于正在运行节点,保存初始节点及当前运行节点ID w.Write(_tempNodeList.Length); for (int i = 0; i < _tempNodeList.Length; i++) { NodeModifier node = _tempNodeList[i]; System.Type type = node.GetType(); w.Write(type.FullName); node.Serialize(w); } w.Write(_contenNodeList.Count); for (int i = 0; i < _contenNodeList.Count; i++) { w.Write(_contenNodeList[i]._id); } } else { //未运行节点,直接保存当前节点即可 w.Write(_contenNodeList.Count); for (int i = 0; i < _contenNodeList.Count; i++) { NodeModifier node = _contenNodeList[i]; System.Type type = node.GetType(); w.Write(type.FullName); node.Serialize(w); } } }
//Save=========================================== public virtual void Serialize(BinaryWriter w) { w.Write(UnityEngine.JsonUtility.ToJson(this)); w.Write(_nextNodeList.Count); for (int i = 0; i < _nextNodeList.Count; i++) { NodeModifier node = _nextNodeList[i]; w.Write(node._id);//(ParentToLayer(node)); bool isSingleNode = node.Parent != this; w.Write(isSingleNode); if (!isSingleNode) { w.Write(node.GetType().FullName); node.Serialize(w); } } OnSaved(w); }
/// <summary> /// 是否是属于该节点的子节点,将检查所有的链表 /// </summary> /// <param name="node"></param> /// <param name="includeSingleNode">是否包括单方面链接的节点(父级具有子级,子级不具有父级)</param> /// <returns></returns> public bool IsChild(NodeModifier node, bool includeSingleNode = true) { if (includeSingleNode) { if (node._lastNode != this) { return(false); } } if (_nextNodeList.Contains(node)) { return(true); } for (int i = 0; i < _nextNodeList.Count; i++) { if (_nextNodeList[i].IsChild(node)) { return(true); } } return(false); }
protected override EnumResult OnProcessing(NodeContent content, NodeModifier[] nextNode) { switch (Mode) { case KillMode.Node: for (int i = 0; i < nextNode.Length; i++) { if (nextNode[i].Parent != this) content.RemoveContenNode(nextNode[i]); } break; case KillMode.NodeAndChild: for (int i = 0; i < nextNode.Length; i++) { if (nextNode[i].Parent != this) { RemoveChild(content, nextNode[i]); } } break; } return EnumResult.Success; }
/// <summary> /// 删除指定子节点 /// </summary> /// <param name="node"></param> public void Remove(NodeModifier node) { _nextNodeList.Remove(node); }
/// <summary> /// 将一组节点添加至容器 /// </summary> /// <param name="nodes"></param> public void AddContentNode(NodeModifier[] nodes) { for (int i = 0; i < nodes.Length; i++) { AddContentNode(nodes[i]); } }
/// <summary> ///设置父节点 /// </summary> /// <param name="node"></param> public void SetParent(NodeModifier node) { node.AddNextNode(this); _lastNode = node; }
/// <summary> /// 添加一个子节点 /// </summary> /// <param name="next"></param> public void AddNextNode(NodeModifier next) { if (!_nextNodeList.Contains(next)) _nextNodeList.Add(next); }
public void Load() { if (_SaveData.Length > 0) { using (MemoryStream ms = new MemoryStream(_SaveData)) { using (BinaryReader r = new BinaryReader(ms)) { float ver = r.ReadSingle(); if (ver != Story.SaveVersion) { Debug.LogError("Error:Archive data version not same! Curent: " + Story.SaveVersion + " Data: " + ver); #if UNITY_EDITOR if (!_queryloaded) { _queryloaded = true; if (!UnityEditor.EditorUtility.DisplayDialog("Error!", "Error:Archive data version not the same! Curent Version: " + Story.SaveVersion + " Data Version:" + ver, "Force Load", "Cancel")) { return; } } #endif } _queryloaded = false; string storyData = r.ReadString(); _story = JsonUtility.FromJson <Story>(storyData); _story.LoadOnlyThisNode(r); } } } if (_story == null) { _story = new Story(); } _haveNullData = false; for (int i = 0; i < _missionSaveList.Count; i++) { MissionData data = _missionSaveList[i]; if (!data.MissionObject) { Debug.Log("Mission Not Found: " + data.Name); _haveNullData = true; continue; } data.MissionObject.Load(); data.MissionObject._mission._name = data.Name; NodeModifier.SetContent(data.MissionObject._mission, _story); } //设置已连接的节点 for (int i = 0; i < _missionSaveList.Count; i++) { MissionData data = _missionSaveList[i]; if (!data.MissionObject) { continue; } if (data.MissionObject._nextMissionNodeList.Count > 0) { foreach (var item in data.MissionObject._nextMissionNodeList) { MissionData next = GetMissionSaveDataByName(item.Name); if (next == null) { continue; } if (next.MissionObject == null) { continue; } if (item.IsSingleNode) { data.MissionObject._mission.AddNextNode(next.MissionObject._mission); } else { Mission.SetParent(next.MissionObject._mission, data.MissionObject._mission); } } } } if (_haveNullData) { UnityEditor.EditorUtility.DisplayDialog("Attention!", "You have some Mission File Lost! ", "OK"); } }
/// <summary> /// 删除一个节点 /// </summary> public bool RemoveContenNode(NodeModifier node) { return(_contenNodeList.Remove(node)); }
/// <summary> /// 父节点转化为Layer,即层级数(用于保存) /// </summary> /// <param name="parent"></param> /// <param name="layer"></param> /// <returns></returns> public int ParentToLayer(NodeModifier parent, int layer = 0) { layer++; if (this == parent) return layer; if (_lastNode == null) return 0; return _lastNode.ParentToLayer(parent, layer); }
protected override void OnLoaded(BinaryReader r) { //清空当前容器节点,避免被重复添加 _contenNodeList.Clear(); base.OnLoaded(r); bool running = r.ReadBoolean(); //恢复初始节点 int count = r.ReadInt32(); for (int i = 0; i < count; i++) { string fullName = r.ReadString(); NodeModifier node = ReflectionHelper.CreateInstance <NodeModifier>(fullName); if (node == null) { node = ReflectionHelper.CreateInstance <NodeModifier>("CryStory.Runtime.MissingNode"); //It's mission //return; } node.SetContent(this); node.Deserialize(r); NodeModifier.SetContent(node, this); } if (running) { //恢复运行节点(以ID为主) count = r.ReadInt32(); List <NodeModifier> runningNode = new List <NodeModifier>(); for (int i = 0; i < count; i++) { for (int j = 0; j < _contenNodeList.Count; j++) { NodeModifier node = _contenNodeList[j].GetNextNodeByID(r.ReadInt32()); if (node != null) { runningNode.Add(node); break; } } } //装填缓存节点 _tempNodeList = _contenNodeList.ToArray(); //移除初始节点 for (int i = 0; i < _tempNodeList.Length; i++) { RemoveContenNode(_tempNodeList[i]); } //重新加入已运行节点 for (int i = 0; i < runningNode.Count; i++) { AddContentNode(runningNode[i]); } } if (OnNodeLoaded != null) { OnNodeLoaded.Invoke(this); } }
/// <summary> /// 删除一个节点 /// </summary> public bool RemoveContenNode(NodeModifier node) { return _contenNodeList.Remove(node); }
/// <summary> /// 获取指定节点的父节点是自身,并且指定节点并非子节点的父节点的子节点 /// </summary> /// <param name="node"></param> /// <returns></returns> public NodeModifier[] GetNextNodes(NodeModifier node) { return _nextNodeList.FindAll((n) => !node.IsParent(n) && n.Parent == node).ToArray(); }
/// <summary> /// 删除其父节点 /// </summary> public void DeleteParent() { if (_lastNode == null) return; _lastNode.Remove(this); _lastNode = null; DeleteSingleConnectChildNode(); }
/// <summary> /// 是否可以将指定节点设置为自身的父节点 /// </summary> /// <param name="node"></param> /// <returns></returns> public bool CanSetParent(NodeModifier node) { if (node == this) return false; if (node.IsChild(this, false)) return false; if (node.IsParent(this)) return false; return true; }
protected override EnumResult OnUpdate() { if (_contenNodeList.Count == 0) { return(EnumResult.Success); } //Run for (int i = 0; i < _contenNodeList.Count; i++) { if (i >= _contenNodeList.Count) { break; } EnumResult result = _contenNodeList[i].Tick(this); if (result != EnumResult.Running) { if (i >= _contenNodeList.Count) { break; } NodeModifier node = _contenNodeList[i]; if (result == EnumResult.Failed) { switch (node.RunMode) { case EnumRunMode.UntilSuccess: continue; case EnumRunMode.ReturnParentNode: _toRemoveNode.Add(node); _toAddNode.Add(node.Parent); continue; case EnumRunMode.StopNodeList: _toRemoveNode.Add(node); continue; } } _toRemoveNode.Add(node); //单独处理Decorator节点运行 //仅运行非单节点 //if (node is Decorator) //{ // NodeModifier child = System.Array.Find<NodeModifier>(node.NextNodes, (n) => n.Parent == node); // if (child != null) // _toAddNode.Add(child); //} //else node.GetNextNodes(_toAddNode); } } //UnityEngine.Profiler.EndSample(); ProcessNode(); return(EnumResult.Running); }
private bool CompareNodeID(NodeModifier node, int id) { if (node == null) return false; return node._id == id; }
public void Load() { if (_SaveData.Length > 0) { using (MemoryStream ms = new MemoryStream(_SaveData)) { using (BinaryReader r = new BinaryReader(ms)) { float ver = r.ReadSingle(); if (ver != Story.SaveVersion) { Debug.LogError("Error:Archive data version not same! Curent: " + Story.SaveVersion + " Data: " + ver); } string storyData = r.ReadString(); _story = JsonUtility.FromJson <Story>(storyData); _story.LoadOnlyThisNode(r); } } } if (_story == null) { _story = new Story(); } for (int i = 0; i < _missionSaveList.Count; i++) { MissionData data = _missionSaveList[i]; if (!data.MissionObject) { Debug.Log("Mission Not Found: " + data.Name); continue; } data.MissionObject.Load(); data.MissionObject._mission._name = data.Name; NodeModifier.SetContent(data.MissionObject._mission, _story); } //设置已连接的节点 for (int i = 0; i < _missionSaveList.Count; i++) { MissionData data = _missionSaveList[i]; if (!data.MissionObject) { continue; } if (data.MissionObject._nextMissionNodeList.Count > 0) { foreach (var item in data.MissionObject._nextMissionNodeList) { MissionData next = GetMissionSaveDataByName(item.Name); if (next == null) { continue; } if (next.MissionObject == null) { continue; } if (item.IsSingleNode) { data.MissionObject._mission.AddNextNode(next.MissionObject._mission); } else { Mission.SetParent(next.MissionObject._mission, data.MissionObject._mission); } } } } }
public static void SetToDefaltContent(NodeModifier node) { SetContent(node, node.GetContent()); }
protected void RemoveChild(NodeContent content, NodeModifier node) { if (content.RemoveContenNode(node)) node.ForceStop(); NodeModifier[] nodes = node.NextNodes; for (int i = 0; i < nodes.Length; i++) { if (nodes[i].Parent != node) continue; RemoveChild(content, nodes[i]); } }
/// <summary> /// 当添加至Story中时,将作为MIssion节点的任务描述类,修改为上述设置类型 /// 若新类型与旧类型不一致,将导致数据丢失 /// </summary> /// <param name="node"></param> protected override void OnAddedContentNode(NodeModifier node) { base.OnAddedContentNode(node); Mission mission = node as Mission; if (mission != null) { if (mission.MissionDescription == null || mission.MissionDescription.GetType().FullName != _missionDescriptionType) mission.MissionDescription = ReflectionHelper.CreateInstance<MissionDescription>(_missionDescriptionType); } }
public static void SetContent(NodeModifier node, NodeContent content) { node.RemoveFromContent(); node.DeleteParent(); node.SetContent(content); }
public static bool SetParent(NodeModifier node, NodeModifier parentNode) { if (node.CanSetParent(parentNode)) { node.RemoveFromContent(); node.DeleteParent(); node.SetParent(parentNode); return true; } return false; }
/// <summary> /// 获取指定节点的父节点是自身,并且指定节点并非子节点的父节点的子节点 /// </summary> /// <param name="node"></param> /// <returns></returns> public NodeModifier[] GetNextNodes(NodeModifier node) { return(_nextNodeList.FindAll((n) => !node.IsParent(n) && n.Parent == node).ToArray()); }
protected virtual void OnAddedContentNode(NodeModifier node) { }
/// <summary> /// 是否是属于该节点的子节点,将检查所有的链表 /// </summary> /// <param name="node"></param> /// <param name="includeSingleNode">是否包括单方面链接的节点(父级具有子级,子级不具有父级)</param> /// <returns></returns> public bool IsChild(NodeModifier node, bool includeSingleNode = true) { if (includeSingleNode) if (node._lastNode != this) return false; if (_nextNodeList.Contains(node)) return true; for (int i = 0; i < _nextNodeList.Count; i++) { if (_nextNodeList[i].IsChild(node)) return true; } return false; }
protected virtual EnumResult OnProcessing(NodeContent content, NodeModifier[] nextNode) { return EnumResult.Success; }
/// <summary> /// 是否属于自身的父节点 /// </summary> /// <param name="node">可能是父节点的节点</param> /// <returns></returns> public bool IsParent(NodeModifier node) { if (_lastNode == null) return false; if (_lastNode == node) return true; return _lastNode.IsParent(node); }