StoryDialogView.cs 21 KB

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