StoryDialogView.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  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 = EquipDataCache.cacher.dialogSpeed;
  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. //
  102. StoryChapterCfg chapterCfg = StoryChapterCfgArray.Instance.GetCfg(MainStoryDataManager.currentChapterCfgId);
  103. StoryLevelCfg storyLevelCfg = StoryLevelCfgArray.Instance.GetCfgsBytypeAndsubTypeAndchapterId(chapterCfg.type, chapterCfg.subType, chapterCfg.id)[0];
  104. if (LauncherConfig.netType == LauncherConfig.EnumNetType.TEMP && MainStoryDataManager.currentChapterCfgId == 10001 && storyLevelCfg.order == 1 && !InstanceZonesDataManager.CheckLevelPass(storyLevelCfg.id))
  105. {
  106. // 临时设置都可以跳过对话
  107. skipable = false;
  108. }
  109. else
  110. {
  111. skipable = true;
  112. }
  113. _ui.m_btnSkip.visible = skipable;
  114. ShowNextStep(_stroyStartID);
  115. _ui.m_c1.selectedIndex = 0;
  116. _ui.m_btnAutoPlay.selected = false;
  117. if (_stroyStartID == MainStoryDataManager.priorId)
  118. {
  119. _ui.m_c1.selectedIndex = 1;
  120. // _ui.m_btnAutoPlay.selected = true;
  121. OnClickBtnAutoPlay();
  122. _speedAutoPlay = 1;
  123. EquipDataCache.cacher.dialogSpeed = _speedAutoPlay;
  124. }
  125. }
  126. protected override void OnHide()
  127. {
  128. base.OnHide();
  129. Timers.inst.Remove(UpdateLetters);
  130. Timers.inst.Remove(UpdateShake);
  131. Timers.inst.Remove(OnScreenEffectComplete);
  132. Timers.inst.Remove(ShowNextWords);
  133. ScreenBlackController.Instance.HideBlack();
  134. StopAutoPlay();
  135. if (_sceneObject != null)
  136. {
  137. GameObject.Destroy(_sceneObject);
  138. _sceneObject = null;
  139. }
  140. MusicManager.Instance.Play(ResPathUtil.GetMusicPath(ConstMusicName.DEFAULT));
  141. _onCompleteStoryDialogCall = null;
  142. _onCompleteStoryDialogCallParam = null;
  143. }
  144. private void OnClickBtnBack()
  145. {
  146. // this.Hide();
  147. ViewManager.GoBackFrom(typeof(StoryDialogView).FullName);
  148. // Over(false);
  149. }
  150. private void OnClickBtnNext()
  151. {
  152. // StopAutoPlay();
  153. ShowNextWords();
  154. }
  155. private void OnClickBtnLookBack()
  156. {
  157. if (_ui.m_btnSkip.enabled)
  158. {
  159. ViewManager.Show(ViewName.STORY_LOOK_BACK_VIEW, _stroyStartID);
  160. }
  161. else
  162. {
  163. ViewManager.Show(ViewName.STORY_LOOK_BACK_VIEW, _dialogListLookBack);
  164. }
  165. }
  166. private void OnBtnSkip()
  167. {
  168. Over(true);
  169. }
  170. private void OnClickListItem(EventContext context)
  171. {
  172. UI_ListDialogItem dialogItem = UI_ListDialogItem.Proxy(context.data as GObject);
  173. _dialogListLookBack.Add(dialogItem.m_txtContent.text);
  174. string stepID = (string)dialogItem.target.data;
  175. if (stepID == null)
  176. {
  177. stepID = "0";
  178. }
  179. UI_ListDialogItem.ProxyEnd();
  180. OnStepComplete(stepID);
  181. }
  182. private void OnClickBtnSpeedUp()
  183. {
  184. //如果没有自动播放先开始自动播放
  185. if (!_autoPlay)
  186. {
  187. _ui.m_btnAutoPlay.selected = true;
  188. OnClickBtnAutoPlay();
  189. }
  190. _speedAutoPlay = _speedAutoPlay * 2;
  191. if (_speedAutoPlay > GameConst.MAX_SPEED_AUTO_PLAY)
  192. {
  193. _speedAutoPlay = 1;
  194. }
  195. EquipDataCache.cacher.dialogSpeed = _speedAutoPlay;
  196. UpdateSpeedUpBtn();
  197. }
  198. private void OnClickBtnAutoPlay()
  199. {
  200. _autoPlay = _ui.m_btnAutoPlay.selected;
  201. if (_autoPlay)
  202. {
  203. ShowNextWords();
  204. }
  205. }
  206. private void InitStepListById(string dialogID)
  207. {
  208. var temp = StoryDialogCfgArray.Instance.GetCfgsByid(dialogID);
  209. _stepListToRead = new List<StoryDialogCfg>(temp);
  210. }
  211. private void ShowNextStep(string nextStepId)
  212. {
  213. if (nextStepId != null)
  214. {
  215. InitStepListById(nextStepId);
  216. }
  217. if (_stepListToRead != null && _stepListToRead.Count > 0)
  218. {
  219. StoryDialogCfg storyDialogCfg = (StoryDialogCfg)_stepListToRead[0];
  220. _stepListToRead.RemoveAt(0);
  221. InitStepContent(storyDialogCfg);
  222. }
  223. else
  224. {
  225. Over();
  226. }
  227. }
  228. private void OnStepComplete(string nextStepId = null)
  229. {
  230. _nextStepId = nextStepId;
  231. _ui.m_dialogText.target.visible = false;
  232. _ui.m_dialogName.target.visible = false;
  233. _ui.m_dialogHead.target.visible = false;
  234. float delay = 0;
  235. //屏幕效果
  236. if (_currentStepCfg != null)
  237. {
  238. if (_currentStepCfg.blackScreenDur > 0)
  239. {
  240. delay = _currentStepCfg.blackScreenDur;
  241. ScreenBlackController.Instance.ShowBlack(delay, this.viewCom, 0);
  242. }
  243. else if (_currentStepCfg.blankScreenDur > 0)
  244. {
  245. delay = _currentStepCfg.blankScreenDur;
  246. UpdatePic("0");
  247. }
  248. }
  249. if (delay > 0)
  250. {
  251. //转换成秒
  252. delay = delay / 1000f;
  253. Timers.inst.Add(delay, 1, OnScreenEffectComplete);
  254. }
  255. else
  256. {
  257. OnScreenEffectComplete();
  258. }
  259. }
  260. private void OnScreenEffectComplete(object param = null)
  261. {
  262. if (_nextStepId == "0")
  263. {
  264. Over();
  265. }
  266. else
  267. {
  268. ShowNextStep(_nextStepId);
  269. }
  270. }
  271. private void InitStepContent(StoryDialogCfg storyDialogCfg)
  272. {
  273. _currentStepCfg = storyDialogCfg;
  274. UpdateMusic(storyDialogCfg.musicRes);
  275. UpdateBg(storyDialogCfg.bgRes);
  276. UpdatePic(storyDialogCfg.picRes);
  277. PlayEffect(storyDialogCfg.effectInfoArr);
  278. PlayShake(storyDialogCfg.shakeInfoArr);
  279. string content = storyDialogCfg.content;
  280. content = storyDialogCfg.content.Replace("self", RoleDataManager.roleName);
  281. if (content.IndexOf("//") >= 0)
  282. {
  283. showList(content);
  284. }
  285. else
  286. {
  287. ShowDialog(storyDialogCfg);
  288. }
  289. }
  290. private void showList(string content)
  291. {
  292. StopAutoPlay();
  293. _ui.m_btnAutoPlay.enabled = false;
  294. _wordTextField = null;
  295. _ui.m_dialogText.target.visible = false;
  296. _ui.m_dialogName.target.visible = false;
  297. _ui.m_dialogHead.target.visible = false;
  298. _ui.m_list.visible = true;
  299. _ui.m_list.RemoveChildrenToPool();
  300. string[] list = Regex.Split(content, "//");
  301. _ui.m_list.itemRenderer = (int index, GObject item) =>
  302. {
  303. string itemInfo = list[index];
  304. string[] itemInfoList = Regex.Split(itemInfo, "=");
  305. UI_ListDialogItem dialogItem = UI_ListDialogItem.Proxy(item);
  306. dialogItem.m_txtContent.text = itemInfoList[0];
  307. dialogItem.target.data = itemInfoList.Length > 1 ? itemInfoList[1] : null;
  308. UI_ListDialogItem.ProxyEnd();
  309. };
  310. _ui.m_list.numItems = list.Length;
  311. }
  312. private void ShowDialog(StoryDialogCfg storyDialogCfg)
  313. {
  314. _ui.m_btnAutoPlay.enabled = true;
  315. _ui.m_list.visible = false;
  316. var content = storyDialogCfg.content.Replace("self", RoleDataManager.roleName);
  317. string words = content;
  318. string roleName = storyDialogCfg.name;
  319. string headRes = storyDialogCfg.head;
  320. string headAniRes = storyDialogCfg.headAni;
  321. if (roleName == "self")
  322. {
  323. roleName = RoleDataManager.roleName;
  324. }
  325. //回顾
  326. if (roleName != null)
  327. {
  328. _dialogListLookBack.Add("[color=#FDA2B1]" + roleName + "[/color]");
  329. }
  330. if (!string.IsNullOrEmpty(headAniRes))
  331. {
  332. _ui.m_dialogText.target.visible = false;
  333. _ui.m_dialogName.target.visible = false;
  334. _ui.m_dialogHead.target.visible = true;
  335. _ui.m_dialogHead.m_txtName.text = roleName;
  336. _ui.m_dialogHead.m_comphead.m_head.visible = false;
  337. _ui.m_dialogHead.m_comphead.m_holder.visible = true;
  338. string resPath = ResPathUtil.GetViewEffectPath("ui_nzbq", headAniRes);
  339. SceneController.AddObjectToView(_npcHead, _npcWrapper, _ui.m_dialogHead.m_comphead.m_holder, resPath, out _npcHead, out _npcWrapper);
  340. // _npcHead.transform.localScale = new Vector3(-100, 100, 100);
  341. _wordTextField = _ui.m_dialogHead.m_txtContent;
  342. _arrow = _ui.m_dialogHead.m_iconNext;
  343. }
  344. else if (!string.IsNullOrEmpty(headRes))
  345. {
  346. _ui.m_dialogText.target.visible = false;
  347. _ui.m_dialogName.target.visible = false;
  348. _ui.m_dialogHead.target.visible = true;
  349. _ui.m_dialogHead.m_txtName.text = roleName;
  350. _ui.m_dialogHead.m_comphead.m_head.visible = true;
  351. _ui.m_dialogHead.m_comphead.m_holder.visible = false;
  352. _ui.m_dialogHead.m_comphead.m_head.url = ResPathUtil.GetNpcHeadPath(headRes);
  353. _wordTextField = _ui.m_dialogHead.m_txtContent;
  354. _arrow = _ui.m_dialogHead.m_iconNext;
  355. }
  356. else if (!string.IsNullOrEmpty(roleName))
  357. {
  358. _ui.m_dialogText.target.visible = false;
  359. _ui.m_dialogName.target.visible = true;
  360. _ui.m_dialogHead.target.visible = false;
  361. _ui.m_dialogName.m_txtName.text = roleName;
  362. _wordTextField = _ui.m_dialogName.m_txtContent;
  363. _arrow = _ui.m_dialogName.m_iconNext;
  364. }
  365. else
  366. {
  367. _ui.m_dialogText.target.visible = true;
  368. _ui.m_dialogName.target.visible = false;
  369. _ui.m_dialogHead.target.visible = false;
  370. _wordTextField = _ui.m_dialogText.m_txtContent;
  371. _arrow = _ui.m_dialogText.m_iconNext;
  372. }
  373. _wordList = Regex.Split(words, "&&");
  374. _wordIndex = 0;
  375. ShowNextDialog();
  376. }
  377. private void ShowNextDialog()
  378. {
  379. if (_wordList != null && _wordList.Length > _wordIndex)
  380. {
  381. string itemInfo = _wordList[_wordIndex];
  382. string[] itemInfoList = Regex.Split(itemInfo, "=");
  383. _currentWords = itemInfoList[0];
  384. if (itemInfoList.Length > 1)
  385. {
  386. _wordTextField.data = itemInfoList[1];
  387. }
  388. else
  389. {
  390. _wordTextField.data = null;
  391. }
  392. StartShowLetters();
  393. }
  394. else
  395. {
  396. OnStepComplete();
  397. }
  398. }
  399. private void ShowCurrentWords()
  400. {
  401. _arrow.target.visible = true;
  402. Timers.inst.Remove(UpdateLetters);
  403. _wordTextField.text = _currentWords;
  404. _dialogListLookBack.Add(_currentWords);
  405. _isShowLetters = false;
  406. _wordIndex++;
  407. if (_autoPlay)
  408. {
  409. Timers.inst.Add(GameConst.NEXT_WORDS_INTERVAL_MAX / _speedAutoPlay, 1, ShowNextWords);
  410. }
  411. }
  412. private void ShowNextWords(object param = null)
  413. {
  414. if (_wordTextField != null)
  415. {
  416. if (_isShowLetters)
  417. {
  418. ShowCurrentWords();
  419. }
  420. else
  421. {
  422. string stepID = (string)_wordTextField.data;
  423. if (stepID != null)
  424. {
  425. OnStepComplete(stepID);
  426. }
  427. else
  428. {
  429. ShowNextDialog();
  430. }
  431. }
  432. }
  433. }
  434. private void StartShowLetters()
  435. {
  436. _isShowLetters = true;
  437. _arrow.target.visible = false;
  438. _wordTextField.verticalAlign = VertAlignType.Top;
  439. _wordTextField.text = "";
  440. ArrayList letters = StoryUtil.GetLettersList(_currentWords);
  441. Timers.inst.Add(GameConst.LETTERS_INTERVAL_MAX / _speedAutoPlay, 0, UpdateLetters, letters);
  442. }
  443. private void UpdateLetters(object param)
  444. {
  445. ArrayList letters = (ArrayList)param;
  446. if (letters == null || letters.Count <= 0)
  447. {
  448. ShowCurrentWords();
  449. }
  450. else
  451. {
  452. string letter = (string)letters[0];
  453. letters.RemoveAt(0);
  454. _wordTextField.text = _wordTextField.text + letter;
  455. }
  456. }
  457. private void UpdateBg(string value)
  458. {
  459. if (value.Length > 0)
  460. {
  461. SceneController.UpdateDialogBg(value, _sceneObject);
  462. }
  463. }
  464. private void UpdatePic(string value)
  465. {
  466. if (value.Length > 0)
  467. {
  468. SceneController.UpdateDialogPic(value, _sceneObject);
  469. }
  470. }
  471. private void UpdateMusic(string value)
  472. {
  473. if (value.Length > 0)
  474. {
  475. if (value == "0")
  476. {
  477. MusicManager.Instance.Stop();
  478. }
  479. else
  480. {
  481. MusicManager.Instance.Play(ResPathUtil.GetMusicPath(value, "mp3"));
  482. }
  483. }
  484. }
  485. private void PlayEffect(string[] infos)
  486. {
  487. }
  488. private void PlayShake(int[] shakeInfoArr)
  489. {
  490. if (shakeInfoArr != null && shakeInfoArr.Length > 0)
  491. {
  492. Vector3 position = _sceneObject.transform.position;
  493. position.x = (float)shakeInfoArr[0] / GameConst.PIXELS_PER_UNITY_UNIT;
  494. position.y = (float)shakeInfoArr[1] / GameConst.PIXELS_PER_UNITY_UNIT;
  495. _sceneObject.transform.position = position;
  496. float attenuationX = (float)shakeInfoArr[2] / GameConst.PIXELS_PER_UNITY_UNIT;
  497. float attenuationY = (float)shakeInfoArr[3] / GameConst.PIXELS_PER_UNITY_UNIT;
  498. float interval = (float)shakeInfoArr[4] / 1000;
  499. float duration = (float)shakeInfoArr[5] / 1000;
  500. int repeat = Mathf.RoundToInt(duration / interval);
  501. Timers.inst.Add(interval, 0, UpdateShake, new float[] { attenuationX, attenuationY });
  502. }
  503. }
  504. private void UpdateShake(object param)
  505. {
  506. float[] attenuations = param as float[];
  507. float attenuationX = attenuations[0];
  508. float attenuationY = attenuations[1];
  509. Vector3 position = _sceneObject.transform.position;
  510. bool done = false;
  511. bool doneX = false;
  512. float absX = Mathf.Abs(position.x);
  513. if (absX > attenuationX)
  514. {
  515. int dir = (int)(position.x / absX);
  516. position.x = Mathf.Abs(position.x) - attenuationX;
  517. position.x *= -1 * dir;
  518. }
  519. else
  520. {
  521. doneX = true;
  522. position.x = 0;
  523. }
  524. bool doneY = false;
  525. float absY = Mathf.Abs(position.y);
  526. if (absY > attenuationY)
  527. {
  528. int dir = (int)(position.y / absY);
  529. position.y = Mathf.Abs(position.y) - attenuationY;
  530. position.y *= -1 * dir;
  531. }
  532. else
  533. {
  534. doneY = true;
  535. position.y = 0;
  536. }
  537. done = doneX && doneY;
  538. _sceneObject.transform.position = position;
  539. if (done)
  540. {
  541. Timers.inst.Remove(UpdateShake);
  542. }
  543. }
  544. private void Over(bool isSkip = false)
  545. {
  546. if (_onCompleteStoryDialogCall != null)
  547. {
  548. _onCompleteStoryDialogCall(isSkip, _onCompleteStoryDialogCallParam);
  549. }
  550. }
  551. private void UpdateSpeedUpBtn()
  552. {
  553. if (_speedAutoPlay > 1)
  554. {
  555. _ui.m_btnSpeedUp.text = "x" + _speedAutoPlay;
  556. }
  557. else
  558. {
  559. _ui.m_btnSpeedUp.text = "";
  560. }
  561. }
  562. private void StopAutoPlay()
  563. {
  564. _autoPlay = false;
  565. _ui.m_btnAutoPlay.selected = false;
  566. Timers.inst.Remove(ShowNextWords);
  567. }
  568. }
  569. }