QuestScript.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. using CommonAI.Zone;
  2. using CommonAI.Zone.Instance;
  3. using CommonAI.Zone.ZoneEditor;
  4. using CommonLang;
  5. using CommonLang.Log;
  6. using CommonLang.Vector;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. using XmdsCommonServer.Plugin.Scene;
  12. using XmdsCommonServer.Plugin.Units;
  13. namespace XmdsCommonServer.Plugin.Quest
  14. {
  15. /// <summary>
  16. /// 任务脚本子类
  17. /// </summary>
  18. public abstract class QuestScript
  19. {
  20. protected readonly static Logger log = LoggerFactory.GetLogger("QuestScript");
  21. private string m_QuestID;
  22. //private int m_QuestType;
  23. private XmdsInstancePlayer m_Player;
  24. private XmdsServerScene m_Zone;
  25. private XmdsVirtual_Player m_Virtual;
  26. /// <summary>
  27. /// 当前场景
  28. /// </summary>
  29. public XmdsServerScene Zone { get { return m_Zone; } }
  30. /// <summary>
  31. /// 触发的任务ID
  32. /// </summary>
  33. public string QuestID { get { return m_QuestID; } }
  34. /// <summary>
  35. /// 触发的任务类型
  36. /// </summary>
  37. //public int QuestType { get { return m_QuestType; } }
  38. /// <summary>
  39. /// 触发任务的玩家实体
  40. /// </summary>
  41. public XmdsInstancePlayer Player { get { return m_Player; } }
  42. /// <summary>
  43. /// 触发任务的玩家虚体
  44. /// </summary>
  45. public XmdsVirtual_Player Virtual { get { return m_Virtual; } }
  46. /// <summary>
  47. /// 当前AOI
  48. /// </summary>
  49. public XmdsPlayerAOI AOI { get { return m_Player.AoiStatus as XmdsPlayerAOI; } }
  50. Action<QuestScript> mOnStart;
  51. Action<QuestScript> mOnDispose;
  52. public event Action<QuestScript> DisposeEvent { add { mOnDispose += value; } remove { mOnDispose -= value; } }
  53. public event Action<QuestScript> StartEvent { add { mOnStart += value; } remove { mOnStart -= value; } }
  54. //---------------------------------------------------------------------------------------------------
  55. internal void Init(string questID, int questType, XmdsInstancePlayer player)
  56. {
  57. this.m_QuestID = questID;
  58. this.m_Player = player;
  59. this.m_Zone = player.Parent as XmdsServerScene;
  60. this.m_Virtual = player.Virtual as XmdsVirtual_Player;
  61. }
  62. internal void Dispose()
  63. {
  64. this.OnDispose();
  65. if (mOnDispose != null)
  66. {
  67. mOnDispose.Invoke(this);
  68. }
  69. this.m_QuestID = null;
  70. this.m_Player = null;
  71. this.m_Zone = null;
  72. this.m_Virtual = null;
  73. }
  74. internal void Start()
  75. {
  76. this.OnStart();
  77. if (mOnStart != null)
  78. {
  79. mOnStart.Invoke(this);
  80. }
  81. }
  82. //---------------------------------------------------------------------------------------------------
  83. /// <summary>
  84. /// 脚本开始回调
  85. /// </summary>
  86. protected abstract void OnStart();
  87. /// <summary>
  88. /// 脚本结束回调
  89. /// </summary>
  90. protected abstract void OnDispose();
  91. /// <summary>
  92. /// 返回true表示处理完成后不传递到其他QuestScrpt中
  93. /// </summary>
  94. /// <returns></returns>
  95. public virtual bool TryDoAction(ObjectAction act)
  96. {
  97. return false;
  98. }
  99. //---------------------------------------------------------------------------------------------------
  100. #region API
  101. /// <summary>
  102. /// 任务失败
  103. /// </summary>
  104. /// <param name="reason">原因</param>
  105. public void QuestFailed(string reason = "")
  106. {
  107. Zone.QuestAdapter.DoDropQuest(Player.PlayerUUID, QuestID, reason);
  108. }
  109. /// <summary>
  110. /// 任务完成
  111. /// </summary>
  112. /// <param name="reason">原因</param>
  113. public void QuestAccomplish(string reason = "")
  114. {
  115. Zone.QuestAdapter.DoCommitQuest(Player.PlayerUUID, QuestID, reason);
  116. }
  117. public virtual void OnStatusChanged(string key, string v)
  118. {
  119. }
  120. /// <summary>
  121. /// 玩家进入AOI
  122. /// </summary>
  123. /// <param name="can_see_me"></param>
  124. /// <param name="can_see_other"></param>
  125. public void PlayerEnterAOI(bool can_see_me = false, bool can_see_other = false)
  126. {
  127. XmdsPlayerAOI aoi = new XmdsPlayerAOI(Player, can_see_me, can_see_other);
  128. Player.setAoiStatus(aoi);
  129. }
  130. /// <summary>
  131. /// 玩家离开AOI
  132. /// </summary>
  133. public void PlayerLeaveAOI()
  134. {
  135. Player.setAoiStatus(null);
  136. }
  137. /// <summary>
  138. /// 添加若干单位在区域范围内
  139. /// </summary>
  140. /// <param name="region_name">区域名字</param>
  141. /// <param name="unit_template_id">模板ID</param>
  142. /// <param name="count">数量</param>
  143. /// <param name="force">Force</param>
  144. /// <param name="aoi">是否在AOI</param>
  145. /// <param name="unit_name">名字</param>
  146. /// <param name="level">等级</param>
  147. /// <param name="attack_to">出生后A到此处</param>
  148. /// <returns></returns>
  149. public InstanceUnit[] AddUnitsInRegion(string region_name, int unit_template_id, int count, int force, bool aoi = true, string unit_name = null, int level = 0, string attack_to = null)
  150. {
  151. InstanceUnit[] ret = new InstanceUnit[count];
  152. var region = Zone.GetEditFlag(region_name) as ZoneRegion;
  153. if (region != null)
  154. {
  155. for (int i = count - 1; i >= 0; --i)
  156. {
  157. var pos = region.getRandomPos(Zone.RandomN);
  158. var unit = AddUnit(pos, unit_template_id, force, aoi, unit_name, level, attack_to);
  159. ret[i] = unit;
  160. }
  161. }
  162. else
  163. {
  164. log.Error("Region not exist : " + region_name);
  165. }
  166. return ret;
  167. }
  168. /// <summary>
  169. /// 添加一个单位
  170. /// </summary>
  171. /// <param name="region_name">区域名字</param>
  172. /// <param name="unit_template_id">模板ID</param>
  173. /// <param name="force">Force</param>
  174. /// <param name="aoi">是否在AOI</param>
  175. /// <param name="unit_name">名字</param>
  176. /// <param name="level">等级</param>
  177. /// <param name="attack_to">出生后A到此处</param>
  178. /// <returns></returns>
  179. public InstanceUnit AddUnitInRegion(string region_name, int unit_template_id, int force, bool aoi = true, string unit_name = null, int level = 0, string attack_to = null)
  180. {
  181. var region = Zone.GetEditFlag(region_name) as ZoneRegion;
  182. if (region != null)
  183. {
  184. return AddUnit(region.Pos, unit_template_id, force, aoi, unit_name, level, attack_to);
  185. }
  186. else
  187. {
  188. log.Error("Region not exist : " + region_name);
  189. return null;
  190. }
  191. }
  192. /// <summary>
  193. /// 添加一个单位
  194. /// </summary>
  195. /// <param name="pos">坐标</param>
  196. /// <param name="unit_template_id">模板ID</param>
  197. /// <param name="force">Force</param>
  198. /// <param name="aoi">是否在AOI</param>
  199. /// <param name="unit_name">名字</param>
  200. /// <param name="level">等级</param>
  201. /// <param name="attack_to">出生后A到此处</param>
  202. /// <returns></returns>
  203. public InstanceUnit AddUnit(IVector2 pos, int unit_template_id, int force, bool aoi = true, string unit_name = null, int level = 0, string attack_to = null)
  204. {
  205. InstanceUnit unit = Zone.AddUnit(unit_template_id, "", (byte)force, level, pos.X, pos.Y, CMath.RandomAngle(Zone.RandomN));
  206. if (unit != null)
  207. {
  208. if (aoi) unit.setAoiStatus(Player.AoiStatus);
  209. if (unit is InstanceGuard && attack_to != null)
  210. {
  211. InstanceGuard guard = unit as InstanceGuard;
  212. ZoneWayPoint flag = Zone.GetEditFlag(attack_to) as ZoneWayPoint;
  213. if (flag != null)
  214. {
  215. guard.attackTo(flag);
  216. }
  217. }
  218. }
  219. else
  220. {
  221. log.Error("Create unit error : " + unit_template_id);
  222. }
  223. return unit;
  224. }
  225. #endregion
  226. //---------------------------------------------------------------------------------------------------
  227. }
  228. public class Quest_Simple : QuestScript
  229. {
  230. protected override void OnDispose() {}
  231. protected override void OnStart() {}
  232. }
  233. }