-
Notifications
You must be signed in to change notification settings - Fork 2
/
Player.cs
281 lines (257 loc) · 9.72 KB
/
Player.cs
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using System.Xml;
using System.IO;
//using LitJson;
using Newtonsoft.Json;
public class Player : MonoBehaviour
{
public Dictionary<string, List<SkillBase>> skillsList = new Dictionary<string, List<SkillBase>>();
RuntimeAnimatorController controller;
public AnimatorOverrideController overrideController;
public Transform effectsparent;
AudioSource audioSource;
//Player
Animator anim;
public List<SkillBase> currSkillComponets = new List<SkillBase>();
/* public float animSpeed = 1;
public float AnimSpeed
{
set
{
if (anim != null)
{
anim.speed = value;
}
animSpeed = value;
}
get
{
return animSpeed;
}
}*/
private void Awake()
{
anim = gameObject.GetComponent<Animator>();
}
public void Update()
{
foreach (var item in currSkillComponets)
{
item.Update(Time.time);
}
}
public void play()
{
foreach (var item in currSkillComponets)
{
item.Play();
}
}
private Skill_Anim _Anim;
private Skill_Audio _Aduio;
private Skill_Effects _Effect;
public static Player Init(string path)
{
if (path != null)
{
string str = "Assets/aaa/" + path + ".prefab";
GameObject obj = AssetDatabase.LoadAssetAtPath<GameObject>(str);
if (obj != null)
{
Player player = Instantiate(obj).GetComponent<Player>();
player.controller = Resources.Load<RuntimeAnimatorController>("Player");
player.anim.runtimeAnimatorController = player.overrideController;
player.overrideController = new AnimatorOverrideController();
player.overrideController.runtimeAnimatorController = player.controller;
player.audioSource = player.gameObject.AddComponent<AudioSource>();
player.effectsparent = player.transform.Find("effectsparent");
player.gameObject.name = path;
player.LoadAllSkill();
return player;
}
}
return null;
}
public List<SkillBase> GetSkill(string skillName)
{
if (skillsList.ContainsKey(skillName))
{
return skillsList[skillName];//List<skillbase>
}
return null;
}
public List<SkillBase> AddNewSkill(string newSkillName)
{
if (skillsList.ContainsKey(newSkillName))
{
return skillsList[newSkillName];
}
skillsList.Add(newSkillName, new List<SkillBase>());
return skillsList[newSkillName];
}
public void RevSkill(string newSkillName)
{
if (skillsList.ContainsKey(newSkillName))
{
skillsList.Remove(newSkillName);
}
}
public void Destroy()
{
Destroy(gameObject);
}
void LoadAllSkill()
{
if (File.Exists("Assets/" + gameObject.name + ".txt"))
{
string str = File.ReadAllText("Assets/" + gameObject.name + ".txt");
List<SkillXml> skills = JsonConvert.DeserializeObject<List<SkillXml>>(str);
foreach (var item in skills)
{
skillsList.Add(item.name, new List<SkillBase>());
foreach (var ite in item.skillComponents)
{
foreach (var it in ite.Value)
{
if (ite.Key.Equals("动画"))
{
AnimationClip clip = AssetDatabase.LoadAssetAtPath<AnimationClip>("Assets/GameDate/Anim/" + it.ComponentName + ".anim");;
Skill_Anim _Anim = new Skill_Anim(this);
_Anim.SetAnimClip(clip);
_Anim.SetTrigger(it.trigger);
skillsList[item.name].Add(_Anim);
}
else if (ite.Key.Equals("音效"))
{
AudioClip clip = AssetDatabase.LoadAssetAtPath<AudioClip>("Assets/GameDate/Audio/" + it.ComponentName + ".mp3");
Skill_Audio _Anim = new Skill_Audio(this);
_Anim.SetAnimClip(clip);
_Anim.SetTrigger(it.trigger);
skillsList[item.name].Add(_Anim);
}
else if (ite.Key.Equals("特效"))
{
GameObject clip = AssetDatabase.LoadAssetAtPath<GameObject>("Assets/GameDate/Effect/Skill/" + it.ComponentName + ".prefab");
Skill_Effects _Anim = new Skill_Effects(this);
_Anim.SetGameClip(clip);
_Anim.SetTrigger(it.trigger);
skillsList[item.name].Add(_Anim);
}
}
}
}
}
}
private void SavaAllSkill()
{
List<SkillXml> skills = new List<SkillXml>();
foreach (var item in skillsList)
{
SkillXml skillXml = new SkillXml();
skillXml.name = item.Key;
foreach (var ite in item.Value)
{
if (ite is Skill_Anim)
{
if (!skillXml.skillComponents.ContainsKey("动画"))
{
skillXml.skillComponents.Add("动画", new List<SkillComponentsData>());
}
skillXml.skillComponents["动画"].Add(new SkillComponentsData(ite.name, ite.trigger));
}
else if (ite is Skill_Audio)
{
if (!skillXml.skillComponents.ContainsKey("音效"))
{
skillXml.skillComponents.Add("音效", new List<SkillComponentsData>());
}
skillXml.skillComponents["音效"].Add(new SkillComponentsData(ite.name, ite.trigger));
}
else if (ite is Skill_Effects)
{
if (!skillXml.skillComponents.ContainsKey("特效"))
{
skillXml.skillComponents.Add("特效", new List<SkillComponentsData>());
}
skillXml.skillComponents["特效"].Add(new SkillComponentsData( ite.name , ite.trigger));
}
}
skills.Add(skillXml);
}
string str = JsonConvert.SerializeObject(skills);
File.WriteAllText("Assets/" + gameObject.name + ".txt", str);
}
#region 游戏
public void InitData()
{
overrideController = new AnimatorOverrideController();
controller = Resources.Load<RuntimeAnimatorController>("Player");
overrideController.runtimeAnimatorController = controller;
anim.runtimeAnimatorController = overrideController;
audioSource = gameObject.AddComponent<AudioSource>();
effectsparent = transform.Find("effectsparent");
//gameObject.name = path;
//LoadAllSkill();
}
public void SetData(string skillName)
{
List<SkillXml> skillList = GameData.Instance.GetSkillsByRoleName("Teddy");
//Debug.Log(skillList[0]);
foreach (var item in skillList)
{
if (item.name == skillName)
{
currSkillComponets.Clear();
foreach (var ite in item.skillComponents)
{
foreach (var it in ite.Value)
{
if (ite.Key.Equals("动画"))
{
AnimationClip clip = AssetDatabase.LoadAssetAtPath<AnimationClip>("Assets/GameDate/Anim/" + it.ComponentName + ".anim");
if (_Anim == null) _Anim = new Skill_Anim(this);
_Anim.SetAnimClip(clip);
_Anim.SetTrigger(it.trigger);
currSkillComponets.Add(_Anim);
}
else if (ite.Key.Equals("音效"))
{
AudioClip clip = AssetDatabase.LoadAssetAtPath<AudioClip>("Assets/GameDate/Audio/" + it.ComponentName + ".mp3");
if (_Aduio == null) _Aduio = new Skill_Audio(this);
_Aduio.SetAnimClip(clip);
_Aduio.SetTrigger(it.trigger);
currSkillComponets.Add(_Aduio);
}
else if (ite.Key.Equals("特效"))
{
GameObject clip = AssetDatabase.LoadAssetAtPath<GameObject>("Assets/GameDate/Effect/Skill/" + it.ComponentName + ".prefab");
if (_Effect == null) _Effect = new Skill_Effects(this);
_Effect.SetGameClip(clip);
_Effect.SetTrigger(it.trigger);
currSkillComponets.Add(_Effect);
}
}
}
}
}
}
#endregion
}
public class SkillXml
{
public string name;
public Dictionary<string, List<SkillComponentsData>> skillComponents = new Dictionary<string, List<SkillComponentsData>>();
}
public class SkillComponentsData
{
public string ComponentName;
public string trigger;
public SkillComponentsData(string cn,string tri)
{
ComponentName = cn;
trigger = tri;
}
}