StoryDialogView.cs 20 KB

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