StoryDialogView.cs 18 KB

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