BehaviorTreeArgsDict.cs 2.0 KB

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