StoryDialogView.cs 17 KB

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