using System.Collections; using System.Linq; using System.Collections.Generic; using UnityEngine; using Sirenix.OdinInspector; using KairoEngine.Core; namespace KairoEngine.Core.GameActions { public enum DebugLogGameActionType { String, Variable } public enum DebugLogGameActionlogType { Log, Warning, Error } [System.Serializable, HideReferenceObjectPicker] public class DebugLogGameAction : GameActionBase { public override string name { get { if(actionType == DebugLogGameActionType.Variable) return $"Debug {logType.ToString()} \'{GetVariable(variable)}\' ({variable})"; else return $"Debug {logType.ToString()} \'{message}\'"; } } public override GameActionsController controller { get => _controller; set { _controller = value; typeName = "DebugLogGameAction"; className = this.GetType().AssemblyQualifiedName; GetCompatibleVariablenames(); } } public override string GetTypeName() => "DebugLogGameAction"; public override string GetActionName() => "Core/Debug Log"; [IconFoldoutGroup("@name", "Assets/Plugins/KairoEngine/Core/Editor/Icons/DebugLogGameActionIcon.png")] public DebugLogGameActionType actionType = DebugLogGameActionType.String; [IconFoldoutGroup("@name"), ShowIf("@actionType == DebugLogGameActionType.String")] public string message = "Debug Log Message"; [IconFoldoutGroup("@name"), ShowIf("@actionType == DebugLogGameActionType.Variable")] [ValueDropdown("possibleVariables", IsUniqueList = false)] public string variable; [IconFoldoutGroup("@name")] public DebugLogGameActionlogType logType = DebugLogGameActionlogType.Log; private IEnumerable possibleVariables = new ValueDropdownList(); public DebugLogGameAction(GameActionsController controller) : base(controller) { this.controller = controller; className = this.GetType().AssemblyQualifiedName; } public override void Start() { if(actionType == DebugLogGameActionType.String) LogMessage(message, logType); else if(actionType == DebugLogGameActionType.Variable) { string value = GetVariable(variable); if(value != "") LogMessage(value, logType); } _done = true; _started = true; } public override void Update() { } public override void Restart() { _done = false; _started = false; } private void GetCompatibleVariablenames() { if(_controller == null) return; if(_controller.context == null) return; possibleVariables = _controller.context.variables //.Where(x => x.data is GameActionContextString) .Select(x => new ValueDropdownItem(x.name, x.name)); } public static DebugLogGameAction JSONToDebugLogGameAction(string data) { return JsonUtility.FromJson(data); } private DebugLogGameAction Duplicate(GameActionsController controller = null) { DebugLogGameAction action = new DebugLogGameAction(controller == null ? this.controller : controller); action.controller = controller; action.actionType = actionType; action.message = message; action.variable = variable; return action; } private string GetVariable(string title) { if(controller == null) return ""; if(controller.context == null) return ""; for (int i = 0; i < controller.context.variables.Count; i++) { if(controller.context.variables[i].name == title) { string value = controller.context.variables[i].GetValue(""); if(value != "") return value; } } return ""; } private void LogMessage(string value, DebugLogGameActionlogType logType) { switch (logType) { case DebugLogGameActionlogType.Log: Debug.Log(value); break; case DebugLogGameActionlogType.Warning: Debug.LogWarning(value); break; case DebugLogGameActionlogType.Error: Debug.LogError(value); break; default: break; } } } }