GameObjectControllerGameAction.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using System;
  2. using System.Collections;
  3. using System.Linq;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using Sirenix.OdinInspector;
  7. using KairoEngine.Core;
  8. namespace KairoEngine.Core.GameActions
  9. {
  10. [System.Serializable, HideReferenceObjectPicker]
  11. public class GameObjectControllerGameAction : GameActionBase
  12. {
  13. public enum ControllerAction
  14. {
  15. Enable,
  16. Disable,
  17. Destroy
  18. }
  19. public enum TargeVariableType
  20. {
  21. Direct,
  22. Variable
  23. }
  24. public override string name
  25. {
  26. get
  27. {
  28. return $"{action.ToString()} {GetObjName(target)}";
  29. }
  30. }
  31. public override GameActionsController controller {
  32. get => _controller;
  33. set
  34. {
  35. _controller = value;
  36. typeName = "GameObjectControllerGameAction";
  37. className = this.GetType().AssemblyQualifiedName;
  38. GetCompatibleVariablenames();
  39. }
  40. }
  41. public override string GetTypeName() => "GameObjectControllerGameAction";
  42. public override string GetActionName() => "Core/GameObject Controller";
  43. [IconFoldoutGroup("@name", "Assets/Plugins/KairoEngine/Core/Editor/Icons/InstantiateGameActionIcon.png")]
  44. public ControllerAction action = ControllerAction.Enable;
  45. [IconFoldoutGroup("@name")]
  46. public TargeVariableType targetType;
  47. [IconFoldoutGroup("@name"), ShowIf("@targetType == TargeVariableType.Direct"), NonSerialized, ShowInInspector]
  48. public GameObject target;
  49. [IconFoldoutGroup("@name"), ShowIf("@targetType == TargeVariableType.Variable"),ValueDropdown("possibleVariables", IsUniqueList = false)]
  50. public string targetVariable;
  51. private IEnumerable possibleVariables = new ValueDropdownList<string>();
  52. public GameObjectControllerGameAction(GameActionsController controller) : base(controller)
  53. {
  54. this.controller = controller;
  55. className = this.GetType().AssemblyQualifiedName;
  56. }
  57. private string GetObjName(GameObject obj)
  58. {
  59. if(obj == null) return "NULL";
  60. else return $"\'{obj.name}\'";
  61. }
  62. public override void Start()
  63. {
  64. GameObject obj;
  65. if(targetType == TargeVariableType.Direct) obj = target;
  66. else obj = GetVariable<GameObject>(targetVariable, null);
  67. if(obj != null)
  68. {
  69. switch (action)
  70. {
  71. case ControllerAction.Enable:
  72. obj.SetActive(true);
  73. break;
  74. case ControllerAction.Disable:
  75. obj.SetActive(false);
  76. break;
  77. case ControllerAction.Destroy:
  78. GameObject.Destroy(obj);
  79. break;
  80. default:
  81. break;
  82. }
  83. }
  84. _done = true;
  85. _started = true;
  86. }
  87. public override void Update() { }
  88. public override void Restart()
  89. {
  90. _done = false;
  91. _started = false;
  92. }
  93. private void GetCompatibleVariablenames()
  94. {
  95. if(_controller == null) return;
  96. if(_controller.context == null) return;
  97. possibleVariables = _controller.context.variables
  98. .Where(x => x.GetTypeName() is "GameActionContextGameObject")
  99. .Select(x => new ValueDropdownItem(x.name, x.name));
  100. }
  101. public static GameObjectControllerGameAction JSONToGameObjectControllerGameAction(string data)
  102. {
  103. return JsonUtility.FromJson<GameObjectControllerGameAction>(data);
  104. }
  105. private GameObjectControllerGameAction Duplicate(GameActionsController controller)
  106. {
  107. GameObjectControllerGameAction action = new GameObjectControllerGameAction(controller == null ? this.controller : controller);
  108. action.controller = controller;
  109. return action;
  110. }
  111. private T GetVariable<T>(string title, T defaultValue)
  112. {
  113. for (int i = 0; i < controller.context.variables.Count; i++)
  114. {
  115. if(controller.context.variables[i].name == title)
  116. {
  117. return controller.context.variables[i].GetValue<T>(defaultValue);
  118. }
  119. }
  120. return defaultValue;
  121. }
  122. public override void OnBeforeSerialize(GameActionObjectSerializer serializer, int n, int depth)
  123. {
  124. serializer.SerializeGameObject($"{depth}-{n}-Prefab", target);
  125. }
  126. public override void OnBeforeDeserialize(GameActionObjectSerializer serializer, int n, int depth)
  127. {
  128. target = serializer.DeserializeGameObject($"{depth}-{n}-Prefab");
  129. }
  130. }
  131. }