using EasyTalk.Controller; using EasyTalk.Nodes.Core; using System; using System.Collections.Generic; using UnityEngine; namespace EasyTalk.Nodes.Logic { /// /// A node which performs a mathematical operation and sends the result to a numeric value output. /// [Serializable] public class MathNode : Node, DialogueFlowNode, FunctionalNode { /// /// The mathematical operation to perform. /// [SerializeField] private MathOperation operation; /// /// The first value to use in the math operation. /// [SerializeField] private string valueA; /// /// The second value to use in the math operation. /// [SerializeField] private string valueB; /// /// Creates a new MathNode. /// public MathNode() { this.name = "MATH"; this.nodeType = NodeType.MATH; } /// /// Gets or sets the math operation used by the node (from a string equivalent to a MathOperation toString() value). /// public string MathOperationString { get { return this.operation.ToString(); } set { Enum.TryParse(value, out operation); } } /// /// Gets or sets the math operation used by the node. /// public MathOperation MathOperation { get { return this.operation; } set { operation = value; } } /// /// Gets or sets the first value to use in the math operation. /// public string ValueA { get { return this.valueA; } set { this.valueA = value; } } /// /// Gets or sets the second value to use in the math operation. /// public string ValueB { get { return this.valueB; } set { this.valueB = value; } } /// public bool DetermineAndStoreValue(NodeHandler nodeHandler, Dictionary nodeValues, GameObject convoOwner = null) { float floatA = 0.0f; float floatB = 0.0f; if (Inputs[1].AttachedIDs.Count > 0) { object valueObj = nodeValues[Inputs[1].AttachedIDs[0]]; floatA = Convert.ToSingle(valueObj); } else { string valueAAString = nodeHandler.ReplaceVariablesInString(valueA); float.TryParse(valueAAString, out floatA); } if (Inputs[2].AttachedIDs.Count > 0) { object valueObj = nodeValues[Inputs[2].AttachedIDs[0]]; floatB = Convert.ToSingle(valueObj); } else { string valueBString = nodeHandler.ReplaceVariablesInString(valueB); float.TryParse(valueBString, out floatB); } NodeConnection resultConn = FindOutputOfType(InputOutputType.NUMBER); switch(operation) { case MathOperation.ADD: nodeValues.TryAdd(resultConn.ID, floatA + floatB); break; case MathOperation.SUBTRACT: nodeValues.TryAdd(resultConn.ID, floatA - floatB); break; case MathOperation.MULTIPLY: nodeValues.TryAdd(resultConn.ID, floatA * floatB); break; case MathOperation.DIVIDE: nodeValues.TryAdd(resultConn.ID, floatA / floatB); break; } return true; } /// public List GetDependencyOutputIDs() { return FindDependencyOutputIDs(); } /// public NodeConnection GetFlowInput() { return FindFlowInputs()[0]; } /// public NodeConnection GetFlowOutput() { return FindFlowOutputs()[0]; } /// public bool HasDependencies() { return FindDependencyOutputIDs().Count > 0; } } /// /// An enumeration defining the types of mathematical operations which can be performed by a math node. /// public enum MathOperation { ADD, SUBTRACT, MULTIPLY, DIVIDE } }