StoryDialogView.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. using FairyGUI;
  2. using UnityEngine;
  3. using UI.Main;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Text.RegularExpressions;
  7. namespace GFGGame
  8. {
  9. public delegate void OnCompleteStoryDialogCall(bool isSkip, object param);
  10. public class StoryDialogView : BaseView
  11. {
  12. private UI_StoryDialogUI _ui;
  13. private UI_CompArrow _arrow;
  14. private GameObject _sceneObject;
  15. private GameObject _scenePrefab;
  16. private GameObject _npcHead;
  17. private GoWrapper _npcWrapper;
  18. private GTextField _wordTextField;
  19. //剧情完成回调
  20. private OnCompleteStoryDialogCall _onCompleteStoryDialogCall;
  21. private object _onCompleteStoryDialogCallParam;
  22. //回顾
  23. private List<string> _dialogListLookBack;
  24. //自动播放
  25. private int _speedAutoPlay = 1;
  26. private bool _autoPlay = false;
  27. //剧情状态
  28. private List<StoryDialogCfg> _stepListToRead;
  29. private StoryDialogCfg _currentStepCfg;
  30. private string _nextStepId;
  31. private string[] _wordList;
  32. private int _wordIndex = 0;
  33. private bool _isShowLetters;
  34. private string _currentWords;
  35. private string _stroyStartID;
  36. public override void Dispose()
  37. {
  38. base.Dispose();
  39. if (_sceneObject != null)
  40. {
  41. GameObject.Destroy(_sceneObject);
  42. _sceneObject = null;
  43. }
  44. _wordTextField = null;
  45. _arrow = null;
  46. _isShowLetters = false;
  47. SceneController.DestroyObjectFromView(_npcHead, _npcWrapper);
  48. Timers.inst.Remove(UpdateLetters);
  49. // if (_ui != null)
  50. // {
  51. // _ui.Dispose();
  52. // _ui = null;
  53. // }
  54. }
  55. protected override void Init()
  56. {
  57. base.Init();
  58. packageName = UI_StoryDialogUI.PACKAGE_NAME;
  59. _ui = UI_StoryDialogUI.Create();
  60. viewCom = _ui.target;
  61. isfullScreen = true;
  62. _scenePrefab = GFGAsset.Load<GameObject>(ResPathUtil.GetPrefabPath("SceneStoryDialog"));
  63. }
  64. protected override void OnInit()
  65. {
  66. base.OnInit();
  67. _ui.m_dialogText.target.visible = false;
  68. _ui.m_dialogName.target.visible = false;
  69. _ui.m_dialogHead.target.visible = false;
  70. _ui.m_list.visible = false;
  71. _ui.m_btnNext.width = GRoot.inst.width;
  72. _ui.m_btnNext.height = GRoot.inst.height;
  73. _ui.m_btnNext.AddRelation(GRoot.inst, RelationType.Size);
  74. _ui.m_btnBack.onClick.Add(OnClickBtnBack);
  75. _ui.m_btnNext.onClick.Add(OnClickBtnNext);
  76. _ui.m_btnLookBack.onClick.Add(OnClickBtnLookBack);
  77. _ui.m_btnSkip.onClick.Add(OnBtnSkip);
  78. _ui.m_list.onClickItem.Add(OnClickListItem);
  79. _ui.m_btnSpeedUp.onClick.Add(OnClickBtnSpeedUp);
  80. _ui.m_btnAutoPlay.onClick.Add(OnClickBtnAutoPlay);
  81. }
  82. protected override void OnShown()
  83. {
  84. base.OnShown();
  85. if (_sceneObject == null)
  86. {
  87. _sceneObject = GameObject.Instantiate(_scenePrefab);
  88. }
  89. _speedAutoPlay = 1;
  90. _autoPlay = false;
  91. UpdateSpeedUpBtn();
  92. _dialogListLookBack = new List<string>();
  93. object[] datas = viewData as object[];
  94. _stroyStartID = (string)datas[0];
  95. bool skipable = (bool)datas[1];
  96. _onCompleteStoryDialogCall = (OnCompleteStoryDialogCall)datas[2];
  97. if (datas.Length > 3)
  98. {
  99. _onCompleteStoryDialogCallParam = datas[3];
  100. }
  101. if (LauncherConfig.netType == LauncherConfig.EnumNetType.LOCAL)
  102. {
  103. //临时设置都可以跳过对话
  104. skipable = true;
  105. }
  106. _ui.m_btnSkip.enabled = skipable;
  107. ShowNextStep(_stroyStartID);
  108. _ui.m_c1.selectedIndex = 0;
  109. _ui.m_btnAutoPlay.selected = false;
  110. if (_stroyStartID == MainStoryDataManager.priorId)
  111. {
  112. _ui.m_c1.selectedIndex = 1;
  113. // _ui.m_btnAutoPlay.selected = true;
  114. OnClickBtnAutoPlay();
  115. }
  116. }
  117. protected override void OnHide()
  118. {
  119. base.OnHide();
  120. Timers.inst.Remove(UpdateLetters);
  121. Timers.inst.Remove(UpdateShake);
  122. Timers.inst.Remove(OnScreenEffectComplete);
  123. Timers.inst.Remove(ShowNextWords);
  124. ScreenBlackController.Instance.HideBlack();
  125. StopAutoPlay();
  126. if (_sceneObject != null)
  127. {
  128. GameObject.Destroy(_sceneObject);
  129. _sceneObject = null;
  130. }
  131. MusicManager.Instance.Play(ResPathUtil.GetMusicPath(ConstMusicName.DEFAULT));
  132. _onCompleteStoryDialogCall = null;
  133. _onCompleteStoryDialogCallParam = null;
  134. }
  135. private void OnClickBtnBack()
  136. {
  137. // this.Hide();
  138. ViewManager.GoBackFrom(typeof(StoryDialogView).FullName);
  139. // Over(false);
  140. }
  141. private void OnClickBtnNext()
  142. {
  143. // StopAutoPlay();
  144. ShowNextWords();
  145. }
  146. private void OnClickBtnLookBack()
  147. {
  148. if (_ui.m_btnSkip.enabled)
  149. {
  150. ViewManager.Show(ViewName.STORY_LOOK_BACK_VIEW, _stroyStartID);
  151. }
  152. else
  153. {
  154. ViewManager.Show(ViewName.STORY_LOOK_BACK_VIEW, _dialogListLookBack);
  155. }
  156. }
  157. private void OnBtnSkip()
  158. {
  159. Over(true);
  160. }
  161. private void OnClickListItem(EventContext context)
  162. {
  163. UI_ListDialogItem dialogItem = UI_ListDialogItem.Proxy(context.data as GObject);
  164. _dialogListLookBack.Add(dialogItem.m_txtContent.text);
  165. string stepID = (string)dialogItem.target.data;
  166. if (stepID == null)
  167. {
  168. stepID = "0";
  169. }
  170. UI_ListDialogItem.ProxyEnd();
  171. OnStepComplete(stepID);
  172. }
  173. private void OnClickBtnSpeedUp()
  174. {
  175. //如果没有自动播放先开始自动播放
  176. if (!_autoPlay)
  177. {
  178. _ui.m_btnAutoPlay.selected = true;
  179. OnClickBtnAutoPlay();
  180. }
  181. _speedAutoPlay = _speedAutoPlay * 2;
  182. if (_speedAutoPlay > GameConst.MAX_SPEED_AUTO_PLAY)
  183. {
  184. _speedAutoPlay = 1;
  185. }
  186. UpdateSpeedUpBtn();
  187. }
  188. private void OnClickBtnAutoPlay()
  189. {
  190. _autoPlay = _ui.m_btnAutoPlay.selected;
  191. if (_autoPlay)
  192. {
  193. ShowNextWords();
  194. }
  195. }
  196. private void InitStepListById(string dialogID)
  197. {
  198. var temp = StoryDialogCfgArray.Instance.GetCfgs(dialogID);
  199. _stepListToRead = new List<StoryDialogCfg>(temp);
  200. }
  201. private void ShowNextStep(string nextStepId)
  202. {
  203. if (nextStepId != null)
  204. {
  205. InitStepListById(nextStepId);
  206. }
  207. if (_stepListToRead != null && _stepListToRead.Count > 0)
  208. {
  209. StoryDialogCfg storyDialogCfg = (StoryDialogCfg)_stepListToRead[0];
  210. _stepListToRead.RemoveAt(0);
  211. InitStepContent(storyDialogCfg);
  212. }
  213. else
  214. {
  215. Over();
  216. }
  217. }
  218. private void OnStepComplete(string nextStepId = null)
  219. {
  220. _nextStepId = nextStepId;
  221. _ui.m_dialogText.target.visible = false;
  222. _ui.m_dialogName.target.visible = false;
  223. _ui.m_dialogHead.target.visible = false;
  224. float delay = 0;
  225. //屏幕效果
  226. if (_currentStepCfg != null)
  227. {
  228. if (_currentStepCfg.blackScreenDur > 0)
  229. {
  230. delay = _currentStepCfg.blackScreenDur;
  231. ScreenBlackController.Instance.ShowBlack(delay, this.viewCom, 0);
  232. }
  233. else if (_currentStepCfg.blankScreenDur > 0)
  234. {
  235. delay = _currentStepCfg.blankScreenDur;
  236. UpdatePic("0");
  237. }
  238. }
  239. if (delay > 0)
  240. {
  241. //转换成秒
  242. delay = delay / 1000f;
  243. Timers.inst.Add(delay, 1, OnScreenEffectComplete);
  244. }
  245. else
  246. {
  247. OnScreenEffectComplete();
  248. }
  249. }
  250. private void OnScreenEffectComplete(object param = null)
  251. {
  252. if (_nextStepId == "0")
  253. {
  254. Over();
  255. }
  256. else
  257. {
  258. ShowNextStep(_nextStepId);
  259. }
  260. }
  261. private void InitStepContent(StoryDialogCfg storyDialogCfg)
  262. {
  263. _currentStepCfg = storyDialogCfg;
  264. UpdateMusic(storyDialogCfg.musicRes);
  265. UpdateBg(storyDialogCfg.bgRes);
  266. UpdatePic(storyDialogCfg.picRes);
  267. PlayEffect(storyDialogCfg.effectInfoArr);
  268. PlayShake(storyDialogCfg.shakeInfoArr);
  269. string content = storyDialogCfg.content;
  270. content = storyDialogCfg.content.Replace("self", RoleDataManager.roleName);
  271. if (content.IndexOf("//") >= 0)
  272. {
  273. showList(content);
  274. }
  275. else
  276. {
  277. ShowDialog(storyDialogCfg);
  278. }
  279. }
  280. private void showList(string content)
  281. {
  282. StopAutoPlay();
  283. _ui.m_btnAutoPlay.enabled = false;
  284. _wordTextField = null;
  285. _ui.m_dialogText.target.visible = false;
  286. _ui.m_dialogName.target.visible = false;
  287. _ui.m_dialogHead.target.visible = false;
  288. _ui.m_list.visible = true;
  289. _ui.m_list.RemoveChildrenToPool();
  290. string[] list = Regex.Split(content, "//");
  291. _ui.m_list.itemRenderer = (int index, GObject item) =>
  292. {
  293. string itemInfo = list[index];
  294. string[] itemInfoList = Regex.Split(itemInfo, "=");
  295. UI_ListDialogItem dialogItem = UI_ListDialogItem.Proxy(item);
  296. dialogItem.m_txtContent.text = itemInfoList[0];
  297. dialogItem.target.data = itemInfoList.Length > 1 ? itemInfoList[1] : null;
  298. UI_ListDialogItem.ProxyEnd();
  299. };
  300. _ui.m_list.numItems = list.Length;
  301. }
  302. private void ShowDialog(StoryDialogCfg storyDialogCfg)
  303. {
  304. _ui.m_btnAutoPlay.enabled = true;
  305. _ui.m_list.visible = false;
  306. var content = storyDialogCfg.content.Replace("self", RoleDataManager.roleName);
  307. string words = content;
  308. string roleName = storyDialogCfg.name;
  309. string headRes = storyDialogCfg.head;
  310. string headAniRes = storyDialogCfg.headAni;
  311. if (roleName == "self")
  312. {
  313. roleName = RoleDataManager.roleName;
  314. }
  315. //回顾
  316. if (roleName != null)
  317. {
  318. _dialogListLookBack.Add("[color=#FDA2B1]" + roleName + "[/color]");
  319. }
  320. if (!string.IsNullOrEmpty(headAniRes))
  321. {
  322. _ui.m_dialogText.target.visible = false;
  323. _ui.m_dialogName.target.visible = false;
  324. _ui.m_dialogHead.target.visible = true;
  325. _ui.m_dialogHead.m_txtName.text = roleName;
  326. _ui.m_dialogHead.m_comphead.m_head.visible = false;
  327. _ui.m_dialogHead.m_comphead.m_holder.visible = true;
  328. string resPath = ResPathUtil.GetViewEffectPath("ui_nzbq", headAniRes);
  329. SceneController.AddObjectToView(_npcHead, _npcWrapper, _ui.m_dialogHead.m_comphead.m_holder, resPath, out _npcHead, out _npcWrapper);
  330. // _npcHead.transform.localScale = new Vector3(-100, 100, 100);
  331. _wordTextField = _ui.m_dialogHead.m_txtContent;
  332. _arrow = _ui.m_dialogHead.m_iconNext;
  333. }
  334. else if (!string.IsNullOrEmpty(headRes))
  335. {
  336. _ui.m_dialogText.target.visible = false;
  337. _ui.m_dialogName.target.visible = false;
  338. _ui.m_dialogHead.target.visible = true;
  339. _ui.m_dialogHead.m_txtName.text = roleName;
  340. _ui.m_dialogHead.m_comphead.m_head.visible = true;
  341. _ui.m_dialogHead.m_comphead.m_holder.visible = false;
  342. _ui.m_dialogHead.m_comphead.m_head.url = ResPathUtil.GetNpcHeadPath(headRes);
  343. _wordTextField = _ui.m_dialogHead.m_txtContent;
  344. _arrow = _ui.m_dialogHead.m_iconNext;
  345. }
  346. else if (!string.IsNullOrEmpty(roleName))
  347. {
  348. _ui.m_dialogText.target.visible = false;
  349. _ui.m_dialogName.target.visible = true;
  350. _ui.m_dialogHead.target.visible = false;
  351. _ui.m_dialogName.m_txtName.text = roleName;
  352. _wordTextField = _ui.m_dialogName.m_txtContent;
  353. _arrow = _ui.m_dialogName.m_iconNext;
  354. }
  355. else
  356. {
  357. _ui.m_dialogText.target.visible = true;
  358. _ui.m_dialogName.target.visible = false;
  359. _ui.m_dialogHead.target.visible = false;
  360. _wordTextField = _ui.m_dialogText.m_txtContent;
  361. _arrow = _ui.m_dialogText.m_iconNext;
  362. }
  363. _wordList = Regex.Split(words, "&&");
  364. _wordIndex = 0;
  365. ShowNextDialog();
  366. }
  367. private void ShowNextDialog()
  368. {
  369. if (_wordList != null && _wordList.Length > _wordIndex)
  370. {
  371. string itemInfo = _wordList[_wordIndex];
  372. string[] itemInfoList = Regex.Split(itemInfo, "=");
  373. _currentWords = itemInfoList[0];
  374. if (itemInfoList.Length > 1)
  375. {
  376. _wordTextField.data = itemInfoList[1];
  377. }
  378. else
  379. {
  380. _wordTextField.data = null;
  381. }
  382. StartShowLetters();
  383. }
  384. else
  385. {
  386. OnStepComplete();
  387. }
  388. }
  389. private void ShowCurrentWords()
  390. {
  391. _arrow.target.visible = true;
  392. Timers.inst.Remove(UpdateLetters);
  393. _wordTextField.text = _currentWords;
  394. _dialogListLookBack.Add(_currentWords);
  395. _isShowLetters = false;
  396. _wordIndex++;
  397. if (_autoPlay)
  398. {
  399. Timers.inst.Add(GameConst.NEXT_WORDS_INTERVAL_MAX / _speedAutoPlay, 1, ShowNextWords);
  400. }
  401. }
  402. private void ShowNextWords(object param = null)
  403. {
  404. if (_wordTextField != null)
  405. {
  406. if (_isShowLetters)
  407. {
  408. ShowCurrentWords();
  409. }
  410. else
  411. {
  412. string stepID = (string)_wordTextField.data;
  413. if (stepID != null)
  414. {
  415. OnStepComplete(stepID);
  416. }
  417. else
  418. {
  419. ShowNextDialog();
  420. }
  421. }
  422. }
  423. }
  424. private void StartShowLetters()
  425. {
  426. _isShowLetters = true;
  427. _arrow.target.visible = false;
  428. _wordTextField.verticalAlign = VertAlignType.Top;
  429. _wordTextField.text = "";
  430. ArrayList letters = StoryUtil.GetLettersList(_currentWords);
  431. Timers.inst.Add(GameConst.LETTERS_INTERVAL_MAX / _speedAutoPlay, 0, UpdateLetters, letters);
  432. }
  433. private void UpdateLetters(object param)
  434. {
  435. ArrayList letters = (ArrayList)param;
  436. if (letters == null || letters.Count <= 0)
  437. {
  438. ShowCurrentWords();
  439. }
  440. else
  441. {
  442. string letter = (string)letters[0];
  443. letters.RemoveAt(0);
  444. _wordTextField.text = _wordTextField.text + letter;
  445. }
  446. }
  447. private void UpdateBg(string value)
  448. {
  449. if (value.Length > 0)
  450. {
  451. SceneController.UpdateDialogBg(value, _sceneObject);
  452. }
  453. }
  454. private void UpdatePic(string value)
  455. {
  456. if (value.Length > 0)
  457. {
  458. SceneController.UpdateDialogPic(value, _sceneObject);
  459. }
  460. }
  461. private void UpdateMusic(string value)
  462. {
  463. if (value.Length > 0)
  464. {
  465. if (value == "0")
  466. {
  467. MusicManager.Instance.Stop();
  468. }
  469. else
  470. {
  471. MusicManager.Instance.Play(ResPathUtil.GetMusicPath(value, "mp3"));
  472. }
  473. }
  474. }
  475. private void PlayEffect(string[] infos)
  476. {
  477. }
  478. private void PlayShake(int[] shakeInfoArr)
  479. {
  480. if (shakeInfoArr != null && shakeInfoArr.Length > 0)
  481. {
  482. Vector3 position = _sceneObject.transform.position;
  483. position.x = (float)shakeInfoArr[0] / GameConst.PIXELS_PER_UNITY_UNIT;
  484. position.y = (float)shakeInfoArr[1] / GameConst.PIXELS_PER_UNITY_UNIT;
  485. _sceneObject.transform.position = position;
  486. float attenuationX = (float)shakeInfoArr[2] / GameConst.PIXELS_PER_UNITY_UNIT;
  487. float attenuationY = (float)shakeInfoArr[3] / GameConst.PIXELS_PER_UNITY_UNIT;
  488. float interval = (float)shakeInfoArr[4] / 1000;
  489. float duration = (float)shakeInfoArr[5] / 1000;
  490. int repeat = Mathf.RoundToInt(duration / interval);
  491. Timers.inst.Add(interval, 0, UpdateShake, new float[] { attenuationX, attenuationY });
  492. }
  493. }
  494. private void UpdateShake(object param)
  495. {
  496. float[] attenuations = param as float[];
  497. float attenuationX = attenuations[0];
  498. float attenuationY = attenuations[1];
  499. Vector3 position = _sceneObject.transform.position;
  500. bool done = false;
  501. bool doneX = false;
  502. float absX = Mathf.Abs(position.x);
  503. if (absX > attenuationX)
  504. {
  505. int dir = (int)(position.x / absX);
  506. position.x = Mathf.Abs(position.x) - attenuationX;
  507. position.x *= -1 * dir;
  508. }
  509. else
  510. {
  511. doneX = true;
  512. position.x = 0;
  513. }
  514. bool doneY = false;
  515. float absY = Mathf.Abs(position.y);
  516. if (absY > attenuationY)
  517. {
  518. int dir = (int)(position.y / absY);
  519. position.y = Mathf.Abs(position.y) - attenuationY;
  520. position.y *= -1 * dir;
  521. }
  522. else
  523. {
  524. doneY = true;
  525. position.y = 0;
  526. }
  527. done = doneX && doneY;
  528. _sceneObject.transform.position = position;
  529. if (done)
  530. {
  531. Timers.inst.Remove(UpdateShake);
  532. }
  533. }
  534. private void Over(bool isSkip = false)
  535. {
  536. if (_onCompleteStoryDialogCall != null)
  537. {
  538. _onCompleteStoryDialogCall(isSkip, _onCompleteStoryDialogCallParam);
  539. }
  540. }
  541. private void UpdateSpeedUpBtn()
  542. {
  543. if (_speedAutoPlay > 1)
  544. {
  545. _ui.m_btnSpeedUp.text = "x" + _speedAutoPlay;
  546. }
  547. else
  548. {
  549. _ui.m_btnSpeedUp.text = "";
  550. }
  551. }
  552. private void StopAutoPlay()
  553. {
  554. _autoPlay = false;
  555. _ui.m_btnAutoPlay.selected = false;
  556. Timers.inst.Remove(ShowNextWords);
  557. }
  558. }
  559. }