QuestScriptManager.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. using CommonAI.Zone.Instance;
  2. using CommonAI.Zone.ZoneEditor;
  3. using CommonLang;
  4. using CommonLang.Log;
  5. using CommonLang.Property;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using XmdsCommon.Plugin;
  11. using XmdsCommonServer.Message;
  12. using XmdsCommonServer.Plugin.Units;
  13. namespace XmdsCommonServer.Plugin.Quest
  14. {
  15. public class QuestScriptManager
  16. {
  17. private static Logger log = LoggerFactory.GetLogger("QuestScriptManager");
  18. private static HashMap<string, Type> QuestTypes = new HashMap<string, Type>();
  19. private static HashMap<int, Type> QuestTypeTypes = new HashMap<int, Type>();
  20. static QuestScriptManager()
  21. {
  22. }
  23. public static void Init()
  24. {
  25. LoadQuestDll();
  26. }
  27. private static void LoadQuestDll()
  28. {
  29. if(QuestTypeTypes.Count > 0)
  30. {
  31. return;
  32. }
  33. try
  34. {
  35. Type base_type = typeof(QuestScript);
  36. foreach (Type sub_type in ReflectionUtil.GetNoneVirtualSubTypes(base_type))
  37. {
  38. if (sub_type.IsSubclassOf(base_type))
  39. {
  40. var attr = PropertyUtil.GetAttribute<QuestListenAttribute>(sub_type);
  41. if (attr != null)
  42. {
  43. QuestTypes.Add(attr.quest_id, sub_type);
  44. }
  45. var attr_qtype = PropertyUtil.GetAttribute<QuestTypeListenAttribute>(sub_type);
  46. if (attr_qtype != null)
  47. {
  48. QuestTypeTypes.Add((int)attr_qtype.quest_type, sub_type);
  49. }
  50. }
  51. }
  52. }
  53. catch (Exception err)
  54. {
  55. log.Error("LoadQuestDll : " + err);
  56. }
  57. }
  58. public static QuestScript CreateQuestScript(string quest_id, XmdsInstancePlayer player)
  59. {
  60. var q = player.GetQuest(quest_id);
  61. QuestScript ret = null;
  62. try
  63. {
  64. int taskType = XmdsQuestData.GetTaskType(q);
  65. string qua = q.Attributes.Get("SecretTransfer");
  66. Type stype;
  67. if (QuestTypes.TryGetValue(quest_id, out stype) || QuestTypeTypes.TryGetValue(taskType, out stype))
  68. {
  69. ret = ReflectionUtil.CreateInstance(stype) as QuestScript;
  70. }
  71. if (!string.IsNullOrEmpty(qua))
  72. {
  73. string[] s = qua.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
  74. int sceneID = int.Parse(s[0]);
  75. if (sceneID == (player.Parent as EditorScene).Data.GetID())
  76. {
  77. ZoneRegion region = player.Parent.getFlagAs<ZoneRegion>(s[1]);
  78. int playerCount = region.getObjectsCountInRegion<InstancePlayer>((ip) =>
  79. {
  80. return ip == player;
  81. });
  82. if (playerCount <= 0)
  83. {
  84. if (ret == null)
  85. {
  86. ret = new Quest_Simple();
  87. }
  88. ZoneRegion.UnitEnterHandler handler = new ZoneRegion.UnitEnterHandler((rg, u) =>
  89. {
  90. if (u == ret.Player)
  91. {
  92. // 传送
  93. var q1 = ret.Player.GetQuest(quest_id);
  94. if (q1 != null && !XmdsQuestData.IsTargetComplete(q1))
  95. {
  96. int transSceneID = XmdsQuestData.GetTargetSceneID(q1);
  97. string transPoint = XmdsQuestData.GetTargetScenePoint(q1);
  98. TransUnitEventB2R evt = new TransUnitEventB2R();
  99. evt.playerId = ret.Player.PlayerUUID;
  100. evt.SceneID = transSceneID;
  101. ret.Player.queueEvent(evt);
  102. }
  103. }
  104. });
  105. ret.DisposeEvent += (script) =>
  106. {
  107. region.OnUnitEnter -= handler;
  108. };
  109. ret.StartEvent += (script) =>
  110. {
  111. region.OnUnitEnter += handler;
  112. };
  113. }
  114. }
  115. }
  116. if (ret != null)
  117. {
  118. player.SetQuestScript(quest_id, taskType, ret);
  119. }
  120. }
  121. catch (Exception err)
  122. {
  123. log.Error(string.Format("Try CreateQuestScript [{0}] Error :{1} ", quest_id, err.ToString()));
  124. }
  125. return ret;
  126. }
  127. public static void ClearQuestScript(string quest_id, XmdsInstancePlayer player)
  128. {
  129. player.RemoveQuestScript(quest_id);
  130. }
  131. public static void QuestStatusChanged(string quest_id, XmdsInstancePlayer player, string key, string value)
  132. {
  133. var qs = player.GetQuestScript(quest_id);
  134. if (qs != null)
  135. {
  136. qs.OnStatusChanged(key, value);
  137. }
  138. }
  139. }
  140. /// <summary>
  141. /// 标记类,处理接取任务事件
  142. /// </summary>
  143. [AttributeUsage(AttributeTargets.Class)]
  144. public class QuestListenAttribute : System.Attribute
  145. {
  146. readonly public string quest_id;
  147. public QuestListenAttribute(string quest_id)
  148. {
  149. this.quest_id = quest_id;
  150. }
  151. }
  152. /// <summary>
  153. /// 标记类,处理接取任务事件
  154. /// </summary>
  155. [AttributeUsage(AttributeTargets.Class)]
  156. public class QuestTypeListenAttribute : System.Attribute
  157. {
  158. readonly public XmdsQuestData.TaskType quest_type;
  159. public QuestTypeListenAttribute(XmdsQuestData.TaskType q_type)
  160. {
  161. this.quest_type = q_type;
  162. }
  163. }
  164. }