StoryDialogView.cs 17 KB

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