InstanceItem.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using CommonAI.ZoneClient;
  5. using CommonAI.Zone.Helper;
  6. using CommonLang;
  7. namespace CommonAI.Zone.Instance
  8. {
  9. /// <summary>
  10. /// 可拾取的道具
  11. /// </summary>
  12. public class InstanceItem : InstanceZoneObject, ViewTrigger.ViewTriggerListener
  13. {
  14. //拾取信息
  15. public HashMap<String, short> unitPickInfo = null;
  16. public int TotalPickTimes = 0;
  17. public string Name { get; private set; }
  18. public string DisPlayName { get; private set; }
  19. public string Alias { get { return mSyncInfo.Alias; } set { mSyncInfo.Alias = value; } }
  20. public override float Weight { get { return 0; } }
  21. protected ItemTemplate mData;
  22. protected SyncItemInfo mSyncInfo;
  23. protected ViewTrigger mViewTrigger;
  24. protected bool mClientVisible;
  25. protected bool mSyncPos;
  26. protected InstanceUnit mItemCreater;
  27. protected TimeExpire<int> mViewTriggerTimer;
  28. protected TimeExpire<int> mRemovedExpire;
  29. public int mFrom; //0:场景,1:游戏服
  30. public bool mPosValid = true;
  31. public InstanceItem(InstanceZone zone, ItemTemplate item, string name, int force, InstanceUnit creater, string disPlayName,int from)
  32. : base(zone, false)
  33. {
  34. this.Name = (name == null) ? "" : name;
  35. this.DisPlayName = (disPlayName == null) ? "" : disPlayName;
  36. this.mData = item;
  37. if(mData.LifeTimeMS > 0)
  38. {
  39. this.mRemovedExpire = new TimeExpire<int>(mData.LifeTimeMS);
  40. }
  41. else
  42. {
  43. this.mRemovedExpire = null;
  44. }
  45. //this.mRemovedExpire = new TimeExpire<int>(mData.LifeTimeMS);
  46. this.mViewTrigger = new ViewTriggerRoundBody(zone, item.BodySize);
  47. this.mViewTrigger.setListener(this);
  48. this.mViewTrigger.Enable = false;
  49. if (!mData.Pickable)
  50. {
  51. this.mViewTriggerTimer = new TimeExpire<int>(item.GotCoolDownTimeMS);
  52. }
  53. this.mSyncInfo = new SyncItemInfo(zone.IsHalfSync);
  54. this.mSyncInfo.TemplateID = mData.ID;
  55. this.mSyncInfo.Force = force;
  56. this.mSyncInfo.showItemLeftTimes = mData.showLifeTime;
  57. if(mData.showLifeTime)
  58. {
  59. this.mSyncInfo.ItemTotalTimeMS = mData.LifeEndTime;
  60. }
  61. else
  62. {
  63. this.mSyncInfo.ItemTotalTimeMS = mData.LifeTimeMS;
  64. }
  65. this.Force = force;
  66. this.mClientVisible = item.ClientVisible;
  67. this.mSyncPos = false;// zone.SyncPos;
  68. this.mItemCreater = creater;
  69. this.mFrom = from;
  70. if(this.Info.maxPickPlayers > 0 || this.Info.maxPickTimes > 0)
  71. {
  72. unitPickInfo = new HashMap<String, short>(Math.Max(1, this.Info.maxPickPlayers/2));
  73. }
  74. }
  75. public ItemTemplate Info { get { return mData; } }
  76. public int Force { get; private set; }
  77. public override bool IntersectMap { get { return false; } }
  78. public override bool IntersectObj { get { return false; } }
  79. public override bool Moveable { get { return false; } }
  80. public override float BodyBlockSize { get { return mData.BodySize; } }
  81. override public float BodyHitSize { get { return mData.BodySize; } }
  82. public override float BodyHeight { get { return mData.BodySize; } }
  83. public override bool ClientVisible { get { return mClientVisible; } }
  84. public override bool SyncPos { get { return mSyncPos; } }
  85. public InstanceUnit ItemCreater { get { return mItemCreater; } }
  86. public int ExpireTimeMS {
  87. get {
  88. if(mRemovedExpire != null)
  89. {
  90. return mRemovedExpire.TotalTimeMS - mRemovedExpire.PassTimeMS;
  91. }
  92. return int.MaxValue;
  93. }
  94. }
  95. public bool IsPosValid()
  96. {
  97. return this.mPosValid;
  98. }
  99. public void setPosNotValid()
  100. {
  101. this.mPosValid = false;
  102. }
  103. public override SyncObjectInfo GenSyncInfo(bool net)
  104. {
  105. return GenSyncItemInfo(net, null);
  106. }
  107. public SyncItemInfo GenSyncItemInfo(bool net, string playerId)
  108. {
  109. mSyncInfo.ItemExpireTimeMS = this.ExpireTimeMS;
  110. mSyncInfo.isCanPick = IsCanPick(playerId);
  111. return mSyncInfo;
  112. }
  113. //针对拾取人数上限控制是否移除
  114. public bool IsPickAll()
  115. {
  116. if(this.Info.maxPickPlayers > 0 && this.Info.maxPickTimes > 0)
  117. {
  118. return this.TotalPickTimes >= (this.Info.maxPickPlayers * this.Info.maxPickTimes);
  119. }
  120. return false;
  121. }
  122. public bool IsCanPick(String playerId)
  123. {
  124. if (this.unitPickInfo == null)
  125. {
  126. return true;
  127. }
  128. if (this.Info.maxPickTimes > 0)
  129. {
  130. if (playerId != null && this.unitPickInfo.ContainsKey(playerId))
  131. {
  132. return this.unitPickInfo.Get(playerId) < this.Info.maxPickTimes;
  133. }
  134. else
  135. {
  136. return this.Info.maxPickPlayers == 0 || this.unitPickInfo.Count < this.Info.maxPickPlayers;
  137. }
  138. }
  139. return this.unitPickInfo.Count < this.Info.maxPickPlayers || (playerId != null && this.unitPickInfo.ContainsKey(playerId));
  140. }
  141. protected override void onAdded(bool pointLv)
  142. {
  143. this.mSyncInfo.x = X;
  144. this.mSyncInfo.y = Y;
  145. this.mSyncInfo.ObjectID = base.ID;
  146. this.mSyncInfo.direction = Direction;
  147. this.mSyncInfo.Alias = this.Alias;
  148. this.mSyncInfo.Name = this.DisPlayName;
  149. }
  150. protected override void Disposing()
  151. {
  152. base.Disposing();
  153. mViewTrigger.Dispose();
  154. }
  155. protected override void onRemoved()
  156. {
  157. }
  158. protected override void onUpdate(bool slowRefresh)
  159. {
  160. if (IsPaused) { return; }
  161. if (mRemovedExpire != null && mRemovedExpire.Update(Parent.UpdateIntervalMS))
  162. {
  163. Parent.RemoveObject(this);
  164. }
  165. if (mViewTriggerTimer != null && mViewTriggerTimer.Update(Parent.UpdateIntervalMS))
  166. {
  167. mViewTrigger.Enable = true;
  168. mViewTrigger.onLookUpdate(X, Y);
  169. }
  170. }
  171. public virtual bool IsPickable(InstanceUnit u)
  172. {
  173. if (this.Info.OnlyForShow)
  174. {
  175. return false;
  176. }
  177. if (!this.mPosValid)
  178. {
  179. return false;
  180. }
  181. if(u.IsDead())
  182. {
  183. return false;
  184. }
  185. /*if (!mData.Pickable)//此参数为是否可手动拾取,而非是否能拾取
  186. {
  187. return false;
  188. }*/
  189. if (mData.PlayerOnly && (!u.IsPlayer))
  190. {
  191. return false;
  192. }
  193. if (!this.Enable)
  194. {
  195. return false;
  196. }
  197. if (mData.DropForAll || Force == u.Force)
  198. {
  199. if (tryPickItem(u) && Parent.cb_unitTryPickItem(u, this))
  200. {
  201. return true;
  202. }
  203. }
  204. return false;
  205. }
  206. public bool DirectPickItem(InstanceUnit u,System.Action action = null)
  207. {
  208. if (IsPickable(u))
  209. {
  210. Parent.cb_unitFinishPickItem(u, this);
  211. bool remove;
  212. int pickTimes;
  213. if (u.doGotInstanceItem(this, out remove, out pickTimes))
  214. {
  215. if (Info.GotEffectSelf != null)
  216. {
  217. Parent.queueEvent(new AddEffectEvent(this.ID, X, Y, Direction, Info.GotEffectSelf));
  218. }
  219. u.queueEvent(new UnitGotInstanceItemEvent(u.ID, this.ID));
  220. if (remove)
  221. {
  222. if(action != null)
  223. {
  224. action.Invoke();
  225. }
  226. mViewTrigger.Enable = false;
  227. Parent.RemoveObject(this);
  228. }
  229. }
  230. return true;
  231. }
  232. return false;
  233. }
  234. public bool PickItem(InstanceUnit u)
  235. {
  236. if (IsPickable(u))
  237. {
  238. if (Parent.TouchObject2(u, this))
  239. {
  240. if (tryPickItem(u) && Parent.cb_unitTryPickItem(u, this))
  241. {
  242. u.startPickProgressObject(this, mData.PickTimeMS, (unit, pickable) =>
  243. {
  244. if (this.Enable)
  245. {
  246. Parent.cb_unitFinishPickItem(u, this);
  247. bool remove;
  248. int pickTimes;
  249. if (u.doGotInstanceItem(this, out remove, out pickTimes))
  250. {
  251. if (Info.GotEffectSelf != null)
  252. {
  253. Parent.queueEvent(new AddEffectEvent(this.ID , X, Y, Direction, Info.GotEffectSelf));
  254. }
  255. InstancePlayer player = (u as InstancePlayer);
  256. int flag = this.IsCanPick(player == null ? null : player.PlayerUUID) ? 0 : 1;
  257. u.queueEvent(new UnitGotInstanceItemEvent(u.ID, this.ID, (byte)flag));
  258. //如果标记移除,或者拾取人数达上限
  259. if (remove || this.IsPickAll())
  260. {
  261. mViewTrigger.Enable = false;
  262. Parent.RemoveObject(this);
  263. }
  264. }
  265. }
  266. });
  267. return true;
  268. }
  269. }
  270. }
  271. return false;
  272. }
  273. private bool tryPickItem(InstanceUnit unit)
  274. {
  275. if (!Parent.IsVisibleAOI(unit, this))
  276. {
  277. return false;
  278. }
  279. bool ret = true;
  280. if (mTryPickItem != null)
  281. {
  282. foreach (TryPickItem trypick in mTryPickItem.GetInvocationList())
  283. {
  284. if (!trypick.Invoke(this, unit))
  285. {
  286. ret = false;
  287. }
  288. }
  289. }
  290. return ret;
  291. }
  292. #region _Collides_
  293. void ViewTrigger.ViewTriggerListener.onObjectEnterView(ViewTrigger src, InstanceZoneObject obj)
  294. {
  295. if (!mData.Pickable && !mData.OnlyForShow)
  296. {
  297. InstanceUnit u = obj as InstanceUnit;
  298. if (mData.PlayerOnly && (!u.IsPlayer))
  299. {
  300. return;
  301. }
  302. if ((mData.DropForAll || Force == u.Force) && this.IsPickable(u))
  303. {
  304. bool remove;
  305. int pickTimes;
  306. if (u.doGotInstanceItem(this, out remove, out pickTimes))
  307. {
  308. u.NotifyRemoveDropItemB2C(this);
  309. if (Info.GotEffectSelf != null)
  310. {
  311. Parent.queueEvent(new AddEffectEvent(this.ID, X, Y, Direction, Info.GotEffectSelf));
  312. }
  313. u.queueEvent(new UnitGotInstanceItemEvent(u.ID, this.ID));
  314. if (remove)
  315. {
  316. mViewTrigger.Enable = false;
  317. Parent.RemoveObject(this);
  318. }
  319. }
  320. }
  321. }
  322. }
  323. void ViewTrigger.ViewTriggerListener.onObjectLeaveView(ViewTrigger src, InstanceZoneObject obj)
  324. {
  325. }
  326. bool ViewTrigger.ViewTriggerListener.select(ViewTrigger src, InstanceZoneObject obj)
  327. {
  328. if (obj == this)
  329. {
  330. return false;
  331. }
  332. else if (obj is InstanceUnit)
  333. {
  334. return true;
  335. }
  336. return false;
  337. }
  338. public override String GetTemplateData()
  339. {
  340. return "Item:" + this.Info.ID;
  341. }
  342. #endregion
  343. #region _API_
  344. public ItemTemplate get_template()
  345. {
  346. return mData;
  347. }
  348. public bool is_attribute(string key)
  349. {
  350. return IsAttribute(key);
  351. }
  352. public void set_attribute(string key, object value)
  353. {
  354. SetAttribute(key, value);
  355. }
  356. public object get_attribute(string key)
  357. {
  358. return GetAttribute(key);
  359. }
  360. #endregion
  361. #region _Delegate_
  362. /// <summary>
  363. /// 单位尝试检取道具监听,
  364. /// 返回False禁止检取
  365. /// </summary>
  366. /// <returns></returns>
  367. public delegate bool TryPickItem(InstanceItem item, InstanceUnit unit);
  368. private TryPickItem mTryPickItem;
  369. public event TryPickItem OnTryPickItem { add { mTryPickItem += value; } remove { mTryPickItem -= value; } }
  370. protected override void clearEvents()
  371. {
  372. base.clearEvents();
  373. this.mTryPickItem = null;
  374. }
  375. #endregion
  376. }
  377. }