StoryDialogView.cs 19 KB

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