StringManipulationGameAction.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 StringManipulationGameAction : GameActionBase
  11. {
  12. public enum VariableType
  13. {
  14. String,
  15. Variable
  16. }
  17. public enum StringManipulation
  18. {
  19. AddToString,
  20. SubtractFromString,
  21. }
  22. public override string name
  23. {
  24. get
  25. {
  26. return $"Manipulate string";
  27. }
  28. }
  29. public override GameActionsController controller {
  30. get => _controller;
  31. set
  32. {
  33. _controller = value;
  34. typeName = "StringManipulationGameAction";
  35. className = this.GetType().AssemblyQualifiedName;
  36. GetCompatibleVariablenames();
  37. }
  38. }
  39. public override string GetTypeName() => "StringManipulationGameAction";
  40. public override string GetActionName() => "Core/String Manipulation";
  41. [IconFoldoutGroup("@name", "Assets/Plugins/KairoEngine/Core/Editor/Icons/DebugLogGameActionIcon.png")]
  42. public VariableType variableType1 = VariableType.String;
  43. [IconFoldoutGroup("@name"), ShowIf("@variableType1 == VariableType.String")]
  44. public string value1 = "custom string";
  45. [IconFoldoutGroup("@name"), ShowIf("@variableType1 == VariableType.Variable")]
  46. [ValueDropdown("possibleVariables", IsUniqueList = false)]
  47. public string variable1;
  48. [IconFoldoutGroup("@name")]
  49. public VariableType variableType2 = VariableType.String;
  50. [IconFoldoutGroup("@name"), ShowIf("@variableType2 == VariableType.String")]
  51. public string value2 = "custom string";
  52. [IconFoldoutGroup("@name"), ShowIf("@variableType2 == VariableType.Variable")]
  53. [ValueDropdown("possibleVariables", IsUniqueList = false)]
  54. public string variable2;
  55. [IconFoldoutGroup("@name")]
  56. public StringManipulation operation = StringManipulation.SubtractFromString;
  57. [IconFoldoutGroup("@name"), ShowIf("@operation == StringManipulation.AddToString")]
  58. public string separator = "";
  59. [IconFoldoutGroup("@name")]
  60. [ValueDropdown("possibleVariables", IsUniqueList = false)]
  61. public string saveToVariable;
  62. private string result = "";
  63. private IEnumerable possibleVariables = new ValueDropdownList<string>();
  64. public StringManipulationGameAction(GameActionsController controller) : base(controller)
  65. {
  66. this.controller = controller;
  67. className = this.GetType().AssemblyQualifiedName;
  68. }
  69. public override void Start()
  70. {
  71. result = "";
  72. if(variableType1 == VariableType.Variable) value1 = GetVariable(variable1);
  73. if(variableType2 == VariableType.Variable) value2 = GetVariable(variable2);
  74. switch (operation)
  75. {
  76. case StringManipulation.AddToString:
  77. result = value1 + separator + value2;
  78. break;
  79. case StringManipulation.SubtractFromString:
  80. result = value1.Replace(value2, "");
  81. break;
  82. default:
  83. break;
  84. }
  85. SaveToVariable(saveToVariable, result);
  86. _done = true;
  87. _started = true;
  88. }
  89. public override void Update() { }
  90. public override void Restart()
  91. {
  92. _done = false;
  93. _started = false;
  94. }
  95. private void GetCompatibleVariablenames()
  96. {
  97. if(_controller == null) return;
  98. if(_controller.context == null) return;
  99. possibleVariables = _controller.context.variables
  100. //.Where(x => x.data is GameActionContextString)
  101. .Select(x => new ValueDropdownItem(x.name, x.name));
  102. }
  103. public static StringManipulationGameAction JSONToStringManipulationGameAction(string data)
  104. {
  105. return JsonUtility.FromJson<StringManipulationGameAction>(data);
  106. }
  107. private StringManipulationGameAction Duplicate(GameActionsController controller = null)
  108. {
  109. StringManipulationGameAction action = new StringManipulationGameAction(controller == null ? this.controller : controller);
  110. action.controller = controller;
  111. action.variableType1 = variableType1;
  112. action.value1 = value1;
  113. action.variable1 = variable1;
  114. action.variableType2 = variableType2;
  115. action.value2 = value2;
  116. action.variable2 = variable2;
  117. action.operation = operation;
  118. action.separator = separator;
  119. action.saveToVariable = saveToVariable;
  120. return action;
  121. }
  122. private string GetVariable(string title)
  123. {
  124. if(controller == null) return "";
  125. if(controller.context == null) return "";
  126. for (int i = 0; i < controller.context.variables.Count; i++)
  127. {
  128. if(controller.context.variables[i].name == title)
  129. {
  130. string value = controller.context.variables[i].GetValue<string>("");
  131. if(value != "") return value;
  132. }
  133. }
  134. return "";
  135. }
  136. private void SaveToVariable(string title, string data)
  137. {
  138. if(controller == null) return;
  139. if(controller.context == null) return;
  140. for (int i = 0; i < controller.context.variables.Count; i++)
  141. {
  142. if(controller.context.variables[i].name == title)
  143. {
  144. controller.context.variables[i].SetValue<string>(data);
  145. break;
  146. }
  147. }
  148. }
  149. }
  150. }