XmdsBattleSkill.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. using CommonAI.data;
  2. using CommonAI.Zone;
  3. using CommonLang;
  4. using CommonLang.Log;
  5. using CommonLang.Property;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Reflection;
  9. using System.Text;
  10. using XmdsCommon.Plugin;
  11. using XmdsCommonServer.XLS.Data;
  12. using static XmdsCommonServer.Plugin.XmdsVirtual;
  13. namespace XmdsCommonServer.Plugin.XmdsSkillTemplate.Skills
  14. {
  15. /// <summary>
  16. /// FileName: XmdsBattleSkill.cs
  17. /// Description:
  18. /// DateTime: 2015/6/1 17:50:47
  19. /// </summary>
  20. public static class XmdsBattleSkill
  21. {
  22. //默认脚本ID.
  23. public static readonly int DefaultSkillScriptID = 9999901;
  24. private static TemplateManager mTemplates = null;
  25. private static Logger log = LoggerFactory.GetLogger("XmdsBattleSkill");
  26. //主动技能.
  27. private static IDictionary<int, Type> UnitSkillMap = new SortedDictionary<int, Type>();
  28. //被动技能.
  29. private static IDictionary<int, Type> UnitPassiveSkillMap = new SortedDictionary<int, Type>();
  30. private static IDictionary<int, BattleFunctionEvent> BattleFunctionEventMap = new SortedDictionary<int, BattleFunctionEvent>();
  31. private static bool FinishInit = false;
  32. //默认技能伤害机制
  33. public static XmdsVirtual.OnCalDmageHandler mDefDamgageHandler;
  34. //private static List<UnitSkill> UnitSkillAllTypes = new List<UnitSkill>();
  35. static XmdsBattleSkill()
  36. {
  37. }
  38. private static void LoadSkillDll()
  39. {
  40. if (FinishInit) { return; }
  41. {
  42. //扩展功能.
  43. try
  44. {
  45. Type ctype = typeof(BattleFunction);
  46. foreach (Type stype in ReflectionUtil.GetNoneVirtualSubTypes(ctype))
  47. {
  48. BattleFunction bf = ReflectionUtil.CreateInstance(stype) as BattleFunction;
  49. BattleFunction.SetFactory(bf);
  50. }
  51. ctype = typeof(BattleFunctionEvent);
  52. foreach (Type stype in ReflectionUtil.GetNoneVirtualSubTypes(ctype))
  53. {
  54. try
  55. {
  56. BattleFunctionEvent be = ReflectionUtil.CreateInstance(stype) as BattleFunctionEvent;
  57. if(be.Init(0, 0))
  58. {
  59. BattleFunctionEventMap.Add(be.GetEventID(), be);
  60. }
  61. }
  62. catch(Exception e)
  63. {
  64. log.Error("init battle funtion event catch: " + stype + ", " + e);
  65. }
  66. }
  67. }
  68. catch (Exception err)
  69. {
  70. log.Error("BattleFunctionEvent Error" + err.ToString());
  71. //throw;
  72. }
  73. }
  74. {
  75. //主动技能.
  76. try
  77. {
  78. Type ctype = typeof(UnitSkill);
  79. foreach (Type stype in ReflectionUtil.GetNoneVirtualSubTypes(ctype))
  80. {
  81. UnitSkill sk = ReflectionUtil.CreateInstance(stype) as UnitSkill;
  82. if (sk.SkillID < 1)
  83. {
  84. if(!sk.GetType().IsAbstract)
  85. {
  86. log.Warn("异常技能1, ID = " + sk.SkillID + ", name=" + sk.ToString());
  87. }
  88. continue;
  89. }
  90. if (XmdsDataMgr.GetInstance().GetXmdsSkillData(sk.SkillID) == null)
  91. {
  92. log.Warn("技能表未配置数据:" + sk.SkillID + ", 伤害走通用序列:" + sk.ToString());
  93. continue;
  94. }
  95. //log.Debug("Regist skill : " + stype.FullName);
  96. if (UnitSkillMap.ContainsKey(sk.SkillID))
  97. {
  98. throw new Exception("主动技能脚本ID重复:" + sk.SkillID);
  99. }
  100. UnitSkillMap.Add(sk.SkillID, stype);
  101. sk.InitSkillParam();
  102. }
  103. }
  104. catch (Exception err)
  105. {
  106. log.Error("ActiveSkillInit Error" + err.ToString());
  107. //throw;
  108. }
  109. }
  110. {
  111. //被动技能.
  112. try
  113. {
  114. Type type = typeof(UnitPassiveSkill);
  115. foreach (Type stype in ReflectionUtil.GetNoneVirtualSubTypes(type))
  116. {
  117. UnitPassiveSkill sk = ReflectionUtil.CreateInstance(stype) as UnitPassiveSkill;
  118. if (sk.SkillID < 1 )
  119. {
  120. log.Info("异常技能2, ID = " + sk.SkillID + ", name=" + sk.ToString());
  121. continue;
  122. }
  123. if (XmdsDataMgr.GetInstance().GetXmdsSkillData(sk.SkillID) == null)
  124. {
  125. log.Error("skillData表未找到技能配置数据:" + sk.SkillID);
  126. continue;
  127. }
  128. //log.Info("Regist PassiveSkill : " + stype.FullName);
  129. if (UnitPassiveSkillMap.ContainsKey(sk.SkillID))
  130. {
  131. throw new Exception("被动技能脚本ID重复:" + sk.SkillID);
  132. }
  133. UnitPassiveSkillMap.Add(sk.SkillID, stype);
  134. sk.InitSkillParam();
  135. }
  136. }
  137. catch (Exception err)
  138. {
  139. log.Error("LoadPassiveSkillDll" + err.ToString());
  140. //throw;
  141. }
  142. }
  143. // 默认技能处理序列
  144. Type defaultSkillType = null;
  145. UnitSkillMap.TryGetValue(DefaultSkillScriptID, out defaultSkillType);
  146. if(defaultSkillType != null)
  147. {
  148. GameSkill gameSkill = new GameSkill(DefaultSkillScriptID);
  149. UnitSkill defaultSkill = ReflectionUtil.CreateInstance(defaultSkillType) as UnitSkill;
  150. SkillTemplate skillTemplate = XmdsBattleSkill.GetSkillTemplate(DefaultSkillScriptID); ;
  151. defaultSkill.Init(gameSkill, null, ref skillTemplate);
  152. mDefDamgageHandler = new OnCalDmageHandler(defaultSkill.OnCallDamageProcess, gameSkill, false, 0);
  153. }
  154. else
  155. {
  156. log.Warn("默认技能伤害处理序列找不到!");
  157. }
  158. FinishInit = true;
  159. }
  160. public static void Init(TemplateManager mgr)
  161. {
  162. mTemplates = mgr;
  163. //反射并初始化所有继承XmdsSkill的类.
  164. LoadSkillDll();
  165. }
  166. /// <summary>
  167. /// 生成示例文件.
  168. /// </summary>
  169. /// <returns></returns>
  170. //public static string GenSampleSkillConfig()
  171. //{
  172. // bool gen_cfg = true;
  173. // StringBuilder sb = new StringBuilder();
  174. // UnitSkillAllTypes.Sort();
  175. // foreach (UnitSkill sk in UnitSkillAllTypes)
  176. // {
  177. // Type type = sk.GetType();
  178. // SkillTypeAttribute desca = PropertyUtil.GetAttribute<SkillTypeAttribute>(type);
  179. // sb.AppendLine("==================================================================");
  180. // sb.AppendLine(string.Format(">>>> {0} Catgory = {1} <<<<", desca.Category, sk.SkillID));
  181. // foreach (string desc_line in desca.Desc.Split('\n'))
  182. // {
  183. // sb.AppendLine(string.Format("=== {0} ===", desc_line));
  184. // }
  185. // sb.AppendLine("==================================================================");
  186. // foreach (FieldInfo fi in type.GetFields())
  187. // {
  188. // if (fi.IsStatic && fi.IsPublic)
  189. // {
  190. // FieldConfigurable cfg = PropertyUtil.GetAttribute<FieldConfigurable>(fi);
  191. // object static_value = fi.GetValue(null);
  192. // string static_text = Parser.ObjectToString(static_value);
  193. // if (cfg != null && gen_cfg)
  194. // {
  195. // sb.AppendLine(cfg.Desc);
  196. // }
  197. // sb.AppendLine(string.Format("{0}.{1} , {2}", type.Name, fi.Name, static_text));
  198. // }
  199. // }
  200. // sb.AppendLine();
  201. // sb.AppendLine();
  202. // }
  203. // return sb.ToString();
  204. //}
  205. /// <summary>
  206. /// 加载数据配置文件.
  207. /// </summary>
  208. /// <param name="csv"></param>
  209. //public static void LoadSkillConfig(string csv)
  210. //{
  211. // IDictionary<string, object> skills = new SortedDictionary<string, object>();
  212. // foreach (UnitSkill sk in UnitSkillAllTypes)
  213. // {
  214. // skills.Add(sk.GetType().Name, sk);
  215. // }
  216. // Properties cfg = new Properties();
  217. // cfg.ParseText(csv, ",");
  218. // foreach (KeyValuePair<string, string> line in cfg)
  219. // {
  220. // try
  221. // {
  222. // string[] kv = line.Key.Split(new char[] { '.' }, 2);
  223. // if (kv.Length == 2)
  224. // {
  225. // string typeName = kv[0];
  226. // string fieldName = kv[1];
  227. // if (skills.ContainsKey(typeName))
  228. // {
  229. // object sk = skills[typeName];
  230. // FieldInfo fi = sk.GetType().GetField(fieldName);
  231. // FieldConfigurable fdesc = PropertyUtil.GetAttribute<FieldConfigurable>(fi);
  232. // object static_value = Parser.StringToObject(line.Value, fi.FieldType);
  233. // if (static_value != null)
  234. // {
  235. // fi.SetValue(null, static_value);
  236. // if (fdesc != null) { log.Info("### " + fdesc.Desc); }
  237. // log.Info(" --------------------- " + line.Key + " = " + line.Value);
  238. // }
  239. // else
  240. // {
  241. // log.Error("无法解析配置项 : " + line.Key + " = " + line.Value);
  242. // }
  243. // }
  244. // }
  245. // }
  246. // catch (Exception err)
  247. // {
  248. // throw new Exception("无法解析配置项 : " + line.Key + " = " + line.Value + " : " + err.Message, err);
  249. // }
  250. // }
  251. //}
  252. #region 获取模板数据.
  253. public static SkillTemplate GetSkillTemplate(int skillTemplateID)
  254. {
  255. SkillTemplate template = mTemplates.getSkill(skillTemplateID);
  256. if (template != null)
  257. {
  258. template = template.Clone() as SkillTemplate;
  259. }
  260. return template;
  261. }
  262. public static SpellTemplate GetSpellTemplate(int spellTemplateID)
  263. {
  264. SpellTemplate template = mTemplates.getSpell(spellTemplateID);
  265. if (template != null)
  266. {
  267. template = template.Clone() as SpellTemplate;
  268. }
  269. return template;
  270. }
  271. public static BuffTemplate GetBuffTemplate(int templateID, bool doClone = true)
  272. {
  273. BuffTemplate template = mTemplates.getBuff(templateID);
  274. if (template != null && doClone)
  275. {
  276. template = template.Clone() as BuffTemplate;
  277. }
  278. return template;
  279. }
  280. public static UnitInfo GetUnitInfo(int templateID)
  281. {
  282. UnitInfo ret = mTemplates.getUnit(templateID);
  283. return ret;
  284. }
  285. #endregion
  286. #region 获取XmdsSkill.
  287. public static UnitSkill GetUnitSkill(int id)
  288. {
  289. UnitSkill ret = null;
  290. Type templateType = null;
  291. if(UnitSkillMap.TryGetValue(id, out templateType))
  292. {
  293. ret = ReflectionUtil.CreateInstance(templateType) as UnitSkill;
  294. }
  295. return ret;
  296. }
  297. #endregion
  298. #region 获取XmdsPassiveSkill.
  299. public static UnitPassiveSkill GetPassiveSkill(int id)
  300. {
  301. UnitPassiveSkill ret = null;
  302. Type templateType = null;
  303. if (UnitPassiveSkillMap.TryGetValue(id, out templateType))
  304. {
  305. ret = ReflectionUtil.CreateInstance(templateType) as UnitPassiveSkill;
  306. }
  307. return ret;
  308. }
  309. #endregion
  310. #region 获取战斗特殊事件.
  311. //添加事件
  312. public static void loadExtendFuntionEvent()
  313. {
  314. HashMap<int, BuffConfig> buffConfigs = XmdsDataMgr.GetInstance().GetBuffConfigDatas();
  315. if (buffConfigs != null)
  316. {
  317. foreach (var buffConfig in buffConfigs.Values)
  318. {
  319. try
  320. {
  321. if (buffConfig.BaseFunID < 0) { continue; }
  322. BattleFunctionEvent bfEvent = XmdsBattleSkill.GetBattleFunctionEvent(buffConfig.TriggerID);
  323. if (bfEvent != null) { continue; }
  324. if (bfEvent == null && buffConfig.BaseFunID == 0)
  325. {
  326. log.Warn("没有实现的触发事件:" + buffConfig.TriggerID);
  327. continue;
  328. }
  329. bfEvent = XmdsBattleSkill.GetBattleFunctionEvent(buffConfig.BaseFunID);
  330. if (bfEvent == null)
  331. {
  332. log.Warn("没有实现的基础触发事件:" + buffConfig.TriggerID + ", base: " + buffConfig.BaseFunID);
  333. continue;
  334. }
  335. BattleFunctionEvent extendEvent = ReflectionUtil.CreateInstance(bfEvent.GetType()) as BattleFunctionEvent;
  336. if (extendEvent == null)
  337. {
  338. log.Warn("创建扩展事件失败:" + buffConfig.TriggerID + ", base: " + buffConfig.BaseFunID);
  339. continue;
  340. }
  341. if(extendEvent.Init(buffConfig.TriggerID, bfEvent.GetBindBuffID()))
  342. {
  343. BattleFunctionEventMap.Add(buffConfig.TriggerID, extendEvent);
  344. }
  345. }
  346. catch (Exception e)
  347. {
  348. log.Error("loadExtendFuntionEvent异常:" + e);
  349. }
  350. }
  351. }
  352. }
  353. public static BattleFunctionEvent GetBattleFunctionEvent(int id)
  354. {
  355. BattleFunctionEvent ret = null;
  356. BattleFunctionEventMap.TryGetValue(id, out ret);
  357. return ret;
  358. }
  359. #endregion
  360. }
  361. #region 技能描述字段.
  362. //SkillTypeAttribute("狂战", "雕文技能")
  363. [AttributeUsage(AttributeTargets.Field)]
  364. public class FieldConfigurable : System.Attribute
  365. {
  366. public readonly string Desc;
  367. public FieldConfigurable(string desc)
  368. {
  369. this.Desc = desc;
  370. }
  371. }
  372. //【顺势】毁灭打击伤害的X%伤害.
  373. [AttributeUsage(AttributeTargets.Class)]
  374. public class SkillTypeAttribute : System.Attribute
  375. {
  376. public readonly string Desc;
  377. public readonly string Category;
  378. public SkillTypeAttribute(string desc, string catgory)
  379. {
  380. this.Desc = desc;
  381. this.Category = catgory;
  382. }
  383. }
  384. #endregion
  385. }