123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453 |
- using CommonAI.data;
- using CommonAI.Zone;
- using CommonLang;
- using CommonLang.Log;
- using CommonLang.Property;
- using System;
- using System.Collections.Generic;
- using System.Reflection;
- using System.Text;
- using XmdsCommon.Plugin;
- using XmdsCommonServer.XLS.Data;
- using static XmdsCommonServer.Plugin.XmdsVirtual;
- namespace XmdsCommonServer.Plugin.XmdsSkillTemplate.Skills
- {
- /// <summary>
- /// FileName: XmdsBattleSkill.cs
- /// Description:
- /// DateTime: 2015/6/1 17:50:47
- /// </summary>
- public static class XmdsBattleSkill
- {
- //默认脚本ID.
- public static readonly int DefaultSkillScriptID = 9999901;
- private static TemplateManager mTemplates = null;
- private static Logger log = LoggerFactory.GetLogger("XmdsBattleSkill");
- //主动技能.
- private static IDictionary<int, Type> UnitSkillMap = new SortedDictionary<int, Type>();
- //被动技能.
- private static IDictionary<int, Type> UnitPassiveSkillMap = new SortedDictionary<int, Type>();
- private static IDictionary<int, BattleFunctionEvent> BattleFunctionEventMap = new SortedDictionary<int, BattleFunctionEvent>();
- private static bool FinishInit = false;
- //默认技能伤害机制
- public static XmdsVirtual.OnCalDmageHandler mDefDamgageHandler;
- //private static List<UnitSkill> UnitSkillAllTypes = new List<UnitSkill>();
- static XmdsBattleSkill()
- {
- }
- private static void LoadSkillDll()
- {
- if (FinishInit) { return; }
- {
- //扩展功能.
- try
- {
- Type ctype = typeof(BattleFunction);
- foreach (Type stype in ReflectionUtil.GetNoneVirtualSubTypes(ctype))
- {
- BattleFunction bf = ReflectionUtil.CreateInstance(stype) as BattleFunction;
- BattleFunction.SetFactory(bf);
- }
- ctype = typeof(BattleFunctionEvent);
- foreach (Type stype in ReflectionUtil.GetNoneVirtualSubTypes(ctype))
- {
- try
- {
- BattleFunctionEvent be = ReflectionUtil.CreateInstance(stype) as BattleFunctionEvent;
- if(be.Init(0, 0))
- {
- BattleFunctionEventMap.Add(be.GetEventID(), be);
- }
- }
- catch(Exception e)
- {
- log.Error("init battle funtion event catch: " + stype + ", " + e);
- }
- }
- }
- catch (Exception err)
- {
- log.Error("BattleFunctionEvent Error" + err.ToString());
- //throw;
- }
- }
- {
- //主动技能.
- try
- {
- Type ctype = typeof(UnitSkill);
- foreach (Type stype in ReflectionUtil.GetNoneVirtualSubTypes(ctype))
- {
- UnitSkill sk = ReflectionUtil.CreateInstance(stype) as UnitSkill;
- if (sk.SkillID < 1)
- {
- if(!sk.GetType().IsAbstract)
- {
- log.Warn("异常技能1, ID = " + sk.SkillID + ", name=" + sk.ToString());
- }
-
- continue;
- }
- XmdsSkillData skillData = XmdsDataMgr.GetInstance().GetXmdsSkillData(sk.SkillID);
- if (skillData == null)
- {
- log.Warn("技能表未配置数据:" + sk.SkillID + ", 伤害走通用序列:" + sk.ToString());
- continue;
- }
- //log.Debug("Regist skill : " + stype.FullName);
- if (UnitSkillMap.ContainsKey(sk.SkillID))
- {
- throw new Exception("主动技能脚本ID重复:" + sk.SkillID);
- }
- UnitSkillMap.Add(sk.SkillID, stype);
- sk.InitSkillParam(skillData);
- }
- }
- catch (Exception err)
- {
- log.Error("ActiveSkillInit Error" + err.ToString());
- //throw;
- }
- }
- {
- //被动技能.
- try
- {
- Type type = typeof(UnitPassiveSkill);
- foreach (Type stype in ReflectionUtil.GetNoneVirtualSubTypes(type))
- {
- UnitPassiveSkill sk = ReflectionUtil.CreateInstance(stype) as UnitPassiveSkill;
- if (sk.SkillID < 1 )
- {
- log.Info("异常技能2, ID = " + sk.SkillID + ", name=" + sk.ToString());
- continue;
- }
- if (XmdsDataMgr.GetInstance().GetXmdsSkillData(sk.SkillID) == null)
- {
- log.Error("skillData表未找到技能配置数据:" + sk.SkillID);
- continue;
- }
- //log.Info("Regist PassiveSkill : " + stype.FullName);
- if (UnitPassiveSkillMap.ContainsKey(sk.SkillID))
- {
- throw new Exception("被动技能脚本ID重复:" + sk.SkillID);
- }
- UnitPassiveSkillMap.Add(sk.SkillID, stype);
- sk.InitSkillParam();
- }
- }
- catch (Exception err)
- {
- log.Error("LoadPassiveSkillDll" + err.ToString());
- //throw;
- }
- }
- // 默认技能处理序列
- Type defaultSkillType = null;
- UnitSkillMap.TryGetValue(DefaultSkillScriptID, out defaultSkillType);
- if(defaultSkillType != null)
- {
- GameSkill gameSkill = new GameSkill(DefaultSkillScriptID);
- UnitSkill defaultSkill = ReflectionUtil.CreateInstance(defaultSkillType) as UnitSkill;
- SkillTemplate skillTemplate = XmdsBattleSkill.GetSkillTemplate(DefaultSkillScriptID); ;
- defaultSkill.Init(gameSkill, null, ref skillTemplate);
- mDefDamgageHandler = new OnCalDmageHandler(defaultSkill.OnCallDamageProcess, gameSkill, false, 0);
- }
- else
- {
- log.Warn("默认技能伤害处理序列找不到!");
- }
- FinishInit = true;
- }
- public static void Init(TemplateManager mgr)
- {
- mTemplates = mgr;
- //反射并初始化所有继承XmdsSkill的类.
- LoadSkillDll();
- }
- /// <summary>
- /// 生成示例文件.
- /// </summary>
- /// <returns></returns>
- //public static string GenSampleSkillConfig()
- //{
- // bool gen_cfg = true;
- // StringBuilder sb = new StringBuilder();
- // UnitSkillAllTypes.Sort();
- // foreach (UnitSkill sk in UnitSkillAllTypes)
- // {
- // Type type = sk.GetType();
- // SkillTypeAttribute desca = PropertyUtil.GetAttribute<SkillTypeAttribute>(type);
- // sb.AppendLine("==================================================================");
- // sb.AppendLine(string.Format(">>>> {0} Catgory = {1} <<<<", desca.Category, sk.SkillID));
- // foreach (string desc_line in desca.Desc.Split('\n'))
- // {
- // sb.AppendLine(string.Format("=== {0} ===", desc_line));
- // }
- // sb.AppendLine("==================================================================");
- // foreach (FieldInfo fi in type.GetFields())
- // {
- // if (fi.IsStatic && fi.IsPublic)
- // {
- // FieldConfigurable cfg = PropertyUtil.GetAttribute<FieldConfigurable>(fi);
- // object static_value = fi.GetValue(null);
- // string static_text = Parser.ObjectToString(static_value);
- // if (cfg != null && gen_cfg)
- // {
- // sb.AppendLine(cfg.Desc);
- // }
- // sb.AppendLine(string.Format("{0}.{1} , {2}", type.Name, fi.Name, static_text));
- // }
- // }
- // sb.AppendLine();
- // sb.AppendLine();
- // }
- // return sb.ToString();
- //}
- /// <summary>
- /// 加载数据配置文件.
- /// </summary>
- /// <param name="csv"></param>
- //public static void LoadSkillConfig(string csv)
- //{
- // IDictionary<string, object> skills = new SortedDictionary<string, object>();
- // foreach (UnitSkill sk in UnitSkillAllTypes)
- // {
- // skills.Add(sk.GetType().Name, sk);
- // }
- // Properties cfg = new Properties();
- // cfg.ParseText(csv, ",");
- // foreach (KeyValuePair<string, string> line in cfg)
- // {
- // try
- // {
- // string[] kv = line.Key.Split(new char[] { '.' }, 2);
- // if (kv.Length == 2)
- // {
- // string typeName = kv[0];
- // string fieldName = kv[1];
- // if (skills.ContainsKey(typeName))
- // {
- // object sk = skills[typeName];
- // FieldInfo fi = sk.GetType().GetField(fieldName);
- // FieldConfigurable fdesc = PropertyUtil.GetAttribute<FieldConfigurable>(fi);
- // object static_value = Parser.StringToObject(line.Value, fi.FieldType);
- // if (static_value != null)
- // {
- // fi.SetValue(null, static_value);
- // if (fdesc != null) { log.Info("### " + fdesc.Desc); }
- // log.Info(" --------------------- " + line.Key + " = " + line.Value);
- // }
- // else
- // {
- // log.Error("无法解析配置项 : " + line.Key + " = " + line.Value);
- // }
- // }
- // }
- // }
- // catch (Exception err)
- // {
- // throw new Exception("无法解析配置项 : " + line.Key + " = " + line.Value + " : " + err.Message, err);
- // }
- // }
- //}
- #region 获取模板数据.
- public static SkillTemplate GetSkillTemplate(int skillTemplateID)
- {
- SkillTemplate template = mTemplates.getSkill(skillTemplateID);
- if (template != null)
- {
- template = template.Clone() as SkillTemplate;
- }
- return template;
- }
- public static SpellTemplate GetSpellTemplate(int spellTemplateID)
- {
- SpellTemplate template = mTemplates.getSpell(spellTemplateID);
- if (template != null)
- {
- template = template.Clone() as SpellTemplate;
- }
- return template;
- }
- public static BuffTemplate GetBuffTemplate(int templateID, bool doClone = true)
- {
- BuffTemplate template = mTemplates.getBuff(templateID);
- if (template != null && doClone)
- {
- template = template.Clone() as BuffTemplate;
- }
- return template;
- }
- public static UnitInfo GetUnitInfo(int templateID)
- {
- UnitInfo ret = mTemplates.getUnit(templateID);
- return ret;
- }
- #endregion
- #region 获取XmdsSkill.
- public static UnitSkill GetUnitSkill(int id)
- {
- UnitSkill ret = null;
- Type templateType = null;
- if(UnitSkillMap.TryGetValue(id, out templateType))
- {
- ret = ReflectionUtil.CreateInstance(templateType) as UnitSkill;
- }
- return ret;
- }
- #endregion
- #region 获取XmdsPassiveSkill.
- public static UnitPassiveSkill GetPassiveSkill(int id)
- {
- UnitPassiveSkill ret = null;
- Type templateType = null;
- if (UnitPassiveSkillMap.TryGetValue(id, out templateType))
- {
- ret = ReflectionUtil.CreateInstance(templateType) as UnitPassiveSkill;
- }
- return ret;
- }
- #endregion
- #region 获取战斗特殊事件.
- //添加事件
- public static void loadExtendFuntionEvent()
- {
- HashMap<int, BuffConfig> buffConfigs = XmdsDataMgr.GetInstance().GetBuffConfigDatas();
- if (buffConfigs != null)
- {
- foreach (var buffConfig in buffConfigs.Values)
- {
- try
- {
- if (buffConfig.BaseFunID < 0) { continue; }
- BattleFunctionEvent bfEvent = XmdsBattleSkill.GetBattleFunctionEvent(buffConfig.TriggerID);
- if (bfEvent != null) { continue; }
- if (bfEvent == null && buffConfig.BaseFunID == 0)
- {
- log.Warn("没有实现的触发事件:" + buffConfig.TriggerID);
- continue;
- }
- bfEvent = XmdsBattleSkill.GetBattleFunctionEvent(buffConfig.BaseFunID);
- if (bfEvent == null)
- {
- log.Warn("没有实现的基础触发事件:" + buffConfig.TriggerID + ", base: " + buffConfig.BaseFunID);
- continue;
- }
- BattleFunctionEvent extendEvent = ReflectionUtil.CreateInstance(bfEvent.GetType()) as BattleFunctionEvent;
- if (extendEvent == null)
- {
- log.Warn("创建扩展事件失败:" + buffConfig.TriggerID + ", base: " + buffConfig.BaseFunID);
- continue;
- }
- if(extendEvent.Init(buffConfig.TriggerID, bfEvent.GetBindBuffID()))
- {
- BattleFunctionEventMap.Add(buffConfig.TriggerID, extendEvent);
- }
- }
- catch (Exception e)
- {
- log.Error("loadExtendFuntionEvent异常:" + e);
- }
- }
- }
- }
- public static BattleFunctionEvent GetBattleFunctionEvent(int id)
- {
- BattleFunctionEvent ret = null;
- BattleFunctionEventMap.TryGetValue(id, out ret);
- return ret;
- }
- #endregion
- }
- #region 技能描述字段.
- //SkillTypeAttribute("狂战", "雕文技能")
- [AttributeUsage(AttributeTargets.Field)]
- public class FieldConfigurable : System.Attribute
- {
- public readonly string Desc;
- public FieldConfigurable(string desc)
- {
- this.Desc = desc;
- }
- }
- //【顺势】毁灭打击伤害的X%伤害.
- [AttributeUsage(AttributeTargets.Class)]
- public class SkillTypeAttribute : System.Attribute
- {
- public readonly string Desc;
- public readonly string Category;
- public SkillTypeAttribute(string desc, string catgory)
- {
- this.Desc = desc;
- this.Category = catgory;
- }
- }
- #endregion
- }
|