summaryrefslogtreecommitdiff
path: root/Client/Assets/Scripts/XEditor/XBehaviorTree.cs
blob: 30b79ad8af6bde9220c9eab971796cfb3ab11e38 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#if UNITY_EDITOR
using XUtliPoolLib;
using UnityEngine;
using BehaviorDesigner.Runtime;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using BehaviorDesigner.Runtime.Tasks;
using UnityEditor;

public class XBehaviorTree : MonoBehaviour, IXBehaviorTree
{
    public static Dictionary<string, BehaviorSource> BehaviorCache = new Dictionary<string, BehaviorSource>();

    public bool Deprecated
    {
        get;
        set;
    }

    void Awake()
    {
        _behavior_tree = gameObject.AddComponent<BehaviorTree>();
    }
    public static BehaviorSource DeepClone(BehaviorSource source) //深clone
    {
        MemoryStream stream = new MemoryStream();
        BinaryFormatter formatter = new BinaryFormatter();
        formatter.Serialize(stream, source);
        stream.Position = 0;
        return formatter.Deserialize(stream) as BehaviorSource;
    }

    public void SetManual(bool enable)
    {
        if (enable)
            BehaviorManager.instance.UpdateInterval = UpdateIntervalType.Manual;
    }
    public void TickBehaviorTree()
    {
        if (_behavior_tree != null)
            BehaviorManager.instance.Tick(_behavior_tree);
    }

    public bool SetBehaviorTree(string name)
    {
        if (string.IsNullOrEmpty(name))
            return false;

        string location = "Assets/Behavior Designer/Behavior Data/" + name + ".asset";
        ExternalBehaviorTree ebt = UnityEditor.AssetDatabase.LoadAssetAtPath(location, typeof(ExternalBehaviorTree)) as ExternalBehaviorTree;
        _behavior_tree.ExternalBehavior = ebt;
        _behavior_tree.RestartWhenComplete = true;

        return true;
    }

    public void EnableBehaviorTree(bool enable)
    {
        if (_behavior_tree == null)
            return;

        if (enable)
            _behavior_tree.EnableBehavior();
        else
            _behavior_tree.DisableBehavior();

    }

    public void OnStartSkill(uint skillid)
    {
        //List<XAIIsCastingSkill> castingSkills = _behavior_tree.FindTasks<XAIIsCastingSkill>();

        //for (int i = 0; i < castingSkills.Count; i++)
        //    castingSkills[i].IsCastingSkill = true;

        //List<XAITryCastSkill> trySkills = _behavior_tree.FindTasks<XAITryCastSkill>();
        //for (int i = 0; i < trySkills.Count; i++)
        //    trySkills[i].CastSkillId = skillid;
    }

    public void OnEndSkill(uint skillid)
    {
        //List<XAIIsCastingSkill> castingSkills = _behavior_tree.FindTasks<XAIIsCastingSkill>();

        //for (int i = 0; i < castingSkills.Count; i++)
        //    castingSkills[i].IsCastingSkill = false;
    }

    public void OnSkillHurt()
    {
        //List<XAIIsCastingSkill> isCasting = _behavior_tree.FindTasks<XAIIsCastingSkill>();

        //for (int i = 0; i < isCasting.Count; i++)
        //    isCasting[i].IsHurt = true;
    }

    public float OnGetHeartRate()
    {
        if (_behavior_tree == null)
            return 0;

        SharedFloat innerRate = (SharedFloat)_behavior_tree.GetVariable("heartrate");
        return innerRate.Value;
    }

    public void SetTarget(Transform target)
    {
        SharedTransform innertarget = (SharedTransform)_behavior_tree.GetVariable("target");

        if (innertarget != null)
        {
            innertarget.SetValue(target);
        }
    }

    public void SetNavPoint(Transform navpoint)
    {
        SharedTransform innernav = (SharedTransform)_behavior_tree.GetVariable("navtarget");

        if (innernav != null)
        {
            innernav.SetValue(navpoint);
        }
    }

    public void SetVariable(string name, object value)
    {
        if (_behavior_tree == null)
            return;

        SharedVariable sharedvar = _behavior_tree.GetVariable(name);

        if (sharedvar != null)
        {
            sharedvar.SetValue(value);
        }
    }

    public void SetIntByName(string name, int value)
    {
        if (_behavior_tree == null)
            return;

        SharedVariable sharedvar = _behavior_tree.GetVariable(name);

        if (sharedvar != null)
        {
            sharedvar.SetValue(value);
        }
    }
    public void SetFloatByName(string name, float value)
    {
        if (_behavior_tree == null)
            return;

        SharedVariable sharedvar = _behavior_tree.GetVariable(name);

        if (sharedvar != null)
        {
            sharedvar.SetValue(value);
        }
    }
    public void SetBoolByName(string name, bool value)
    {
        if (_behavior_tree == null)
            return;

        SharedVariable sharedvar = _behavior_tree.GetVariable(name);

        if (sharedvar != null)
        {
            sharedvar.SetValue(value);
        }
    }
    public void SetVector3ByName(string name, Vector3 value)
    {
        if (_behavior_tree == null)
            return;

        SharedVariable sharedvar = _behavior_tree.GetVariable(name);

        if (sharedvar != null)
        {
            sharedvar.SetValue(value);
        }
    }
    public void SetTransformByName(string name, Transform value)
    {
        if (_behavior_tree == null)
            return;

        SharedVariable sharedvar = _behavior_tree.GetVariable(name);

        if (sharedvar != null)
        {
            sharedvar.SetValue(value);
        }
    }
    public void SetXGameObjectByName(string name, XGameObject value)
    {
        if(_behavior_tree == null)
            return;

        SharedVariable sharedvar = _behavior_tree.GetVariable(name);

        if (sharedvar != null)
        {
            sharedvar.SetValue(value == null ? null : value.Find(""));
        }
    }

    public static AudioClip GetAudioClip(string path)
    {
#if UNITY_EDITOR
        AudioClip clip = UnityEditor.AssetDatabase.LoadAssetAtPath(path, typeof(AudioClip)) as AudioClip;
        return clip;
#else
        return null;
#endif
    }

    private BehaviorTree _behavior_tree = null;
}
#endif