StoryDialogView.cs 20 KB

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