summaryrefslogtreecommitdiff
path: root/Client/Assets/Behavior Designer/Runtime/Basic Tasks/Math/IntOperator.cs
blob: 4805508ebd4355f646887630bf309a4644b64822 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using UnityEngine;

namespace BehaviorDesigner.Runtime.Tasks.Basic.Math
{
    [TaskCategory("Basic/Math")]
    [TaskDescription("Performs a math operation on two integers: Add, Subtract, Multiply, Divide, Min, or Max.")]
    public class IntOperator : Action
    {
        public enum Operation
        {
            Add,
            Subtract,
            Multiply,
            Divide,
            Min,
            Max
        }

        [Tooltip("The operation to perform")]
        public Operation operation;
        [Tooltip("The first integer")]
        public SharedInt integer1;
        [Tooltip("The second integer")]
        public SharedInt integer2;
        [RequiredField]
        [Tooltip("The variable to store the result")]
        public SharedInt storeResult;

        public override TaskStatus OnUpdate()
        {
            switch (operation) {
                case Operation.Add:
                    storeResult.Value = integer1.Value + integer2.Value;
                    break;
                case Operation.Subtract:
                    storeResult.Value = integer1.Value - integer2.Value;
                    break;
                case Operation.Multiply:
                    storeResult.Value = integer1.Value * integer2.Value;
                    break;
                case Operation.Divide:
                    storeResult.Value = integer1.Value / integer2.Value;
                    break;
                case Operation.Min:
                    storeResult.Value = Mathf.Min(integer1.Value, integer2.Value);
                    break;
                case Operation.Max:
                    storeResult.Value = Mathf.Max(integer1.Value, integer2.Value);
                    break;
            }
            return TaskStatus.Success;
        }

        public override void OnReset()
        {
            operation = Operation.Add;
            integer1.Value = 0;
            integer2.Value = 0;
            storeResult.Value = 0;
        }
    }
}