StoryDialogView.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  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(true);
  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. string itemInfo = list[index];
  261. string[] itemInfoList = Regex.Split(itemInfo, "=");
  262. UI_ListDialogItem dialogItem = UI_ListDialogItem.Proxy(item);
  263. dialogItem.m_txtContent.text = itemInfoList[0];
  264. dialogItem.target.data = itemInfoList.Length > 1?itemInfoList[1]: null;
  265. };
  266. _ui.m_list.numItems = list.Length;
  267. }
  268. private void ShowDialog(StoryDialogCfg storyDialogCfg)
  269. {
  270. _ui.m_btnAutoPlay.enabled = true;
  271. _ui.m_list.visible = false;
  272. var content = storyDialogCfg.content.Replace("self", RoleDataManager.roleName);
  273. string words = content;
  274. string roleName = storyDialogCfg.name;
  275. string headRes = storyDialogCfg.head;
  276. if (roleName == "self")
  277. {
  278. roleName = RoleDataManager.roleName;
  279. }
  280. //回顾
  281. if(roleName != null)
  282. {
  283. _dialogListLookBack.Add("[color=#FDA2B1]" + roleName + "[/color]");
  284. }
  285. if (!string.IsNullOrEmpty(headRes))
  286. {
  287. _ui.m_dialogText.target.visible = false;
  288. _ui.m_dialogName.target.visible = false;
  289. _ui.m_dialogHead.target.visible = true;
  290. _ui.m_dialogHead.m_txtName.text = roleName;
  291. _ui.m_dialogHead.m_comphead.m_head.url = ResPathUtil.GetNpcHeadPath(headRes);
  292. _wordTextField = _ui.m_dialogHead.m_txtContent;
  293. _arrow = _ui.m_dialogHead.m_iconNext;
  294. }
  295. else if(!string.IsNullOrEmpty(roleName))
  296. {
  297. _ui.m_dialogText.target.visible = false;
  298. _ui.m_dialogName.target.visible = true;
  299. _ui.m_dialogHead.target.visible = false;
  300. _ui.m_dialogName.m_txtName.text = roleName;
  301. _wordTextField = _ui.m_dialogName.m_txtContent;
  302. _arrow = _ui.m_dialogName.m_iconNext;
  303. }
  304. else
  305. {
  306. _ui.m_dialogText.target.visible = true;
  307. _ui.m_dialogName.target.visible = false;
  308. _ui.m_dialogHead.target.visible = false;
  309. _wordTextField = _ui.m_dialogText.m_txtContent;
  310. _arrow = _ui.m_dialogText.m_iconNext;
  311. }
  312. _wordList = Regex.Split(words, "&&");
  313. _wordIndex = 0;
  314. ShowNextDialog();
  315. }
  316. private void ShowNextDialog()
  317. {
  318. if(_wordList != null && _wordList.Length > _wordIndex)
  319. {
  320. string itemInfo = _wordList[_wordIndex];
  321. string[] itemInfoList = Regex.Split(itemInfo, "=");
  322. _currentWords = itemInfoList[0];
  323. StartShowLetters();
  324. if(itemInfoList.Length > 1)
  325. {
  326. _wordTextField.data = itemInfoList[1];
  327. }
  328. else
  329. {
  330. _wordTextField.data = null;
  331. }
  332. }
  333. else
  334. {
  335. OnStepComplete();
  336. }
  337. }
  338. private void ShowCurrentWords()
  339. {
  340. _arrow.target.visible = true;
  341. Timers.inst.Remove(UpdateLetters);
  342. _wordTextField.text = _currentWords;
  343. _dialogListLookBack.Add(_currentWords);
  344. _isShowLetters = false;
  345. _wordIndex++;
  346. if(_autoPlay)
  347. {
  348. Timers.inst.Add(GameConst.NEXT_WORDS_INTERVAL_MAX/_speedAutoPlay, 1, ShowNextWords);
  349. }
  350. }
  351. private void ShowNextWords(object param = null)
  352. {
  353. if(_wordTextField != null)
  354. {
  355. if(_isShowLetters)
  356. {
  357. ShowCurrentWords();
  358. }
  359. else
  360. {
  361. string stepID = (string)_wordTextField.data;
  362. if(stepID != null)
  363. {
  364. OnStepComplete(stepID);
  365. }
  366. else
  367. {
  368. ShowNextDialog();
  369. }
  370. }
  371. }
  372. }
  373. private void StartShowLetters()
  374. {
  375. _isShowLetters = true;
  376. _arrow.target.visible = false;
  377. _wordTextField.verticalAlign = VertAlignType.Top;
  378. _wordTextField.text = "";
  379. ArrayList letters = StoryUtil.GetLettersList(_currentWords);
  380. Timers.inst.Add(GameConst.LETTERS_INTERVAL_MAX/_speedAutoPlay, 0, UpdateLetters, letters);
  381. }
  382. private void UpdateLetters(object param)
  383. {
  384. ArrayList letters = (ArrayList)param;
  385. if(letters == null || letters.Count <= 0)
  386. {
  387. ShowCurrentWords();
  388. }
  389. else
  390. {
  391. string letter = (string)letters[0];
  392. letters.RemoveAt(0);
  393. _wordTextField.text = _wordTextField.text + letter;
  394. }
  395. }
  396. private void UpdateBg(string value)
  397. {
  398. if(value.Length > 0)
  399. {
  400. SceneController.UpdateDialogBg(value, _sceneObject);
  401. }
  402. }
  403. private void UpdatePic(string value)
  404. {
  405. if(value.Length > 0)
  406. {
  407. SceneController.UpdateDialogPic(value, _sceneObject);
  408. }
  409. }
  410. private void UpdateMusic(string value)
  411. {
  412. if(value.Length > 0)
  413. {
  414. if (value == "0")
  415. {
  416. MusicManager.Instance.Stop();
  417. }
  418. else
  419. {
  420. MusicManager.Instance.Play(ResPathUtil.GetMusicPath(value, "mp3"));
  421. }
  422. }
  423. }
  424. private void PlayEffect(string[] infos)
  425. {
  426. }
  427. private void PlayShake(int[] shakeInfoArr)
  428. {
  429. if(shakeInfoArr != null && shakeInfoArr.Length > 0)
  430. {
  431. Vector3 position = _sceneObject.transform.position;
  432. position.x = (float)shakeInfoArr[0]/GameConst.PIXELS_PER_UNITY_UNIT;
  433. position.y = (float)shakeInfoArr[1]/ GameConst.PIXELS_PER_UNITY_UNIT;
  434. _sceneObject.transform.position = position;
  435. float attenuationX = (float)shakeInfoArr[2] / GameConst.PIXELS_PER_UNITY_UNIT;
  436. float attenuationY = (float)shakeInfoArr[3] / GameConst.PIXELS_PER_UNITY_UNIT;
  437. float interval = (float)shakeInfoArr[4] / 1000;
  438. float duration = (float)shakeInfoArr[5] / 1000;
  439. int repeat = Mathf.RoundToInt(duration / interval);
  440. Timers.inst.Add(interval, 0, UpdateShake, new float[] { attenuationX, attenuationY });
  441. }
  442. }
  443. private void UpdateShake(object param)
  444. {
  445. float[] attenuations = param as float[];
  446. float attenuationX = attenuations[0];
  447. float attenuationY = attenuations[1];
  448. Vector3 position = _sceneObject.transform.position;
  449. bool done = false;
  450. bool doneX = false;
  451. float absX = Mathf.Abs(position.x);
  452. if (absX > attenuationX)
  453. {
  454. int dir = (int)(position.x / absX);
  455. position.x = Mathf.Abs(position.x) - attenuationX;
  456. position.x *= -1 * dir;
  457. }
  458. else
  459. {
  460. doneX = true;
  461. position.x = 0;
  462. }
  463. bool doneY = false;
  464. float absY = Mathf.Abs(position.y);
  465. if (absY > attenuationY)
  466. {
  467. int dir = (int)(position.y / absY);
  468. position.y = Mathf.Abs(position.y) - attenuationY;
  469. position.y *= -1 * dir;
  470. }
  471. else
  472. {
  473. doneY = true;
  474. position.y = 0;
  475. }
  476. done = doneX && doneY;
  477. _sceneObject.transform.position = position;
  478. if(done)
  479. {
  480. Timers.inst.Remove(UpdateShake);
  481. }
  482. }
  483. private void Over(bool isSkip = false)
  484. {
  485. if(_onCompleteStoryDialogCall != null)
  486. {
  487. _onCompleteStoryDialogCall(isSkip, _onCompleteStoryDialogCallParam);
  488. }
  489. }
  490. private void UpdateSpeedUpBtn()
  491. {
  492. if (_speedAutoPlay > 1)
  493. {
  494. _ui.m_btnSpeedUp.text = "x" + _speedAutoPlay;
  495. }
  496. else
  497. {
  498. _ui.m_btnSpeedUp.text = "";
  499. }
  500. }
  501. private void StopAutoPlay()
  502. {
  503. _autoPlay = false;
  504. _ui.m_btnAutoPlay.selected = false;
  505. Timers.inst.Remove(ShowNextWords);
  506. }
  507. }
  508. }