XmdsBattleSkill.cs 15 KB

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