StoryDialogView.cs 19 KB

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