StoryDialogView.cs 20 KB

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