TriggerGenericEventGameAction.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. public enum GameActionStringType
  10. {
  11. String,
  12. Variable
  13. }
  14. [System.Serializable, HideReferenceObjectPicker]
  15. public class TriggerGenericEventGameAction : GameActionBase
  16. {
  17. public override string name
  18. {
  19. get
  20. {
  21. return "Trigger Generic Event";
  22. }
  23. }
  24. public override GameActionsController controller {
  25. get => _controller;
  26. set
  27. {
  28. _controller = value;
  29. typeName = "TriggerGenericEventGameAction";
  30. className = this.GetType().AssemblyQualifiedName;
  31. GetCompatibleVariablenames();
  32. }
  33. }
  34. public override string GetTypeName() => "TriggerGenericEventGameAction";
  35. public override string GetActionName() => "Core/Trigger Generic Event";
  36. [FoldoutGroup("@name")]
  37. public GameActionStringType actionType = GameActionStringType.String;
  38. [FoldoutGroup("@name"), ShowIf("@actionType == GameActionStringType.String")]
  39. public string eventName = "EventName";
  40. [FoldoutGroup("@name"), ShowIf("@actionType == GameActionStringType.Variable")]
  41. [ValueDropdown("possibleVariables", IsUniqueList = false)]
  42. public string eventNameVariable;
  43. private IEnumerable possibleVariables = new ValueDropdownList<string>();
  44. public TriggerGenericEventGameAction(GameActionsController controller) : base(controller)
  45. {
  46. this.controller = controller;
  47. className = this.GetType().AssemblyQualifiedName;
  48. }
  49. public override void Start()
  50. {
  51. string targetEvent = "";
  52. if(actionType == GameActionStringType.String) targetEvent = eventName;
  53. else if(actionType == GameActionStringType.Variable) targetEvent = GetVariable(eventNameVariable);
  54. GenericEvents.Trigger(targetEvent);
  55. _done = true;
  56. _started = true;
  57. }
  58. public override void Update() { }
  59. public override void Restart()
  60. {
  61. _done = false;
  62. _started = false;
  63. }
  64. private void GetCompatibleVariablenames()
  65. {
  66. if(_controller == null) return;
  67. if(_controller.context == null) return;
  68. possibleVariables = _controller.context.variables
  69. //.Where(x => x.data is GameActionContextString)
  70. .Select(x => new ValueDropdownItem(x.name, x.name));
  71. }
  72. public static TriggerGenericEventGameAction JSONToTriggerGenericEventGameAction(string data)
  73. {
  74. return JsonUtility.FromJson<TriggerGenericEventGameAction>(data);
  75. }
  76. private TriggerGenericEventGameAction Duplicate(GameActionsController controller = null)
  77. {
  78. TriggerGenericEventGameAction action = new TriggerGenericEventGameAction(controller == null ? this.controller : controller);
  79. action.controller = controller;
  80. action.actionType = actionType;
  81. action.eventName = eventName;
  82. action.eventNameVariable = eventNameVariable;
  83. return action;
  84. }
  85. private string GetVariable(string title)
  86. {
  87. if(controller == null) return "";
  88. if(controller.context == null) return "";
  89. for (int i = 0; i < controller.context.variables.Count; i++)
  90. {
  91. if(controller.context.variables[i].name == title)
  92. {
  93. string value = controller.context.variables[i].GetValue<string>("");
  94. if(value != "") return value;
  95. }
  96. }
  97. return "";
  98. }
  99. }
  100. }