StoryDialogView.cs 20 KB

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