DebugLogGameAction.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 DebugLogGameActionType
  10. {
  11. String,
  12. Variable
  13. }
  14. public enum DebugLogGameActionlogType
  15. {
  16. Log,
  17. Warning,
  18. Error
  19. }
  20. [System.Serializable, HideReferenceObjectPicker]
  21. public class DebugLogGameAction : GameActionBase
  22. {
  23. public override string name
  24. {
  25. get
  26. {
  27. if(actionType == DebugLogGameActionType.Variable) return $"Debug {logType.ToString()} \'{GetVariable(variable)}\' ({variable})";
  28. else return $"Debug {logType.ToString()} \'{message}\'";
  29. }
  30. }
  31. public override GameActionsController controller {
  32. get => _controller;
  33. set
  34. {
  35. _controller = value;
  36. typeName = "DebugLogGameAction";
  37. className = this.GetType().AssemblyQualifiedName;
  38. GetCompatibleVariablenames();
  39. }
  40. }
  41. public override string GetTypeName() => "DebugLogGameAction";
  42. public override string GetActionName() => "Core/Debug Log";
  43. [IconFoldoutGroup("@name", "Assets/Plugins/KairoEngine/Core/Editor/Icons/DebugLogGameActionIcon.png")]
  44. public DebugLogGameActionType actionType = DebugLogGameActionType.String;
  45. [IconFoldoutGroup("@name"), ShowIf("@actionType == DebugLogGameActionType.String")]
  46. public string message = "Debug Log Message";
  47. [IconFoldoutGroup("@name"), ShowIf("@actionType == DebugLogGameActionType.Variable")]
  48. [ValueDropdown("possibleVariables", IsUniqueList = false)]
  49. public string variable;
  50. [IconFoldoutGroup("@name")] public DebugLogGameActionlogType logType = DebugLogGameActionlogType.Log;
  51. private IEnumerable possibleVariables = new ValueDropdownList<string>();
  52. public DebugLogGameAction(GameActionsController controller) : base(controller)
  53. {
  54. this.controller = controller;
  55. className = this.GetType().AssemblyQualifiedName;
  56. }
  57. public override void Start()
  58. {
  59. if(actionType == DebugLogGameActionType.String) LogMessage(message, logType);
  60. else if(actionType == DebugLogGameActionType.Variable)
  61. {
  62. string value = GetVariable(variable);
  63. if(value != "") LogMessage(value, logType);
  64. }
  65. _done = true;
  66. _started = true;
  67. }
  68. public override void Update() { }
  69. public override void Restart()
  70. {
  71. _done = false;
  72. _started = false;
  73. }
  74. private void GetCompatibleVariablenames()
  75. {
  76. if(_controller == null) return;
  77. if(_controller.context == null) return;
  78. possibleVariables = _controller.context.variables
  79. //.Where(x => x.data is GameActionContextString)
  80. .Select(x => new ValueDropdownItem(x.name, x.name));
  81. }
  82. public static DebugLogGameAction JSONToDebugLogGameAction(string data)
  83. {
  84. return JsonUtility.FromJson<DebugLogGameAction>(data);
  85. }
  86. private DebugLogGameAction Duplicate(GameActionsController controller = null)
  87. {
  88. DebugLogGameAction action = new DebugLogGameAction(controller == null ? this.controller : controller);
  89. action.controller = controller;
  90. action.actionType = actionType;
  91. action.message = message;
  92. action.variable = variable;
  93. return action;
  94. }
  95. private string GetVariable(string title)
  96. {
  97. if(controller == null) return "";
  98. if(controller.context == null) return "";
  99. for (int i = 0; i < controller.context.variables.Count; i++)
  100. {
  101. if(controller.context.variables[i].name == title)
  102. {
  103. string value = controller.context.variables[i].GetValue<string>("");
  104. if(value != "") return value;
  105. }
  106. }
  107. return "";
  108. }
  109. private void LogMessage(string value, DebugLogGameActionlogType logType)
  110. {
  111. switch (logType)
  112. {
  113. case DebugLogGameActionlogType.Log:
  114. Debug.Log(value);
  115. break;
  116. case DebugLogGameActionlogType.Warning:
  117. Debug.LogWarning(value);
  118. break;
  119. case DebugLogGameActionlogType.Error:
  120. Debug.LogError(value);
  121. break;
  122. default:
  123. break;
  124. }
  125. }
  126. }
  127. }