FTimeline.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. using UnityEngine;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using System.Security;
  6. using Mono.Xml;
  7. using System.IO;
  8. namespace Flux
  9. {
  10. /**
  11. * @brief A timeline is an object that holds tracks that pertain to
  12. * a specific object, aka Owner. When evaluated this timeline will
  13. * run events which most of the times will directly affect the Owner.
  14. * @sa FSquence, FTrack, FEvent
  15. */
  16. public class FTimeline : FObject//, ISerializationCallbackReceiver
  17. {
  18. // To which Sequence this timeline belongs to
  19. // [SerializeField]
  20. // private FSequence _sequence;
  21. [SerializeField]
  22. private FContainer _container = null;
  23. public FContainer Container { get { return _container; } }
  24. // Which object is the owner of this timeline
  25. [SerializeField]
  26. private Transform _owner = null;
  27. // What's the path to the _owner, used for serialization purposes in prefabs
  28. [SerializeField]
  29. private string _ownerPath = null;
  30. public string OwnerPath { get { return _ownerPath; } }
  31. [HideInInspector]
  32. [SerializeField]
  33. private int _buffId = 0;
  34. public int BuffId
  35. {
  36. get { return _buffId; }
  37. }
  38. [HideInInspector]
  39. [SerializeField]
  40. private string _buffName = "";
  41. public string BuffName
  42. {
  43. get { return _buffName; }
  44. }
  45. [HideInInspector]
  46. [SerializeField]
  47. private int _gender = 3;
  48. public int Gender
  49. {
  50. get { return _gender; }
  51. }
  52. [HideInInspector]
  53. [SerializeField]
  54. private int _jobType;
  55. public int JobType
  56. {
  57. get { return _jobType; }
  58. }
  59. [HideInInspector]
  60. [SerializeField]
  61. private int _jobStage;
  62. public int JobStage
  63. {
  64. get { return _jobStage; }
  65. }
  66. [HideInInspector]
  67. [SerializeField]
  68. private int _jobBranch;
  69. public int JobBranch
  70. {
  71. get { return _jobBranch; }
  72. }
  73. public override FSequence Sequence { get { return _container.Sequence; } }
  74. public override Transform Owner { get { return _owner; } }
  75. public void Awake()
  76. {
  77. if( _owner == null && !string.IsNullOrEmpty(_ownerPath) )
  78. _owner = transform.Find( _ownerPath );
  79. }
  80. /// @brief Sets the owner of this timeline
  81. public void SetOwner( Transform owner ) {
  82. _owner = owner;
  83. if( _owner != null )
  84. name = _owner.name;
  85. OnValidate();
  86. if( Container != null && Sequence.IsInit )
  87. Init();
  88. }
  89. internal void SetContainer( FContainer container )
  90. {
  91. _container = container;
  92. if( _container )
  93. transform.parent = container.transform;
  94. else
  95. transform.parent = null;
  96. OnValidate();
  97. }
  98. // tracks
  99. [SerializeField]
  100. private List<FTrack> _tracks = new List<FTrack>();
  101. /// @brief Get the tracks inside this timeline
  102. public List<FTrack> Tracks { get { return _tracks; } }
  103. public static FTimeline Create( Transform owner )
  104. {
  105. GameObject go = new GameObject(owner.name);
  106. FTimeline timeline = go.AddComponent<FTimeline>();
  107. timeline.SetOwner( owner );
  108. return timeline;
  109. }
  110. public override void Init()
  111. {
  112. if( _owner == null )
  113. Awake();
  114. enabled = Owner != null;
  115. if( !enabled )
  116. return;
  117. for( int i = 0; i != _tracks.Count; ++i )
  118. _tracks[i].Init();
  119. }
  120. public void Pause()
  121. {
  122. for( int i = 0; i != _tracks.Count; ++i )
  123. _tracks[i].Pause();
  124. }
  125. public void Resume()
  126. {
  127. for( int i = 0; i != _tracks.Count; ++i )
  128. _tracks[i].Resume();
  129. }
  130. public override void Stop()
  131. {
  132. for( int i = 0; i != _tracks.Count; ++i )
  133. _tracks[i].Stop();
  134. }
  135. public bool IsEmpty()
  136. {
  137. foreach( FTrack track in _tracks )
  138. {
  139. if( !track.IsEmpty() )
  140. return false;
  141. }
  142. return true;
  143. }
  144. public FTrack Add<T>( FrameRange range ) where T : FEvent
  145. {
  146. FTrack track = FTrack.Create<T>();
  147. Add( track );
  148. FEvent evt = FEvent.Create<T>( range );
  149. track.Add( evt );
  150. evt.SetDefaultValues();
  151. return track;
  152. }
  153. public void Add( FTrack track )
  154. {
  155. int id = _tracks.Count;
  156. _tracks.Add( track );
  157. track.SetTimeline( this );
  158. track.SetId( id );
  159. if( !Sequence.IsStopped )
  160. track.Init();
  161. }
  162. public void Remove( FTrack track )
  163. {
  164. if( _tracks.Remove( track ) )
  165. {
  166. track.SetTimeline( null );
  167. UpdateTrackIds();
  168. }
  169. }
  170. public void UpdateTracks( int frame, float time )
  171. {
  172. for( int i = 0; i != _tracks.Count; ++i )
  173. {
  174. if( !_tracks[i].enabled ) continue;
  175. _tracks[i].UpdateEvents( frame, time );
  176. }
  177. }
  178. public void UpdateTracksEditor( int frame, float time )
  179. {
  180. for( int i = 0; i != _tracks.Count; ++i )
  181. {
  182. if( !_tracks[i].enabled ) continue;
  183. _tracks[i].UpdateEventsEditor( frame, time );
  184. }
  185. }
  186. #if UNITY_EDITOR
  187. public void SaveToXml()
  188. {
  189. string frameEventFileName = string.Format("{0}/Content/Xml/Skill/buff_{1}_{2}_{3}_{4}_{5}.xml", Application.dataPath, _buffId,_gender,_jobType,_jobStage,_jobBranch);
  190. SecurityElement root = new SecurityElement("FrameEvent");
  191. SecurityElement buffNode = new SecurityElement("buff");
  192. buffNode.AddAttribute("id", this._buffId.ToString());
  193. buffNode.AddAttribute("name", this._buffName);
  194. buffNode.AddAttribute("endFrame", Sequence.Length.ToString());
  195. buffNode.AddAttribute("gender", this._gender.ToString());
  196. buffNode.AddAttribute("jobType", this._jobType.ToString());
  197. buffNode.AddAttribute("jobStage", this._jobStage.ToString());
  198. buffNode.AddAttribute("jobBranch", this._jobBranch.ToString());
  199. if(Sequence.CasterActor!=null)
  200. buffNode.AddAttribute("caster", Sequence.CasterActor.name);
  201. if (Sequence.TargetActor != null)
  202. buffNode.AddAttribute("target", Sequence.TargetActor.name);
  203. if(_tracks!=null)
  204. {
  205. for(int idx =0; idx < _tracks.Count; idx++ )
  206. {
  207. FTrack track = _tracks[idx];
  208. if(track.Events!=null)
  209. {
  210. for(int jdx = 0; jdx < track.Events.Count;jdx++)
  211. {
  212. SecurityElement eventNode = track.Events[jdx].SaveToXml();
  213. if(eventNode!=null)
  214. {
  215. buffNode.AddChild(eventNode);
  216. }
  217. }
  218. }
  219. }
  220. }
  221. root.AddChild(buffNode);
  222. SecurityTools.DumpSecurityElementToXml(root, frameEventFileName);
  223. }
  224. public void LoadFromXml(SecurityElement buffNode)
  225. {
  226. if (buffNode == null) return;
  227. int.TryParse(buffNode.Attribute("id"), out _buffId);
  228. _buffName = buffNode.Attribute("name");
  229. int.TryParse(buffNode.Attribute("gender"), out _gender);
  230. int.TryParse(buffNode.Attribute("jobType"), out _jobType);
  231. int.TryParse(buffNode.Attribute("jobStage"), out _jobStage);
  232. int.TryParse(buffNode.Attribute("jobBranch"), out _jobBranch);
  233. if(_gender == 0)
  234. {
  235. _gender = 3;
  236. }
  237. if(buffNode.Children!=null)
  238. {
  239. for (int idx = 0; idx < buffNode.Children.Count; idx++)
  240. {
  241. var eventNode = buffNode.Children[idx] as SecurityElement;
  242. if (eventNode.Tag.CompareTo("event") != 0) continue;
  243. int eventType = 0;
  244. int startFrame = 0, endFrame = 0;
  245. int.TryParse(eventNode.Attribute("type"), out eventType);
  246. int.TryParse(eventNode.Attribute("startFrame"), out startFrame);
  247. int.TryParse(eventNode.Attribute("endFrame"), out endFrame);
  248. if(endFrame == 0)
  249. {
  250. endFrame = startFrame+10;
  251. }
  252. SkillActionFrameEventType type = (SkillActionFrameEventType)eventType;
  253. FEvent evt = null;
  254. FTrack track = null;
  255. var range = new FrameRange(startFrame, endFrame);
  256. if (type == SkillActionFrameEventType.FE_ShakeCamera)
  257. {
  258. evt = FEvent.Create<FCameraShakeEvent>(range);
  259. track = FTrack.Create <FCameraShakeEvent>();
  260. }
  261. else if(type == SkillActionFrameEventType.FE_SWITCH_SKILL)
  262. {
  263. evt = FEvent.Create<FChangeSkillEvent>(range);
  264. track = FTrack.Create<FChangeSkillEvent>();
  265. }
  266. else if(type == SkillActionFrameEventType.FE_Clone)
  267. {
  268. evt = FEvent.Create<FCloneEvent>(range);
  269. track = FTrack.Create<FCloneEvent>();
  270. }
  271. else if(type == SkillActionFrameEventType.FE_Move)
  272. {
  273. evt = FEvent.Create<FMoveEvent>(range);
  274. track = FTrack.Create<FMoveEvent>();
  275. }
  276. else if(type == SkillActionFrameEventType.FE_PlaySkillAnim)
  277. {
  278. evt = FEvent.Create<FPlayAnimEvent>(range);
  279. track = FTrack.Create<FPlayAnimEvent>();
  280. }
  281. else if(type == SkillActionFrameEventType.FE_PlayEffect)
  282. {
  283. evt = FEvent.Create<FPlayEffectEvent>(range);
  284. track = FTrack.Create<FPlayEffectEvent>();
  285. }
  286. else if(type == SkillActionFrameEventType.FE_PlaySound)
  287. {
  288. evt = FEvent.Create<FPlaySoundEvent>(range);
  289. track = FTrack.Create<FPlaySoundEvent>();
  290. }
  291. else if(type == SkillActionFrameEventType.FE_SELECT_TARGET)
  292. {
  293. evt = FEvent.Create<FSelectTargetEvent>(range);
  294. track = FTrack.Create<FSelectTargetEvent>();
  295. }
  296. else if(type == SkillActionFrameEventType.FE_Repeat_Caster)
  297. {
  298. evt = FEvent.Create<FSkillCastingEvent>(range);
  299. track = FTrack.Create<FSkillCastingEvent>();
  300. }
  301. else if(type == SkillActionFrameEventType.FE_Delay_Hurt)
  302. {
  303. evt = FEvent.Create<FSkillDelayHurtEvent>(range);
  304. track = FTrack.Create<FSkillDelayHurtEvent>();
  305. }
  306. else if(type == SkillActionFrameEventType.FE_Hit)
  307. {
  308. evt = FEvent.Create<FSkillHitEvent>(range);
  309. track = FTrack.Create<FSkillHitEvent>();
  310. }
  311. else if(type == SkillActionFrameEventType.FE_Skill_Sing)
  312. {
  313. evt = FEvent.Create<FSkillSingEvent>(range);
  314. track = FTrack.Create<FSkillSingEvent>();
  315. }
  316. else if(type == SkillActionFrameEventType.FE_Summon)
  317. {
  318. evt = FEvent.Create<FSummonEvent>(range);
  319. track = FTrack.Create<FSummonEvent>();
  320. }
  321. else if(type == SkillActionFrameEventType.FE_Buff)
  322. {
  323. evt = FEvent.Create<FTriggerBuffEvent>(range);
  324. track = FTrack.Create<FTriggerBuffEvent>();
  325. }
  326. else if(type == SkillActionFrameEventType.FE_Bullet)
  327. {
  328. evt = FEvent.Create<FTriggerBulletEvent>(range);
  329. track = FTrack.Create<FTriggerBulletEvent>();
  330. }
  331. if(evt!=null && track!=null)
  332. {
  333. track.SetTimeline(this);
  334. track.SetId(idx + 1);
  335. evt.LoadFromXml(eventNode);
  336. track.Add(evt);
  337. evt.SetDefaultValues();
  338. _tracks.Add(track);
  339. }
  340. }
  341. }
  342. if (Owner != null)
  343. {
  344. Owner.name = "buff_" + _buffId + "_" + _gender + "_" + _jobType + "_" + _jobStage + "_" + _jobBranch;
  345. }
  346. }
  347. #endif
  348. public void Rebuild()
  349. {
  350. Transform t = transform;
  351. _tracks.Clear();
  352. for( int i = 0; i != t.childCount; ++i )
  353. {
  354. FTrack track = t.GetChild(i).GetComponent<FTrack>();
  355. if( track )
  356. {
  357. _tracks.Add( track );
  358. track.SetTimeline( this );
  359. track.Rebuild();
  360. }
  361. }
  362. UpdateTrackIds();
  363. }
  364. // updates the track ids
  365. private void UpdateTrackIds()
  366. {
  367. for( int i = 0; i != _tracks.Count; ++i )
  368. _tracks[i].SetId( i );
  369. }
  370. protected virtual void OnValidate()
  371. {
  372. if( _owner != null )
  373. _ownerPath = GetTransformPath( _owner );
  374. }
  375. // helper function to get the transform path of a specific transform t
  376. private string GetTransformPath( Transform t )
  377. {
  378. StringBuilder sb = new StringBuilder(t.name);
  379. if( Container == null || Sequence == null )
  380. return string.Empty;
  381. Transform sequenceTransform = Sequence.transform;
  382. t = t.parent;
  383. while( t != null )
  384. {
  385. if( t == sequenceTransform )
  386. return sb.ToString();
  387. sb.Insert( 0, string.Format("{0}/", t.name) );
  388. t = t.parent;
  389. }
  390. sb.Insert( 0, '/' );
  391. return sb.ToString();
  392. }
  393. }
  394. }