BehaviorTreeArgsDict.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace Model
  5. {
  6. [Serializable]
  7. public class BehaviorTreeArgsDict
  8. {
  9. private readonly Dictionary<string, object> dict = new Dictionary<string, object>();
  10. public void Add(string key, object value)
  11. {
  12. this.dict.Add(key, value);
  13. }
  14. public void Remove(string key)
  15. {
  16. this.dict.Remove(key);
  17. }
  18. public object Get(string fieldName)
  19. {
  20. if (!this.dict.ContainsKey(fieldName))
  21. {
  22. //Log.Error($"fieldName:{fieldName} 不存在!!!!");
  23. return null;
  24. }
  25. return this.dict[fieldName];
  26. }
  27. public bool ContainsKey(string key)
  28. {
  29. return this.dict.ContainsKey(key);
  30. }
  31. public Dictionary<string, object> Dict()
  32. {
  33. return this.dict;
  34. }
  35. public BehaviorTreeArgsDict Clone()
  36. {
  37. BehaviorTreeArgsDict behaviorTreeArgsDict = new BehaviorTreeArgsDict();
  38. foreach (KeyValuePair<string, object> keyValuePair in this.dict)
  39. {
  40. behaviorTreeArgsDict.Add(keyValuePair.Key, Clone(keyValuePair.Value));
  41. }
  42. return behaviorTreeArgsDict;
  43. }
  44. public static object Clone(object obj)
  45. {
  46. Type vType = obj.GetType();
  47. if (!vType.IsSubclassOf(typeof(Array)))
  48. {
  49. return obj;
  50. }
  51. Array sourceArray = (Array)obj;
  52. Array dest = Array.CreateInstance(vType.GetElementType(), sourceArray.Length);
  53. Array.Copy(sourceArray, dest, dest.Length);
  54. return dest;
  55. }
  56. public void SetKeyValueComp(string fieldName, object value)
  57. {
  58. try
  59. {
  60. this.dict[fieldName] = value;
  61. }
  62. catch (Exception e)
  63. {
  64. Log.Error($"SetKeyValueComp error: {fieldName} {e}");
  65. }
  66. }
  67. public static bool SatisfyCondition(GameObject go, Type[] constraintTypes)
  68. {
  69. if (go == null || constraintTypes == null || constraintTypes.Length <= 0)
  70. {
  71. return true;
  72. }
  73. foreach (var constraint in constraintTypes)
  74. {
  75. if (go.GetComponent(constraint) == null)
  76. {
  77. Log.Error($"此GameObject必须包含:{constraint}");
  78. return false;
  79. }
  80. }
  81. return true;
  82. }
  83. }
  84. }