StoryDialogView.cs 18 KB

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