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