using EasyTalk.Controller; using EasyTalk.Nodes.Core; using System; using System.Collections.Generic; using UnityEngine; namespace EasyTalk.Nodes.Logic { /// /// A node which performs a boolean logic operation, outputs the boolean result, and continues down the dialogue flow path attributed to that result. /// [Serializable] public class LogicNode : Node, DialogueFlowNode, ConditionalNode, FunctionalNode { /// /// The logic operation to use. /// [SerializeField] private LogicOperation logicOperation = LogicOperation.AND; /// /// Creates a new LogicNode. /// public LogicNode() { this.name = "BOOL LOGIC"; this.nodeType = NodeType.BOOL_LOGIC; } /// /// Gets or sets the logic operation to use. /// public LogicOperation LogicMode { get { return this.logicOperation; } set { this.logicOperation = value; } } /// /// Gets or sets the logic operation to use (from a string equivalent to a LogicOperation toString() value). /// [SerializeField] public string LogicOperationString { get { return this.logicOperation.ToString(); } set { Enum.TryParse(value, out logicOperation); } } /// /// Returns the logic operation used by the node. /// /// The logic operation used by this node. public LogicOperation GetLogicMode() { return this.logicOperation; } /// /// Sets the logic operation used by the node. /// /// The logic operation to use. public void SetLogicMode(LogicOperation operation) { this.logicOperation = operation; } /// public bool DetermineAndStoreValue(NodeHandler nodeHandler, Dictionary nodeValues, GameObject convoOwner = null) { bool valueA = true; bool valueB = true; if (Inputs[1].AttachedIDs.Count > 0) { valueA = (bool)nodeValues[Inputs[1].AttachedIDs[0]]; } if (Inputs[2].AttachedIDs.Count > 0) { valueB = (bool)nodeValues[Inputs[2].AttachedIDs[0]]; } NodeConnection boolOutput = FindOutputOfType(InputOutputType.BOOL); switch (this.logicOperation) { case LogicOperation.AND: nodeValues.TryAdd(boolOutput.ID, valueA && valueB); nodeValues.TryAdd(this.ID, valueA && valueB); break; case LogicOperation.OR: nodeValues.TryAdd(boolOutput.ID, valueA || valueB); nodeValues.TryAdd(this.ID, valueA || valueB); break; case LogicOperation.NOT: nodeValues.TryAdd(boolOutput.ID, !valueA); nodeValues.TryAdd(this.ID, !valueA); break; case LogicOperation.XOR: nodeValues.TryAdd(boolOutput.ID, valueA ^ valueB); nodeValues.TryAdd(this.ID, valueA ^ valueB); break; case LogicOperation.IS_TRUE: nodeValues.TryAdd(boolOutput.ID, valueA); nodeValues.TryAdd(this.ID, valueA); break; case LogicOperation.IS_FALSE: nodeValues.TryAdd(boolOutput.ID, !valueA); nodeValues.TryAdd(this.ID, !valueA); break; } return true; } /// public List GetDependencyOutputIDs() { return FindDependencyOutputIDs(); } /// public NodeConnection GetFalseOutput() { return FindOutputOfType(InputOutputType.DIALOGUE_FALSE_FLOW); } /// public NodeConnection GetFlowInput() { return FindFlowInputs()[0]; } /// public NodeConnection GetFlowOutput() { NodeConnection conn = GetTrueOutput(); if (conn.AttachedIDs.Count > 0) { return conn; } return GetFalseOutput(); } /// public NodeConnection GetOutput(bool value) { if (value) { return GetTrueOutput(); } return GetFalseOutput(); } /// public NodeConnection GetTrueOutput() { return FindOutputOfType(InputOutputType.DIALOGUE_TRUE_FLOW); } /// public bool HasDependencies() { return FindDependencyOutputIDs().Count > 0; } /// /// An enumeration defining the types of boolean logic operations which can be performed by a logic node. /// [Serializable] public enum LogicOperation { AND, OR, XOR, NOT, IS_TRUE, IS_FALSE } } }