BattleOutput.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. using UnityEngine;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. public enum OutputType
  8. {
  9. BeginFighting = 1, //开始战斗
  10. EndFighting = 2, //结束战斗
  11. Dead = 3, //角色死亡
  12. Damage = 4, //技能伤害
  13. Heal = 5, //治疗
  14. Shield = 6, //护盾
  15. Disperse = 7, //驱散
  16. Clean = 8, //净化
  17. Break = 9, //打断
  18. Vertigo = 10, //眩晕
  19. }
  20. public struct OutputInfo
  21. {
  22. public OutputType type;
  23. public string timeStr;
  24. public string casterName;
  25. public string targetName;
  26. public string skillName;
  27. public object val;
  28. public OutputInfo(OutputType type)
  29. {
  30. this.type = type;
  31. timeStr = DateTime.Now.ToLongTimeString();
  32. casterName = null;
  33. targetName = null;
  34. skillName = null;
  35. val = null;
  36. }
  37. }
  38. /// <summary>
  39. /// 超链接信息类
  40. /// </summary>
  41. public class HrefInfo
  42. {
  43. public string color;
  44. public string text;
  45. public HrefInfo(string clr,string content)
  46. {
  47. color = clr;
  48. text = content;
  49. }
  50. }
  51. public class BattleOutput
  52. {
  53. const int MaxNum = 200;
  54. private List<string> mOutputList = new List<string>();
  55. public List<string> OutputList
  56. {
  57. get { return mOutputList; }
  58. }
  59. public void Begin()
  60. {
  61. ConvertLogToStr(OutputType.BeginFighting);
  62. }
  63. public void End()
  64. {
  65. ConvertLogToStr(OutputType.EndFighting);
  66. }
  67. public void Output(OutputType type, Fighter caster, Fighter target = null, string skillName = null, object val = null)
  68. {
  69. ConvertLogToStr(type, caster, target, skillName, val);
  70. }
  71. private void ConvertLogToStr(OutputType type, Fighter caster = null, Fighter target = null, string skillName = null, object val = null)
  72. {
  73. string timeStr = DateTime.Now.ToLongTimeString();
  74. string str = "";
  75. string casterName = caster != null ? I18N.T(caster.Name) : "";
  76. string targetName = target != null ? I18N.T(target.Name) : "";
  77. string sName = skillName != null ? I18N.SetLanguageValue("BattleOutput_SkillName", skillName) : "";
  78. string valStr = val != null ? val.ToString() : "";
  79. bool team = caster != null ? caster.TeamSide == 0 : false;
  80. switch (type)
  81. {
  82. case OutputType.BeginFighting:
  83. str = I18N.SetLanguageValue("BattleOutput_001", timeStr);
  84. break;
  85. case OutputType.EndFighting:
  86. str = I18N.SetLanguageValue("BattleOutput_002", timeStr);
  87. break;
  88. case OutputType.Dead:
  89. if(team)
  90. str = I18N.SetLanguageValue("BattleOutput_003", timeStr, casterName, sName, targetName, valStr);
  91. else
  92. str = I18N.SetLanguageValue("BattleOutput_011", timeStr, casterName, sName, targetName, valStr);
  93. break;
  94. case OutputType.Damage:
  95. if(team)
  96. str = I18N.SetLanguageValue("BattleOutput_004", timeStr, casterName, sName, targetName, valStr);
  97. else
  98. str = I18N.SetLanguageValue("BattleOutput_012", timeStr, casterName, sName, targetName, valStr);
  99. break;
  100. case OutputType.Heal:
  101. if(team)
  102. str = I18N.SetLanguageValue("BattleOutput_005", timeStr, casterName, sName, targetName, valStr);
  103. else
  104. str = I18N.SetLanguageValue("BattleOutput_013", timeStr, casterName, sName, targetName, valStr);
  105. break;
  106. case OutputType.Shield:
  107. if(team)
  108. str = I18N.SetLanguageValue("BattleOutput_006", timeStr, casterName, sName, targetName, valStr);
  109. else
  110. str = I18N.SetLanguageValue("BattleOutput_014", timeStr, casterName, sName, targetName, valStr);
  111. break;
  112. case OutputType.Disperse:
  113. if(team)
  114. str = I18N.SetLanguageValue("BattleOutput_007", timeStr, casterName, sName, targetName, valStr);
  115. else
  116. str = I18N.SetLanguageValue("BattleOutput_015", timeStr, casterName, sName, targetName, valStr);
  117. break;
  118. case OutputType.Clean:
  119. if(team)
  120. str = I18N.SetLanguageValue("BattleOutput_008", timeStr, casterName, sName, targetName, valStr);
  121. else
  122. str = I18N.SetLanguageValue("BattleOutput_016", timeStr, casterName, sName, targetName, valStr);
  123. break;
  124. case OutputType.Break:
  125. if(team)
  126. str = I18N.SetLanguageValue("BattleOutput_009", timeStr, casterName, sName, targetName, valStr);
  127. else
  128. str = I18N.SetLanguageValue("BattleOutput_017", timeStr, casterName, sName, targetName, valStr);
  129. break;
  130. case OutputType.Vertigo:
  131. if(team)
  132. str = I18N.SetLanguageValue("BattleOutput_010", timeStr, casterName, sName, targetName, valStr);
  133. else
  134. str = I18N.SetLanguageValue("BattleOutput_018", timeStr, casterName, sName, targetName, valStr);
  135. break;
  136. }
  137. GetOutputText(str);
  138. string tempStr = "";
  139. int lastCnt = 0;
  140. int deltaCnt = 0;
  141. int cursor = 0;
  142. for(int idx = 0; idx < mTextInfos.Count;idx++)
  143. {
  144. tempStr += mTextInfos[idx].text;
  145. int cnt = StringUtil.GetStringByteLength(tempStr);
  146. if(cnt >= 50)
  147. {
  148. cursor = idx;
  149. deltaCnt = 50 - lastCnt;
  150. break;
  151. }else
  152. {
  153. lastCnt = cnt;
  154. }
  155. }
  156. if(deltaCnt > 0)
  157. {
  158. var info = mTextInfos[cursor];
  159. string temp = StringUtil.CutOutString(info.text, deltaCnt);
  160. string temp2 = info.text.Replace(temp, "");
  161. mTextInfos.Insert(cursor,new HrefInfo(info.color, temp));
  162. cursor++;
  163. info.text = temp2;
  164. }
  165. string str1 = "";
  166. string str2 = "";
  167. for(int idx =0; idx < cursor;idx++)
  168. {
  169. var info = mTextInfos[idx];
  170. if (!string.IsNullOrEmpty(info.text))
  171. {
  172. if (string.IsNullOrEmpty(info.color))
  173. {
  174. str1 += info.text;
  175. }
  176. else
  177. {
  178. str1 = str1 + string.Format("<color=#{0}>{1}</color>", info.color, info.text);
  179. }
  180. }
  181. }
  182. for(int idx = cursor; idx < mTextInfos.Count;idx++)
  183. {
  184. var info = mTextInfos[idx];
  185. if(!string.IsNullOrEmpty(info.text))
  186. {
  187. if (string.IsNullOrEmpty(info.color))
  188. {
  189. str2 += info.text;
  190. }
  191. else
  192. {
  193. str2 = str2 + string.Format("<color=#{0}>{1}</color>", info.color, info.text);
  194. }
  195. }
  196. }
  197. if (!string.IsNullOrEmpty(str1))
  198. {
  199. if (mOutputList.Count >= MaxNum)
  200. {
  201. mOutputList.RemoveAt(0);
  202. }
  203. mOutputList.Add(str1);
  204. }
  205. if (!string.IsNullOrEmpty(str2))
  206. {
  207. if (mOutputList.Count >= MaxNum)
  208. {
  209. mOutputList.RemoveAt(0);
  210. }
  211. mOutputList.Add(str2);
  212. }
  213. NotifyOutput();
  214. }
  215. List<HrefInfo> mTextInfos = new List<HrefInfo>();
  216. Regex s_HrefRegex = new Regex(@"<color=#(.+?)>(.*?)(</color>)", RegexOptions.Singleline);
  217. protected void GetOutputText(string text)
  218. {
  219. mTextInfos.Clear();
  220. var indexText = 0;
  221. foreach (Match match in s_HrefRegex.Matches(text))
  222. {
  223. var hrefInfo = new HrefInfo(null, text.Substring(indexText, match.Index - indexText));
  224. mTextInfos.Add(hrefInfo);
  225. var hrefInfo2 = new HrefInfo(match.Groups[1].Value, match.Groups[2].Value);
  226. mTextInfos.Add(hrefInfo2);
  227. indexText = match.Index + match.Length;
  228. }
  229. string lastText = text.Substring(indexText, text.Length - indexText);
  230. if (!string.IsNullOrEmpty(lastText))
  231. {
  232. var lastInfo = new HrefInfo(null, lastText);
  233. mTextInfos.Add(lastInfo);
  234. }
  235. }
  236. private void NotifyOutput()
  237. {
  238. BattleMgr.Instance.OnRefreshBattleOutput();
  239. }
  240. public void Clean()
  241. {
  242. mOutputList.Clear();
  243. }
  244. public void Dispose()
  245. {
  246. Clean();
  247. }
  248. }