CompareStringsGameActionCondition.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using System.Collections;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using Sirenix.OdinInspector;
  6. using KairoEngine.Core;
  7. namespace KairoEngine.Core.GameActions
  8. {
  9. [System.Serializable, HideReferenceObjectPicker]
  10. public class CompareStringsGameActionCondition : GameActionCondition
  11. {
  12. public override string name
  13. {
  14. get
  15. {
  16. if(equals) return $"{variable} == \"{stringValue}\"";
  17. else return $"{variable} != \"{stringValue}\"";
  18. }
  19. }
  20. public override GameActionsController controller {
  21. get => _controller;
  22. set
  23. {
  24. _controller = value;
  25. typeName = "CompareStringsGameActionCondition";
  26. className = this.GetType().AssemblyQualifiedName;
  27. GetCompatibleVariablenames();
  28. }
  29. }
  30. public override string GetTypeName() => "CompareStringsGameActionCondition";
  31. public override string GetActionName() => "Compare Strings";
  32. [IconFoldoutGroup("@name","Assets/Plugins/KairoEngine/Core/Editor/Icons/CompareStringsGameActionConditionIcon.png")]
  33. public string stringValue = "";
  34. [ValueDropdown("possibleVariables", IsUniqueList = false)]
  35. [IconFoldoutGroup("@name")]
  36. public string variable;
  37. [IconFoldoutGroup("@name")]
  38. public bool equals = true;
  39. private IEnumerable possibleVariables = new ValueDropdownList<string>();
  40. public override bool Evaluate()
  41. {
  42. if(equals) return GetVariable(variable) == stringValue;
  43. else return GetVariable(variable) != stringValue;
  44. }
  45. private void GetCompatibleVariablenames()
  46. {
  47. if(_controller == null) return;
  48. if(_controller.context == null) return;
  49. possibleVariables = _controller.context.variables
  50. .Where(x => x.GetTypeName() == "GameActionContextString")
  51. .Select(x => new ValueDropdownItem(x.name, x.name));
  52. }
  53. public static CompareStringsGameActionCondition JSONToCompareStringsGameActionCondition(string data)
  54. {
  55. return JsonUtility.FromJson<CompareStringsGameActionCondition>(data);
  56. }
  57. private CompareStringsGameActionCondition Duplicate()
  58. {
  59. CompareStringsGameActionCondition condition = new CompareStringsGameActionCondition();
  60. condition.controller = controller;
  61. condition.stringValue = stringValue;
  62. condition.equals = equals;
  63. condition.variable = variable;
  64. return condition;
  65. }
  66. private string GetVariable(string title)
  67. {
  68. for (int i = 0; i < controller.context.variables.Count; i++)
  69. {
  70. if(controller.context.variables[i].name == title)
  71. {
  72. return controller.context.variables[i].GetValue<string>("");
  73. }
  74. }
  75. return "";
  76. }
  77. }
  78. }