StoryDialogView.cs 19 KB

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