DropItemAuto.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using DG.Tweening;
  5. public class DropItemFollow : MonoBehaviour
  6. {
  7. public enum AbsorbEffType
  8. {
  9. empty = 0,
  10. glod = 1,
  11. }
  12. public GameObject target = null;
  13. public AbsorbEffType effType = AbsorbEffType.empty;
  14. private void Update()
  15. {
  16. if(target)
  17. {
  18. this.transform.DOMove(target.transform.position, 1.0f);
  19. if(Vector3.Distance( this.transform.position, target.transform.position) < 1.0f)
  20. {
  21. // 增加拾取特效
  22. if(effType == AbsorbEffType.glod)
  23. {
  24. ResourceHelper.Instance.LoadAssetBundle("jinbi_shouji_tishi", (ab) =>
  25. {
  26. if (ab != null)
  27. {
  28. GameObject eff = (GameObject)Instantiate(ab.LoadAsset("jinbi_shouji_tishi"));
  29. eff.transform.parent = target.transform.Find("FBX/Effect_000/Top").transform;
  30. eff.transform.localPosition = Vector3.zero;
  31. }
  32. });
  33. }
  34. GameObject.Destroy(this.gameObject);
  35. }
  36. }
  37. }
  38. }
  39. public class DropItemAuto : MonoBehaviour
  40. {
  41. /// <summary>
  42. /// 方向
  43. /// </summary>
  44. private Vector3 mDir = Vector3.zero;
  45. public float mSpeedX = 2;
  46. public float mSpeedY = 10;
  47. public float gravity = 20.0F;
  48. private Vector3 moveDirection = Vector3.zero;
  49. private bool mFly = false;
  50. private bool mUpdate = false;
  51. public ItemVo mItemVo = null;
  52. /// <summary>
  53. /// 自身
  54. /// </summary>
  55. private Transform mTransform = null;
  56. /// <summary>
  57. /// 目标
  58. /// </summary>
  59. private Transform mTargetTransform = null;
  60. private Tween mtweener = null;
  61. private bool tweenFly = false;
  62. private Role role = null;
  63. private GameObject eff = null;
  64. public void Init(Transform role, ItemVo vo)
  65. {
  66. mTargetTransform = role;
  67. mTransform = transform;
  68. mFly = false;
  69. mUpdate = false;
  70. mFly = false;
  71. mUpdate = false;
  72. mItemVo = vo;
  73. if ((EItemSubType)mItemVo.nMo.subType == EItemSubType.BattleItem_HP || (EItemSubType)mItemVo.nMo.subType == EItemSubType.BattleItem_MP)
  74. {
  75. // do nothing
  76. }
  77. else
  78. {
  79. Invoke("LateMove", 6.0f);
  80. }
  81. InvokeRepeating("CheckPlayer", 1.0f, 0.3f);
  82. Invoke("LateDestroy", 30);
  83. }
  84. public void Init(ItemVo vo)
  85. {
  86. mFly = false;
  87. mUpdate = false;
  88. mItemVo = vo;
  89. InvokeRepeating("CheckPlayer", 0.1f, 0.3f);
  90. Invoke("LateDestroy", 30);
  91. }
  92. /// <summary>
  93. /// 延迟销毁
  94. /// </summary>
  95. private void LateDestroy()
  96. {
  97. DropManager.Instance.RemoveDropItem(this);
  98. }
  99. private void OnDisable()
  100. {
  101. if (IsInvoking("CheckPlayer"))
  102. {
  103. CancelInvoke("CheckPlayer");
  104. }
  105. }
  106. /// <summary>
  107. /// 检测玩家
  108. /// </summary>
  109. private void CheckPlayer()
  110. {
  111. if(tweenFly)
  112. {
  113. return;
  114. }
  115. // 检测身边是否有道具
  116. Collider[] colliders = Physics.OverlapSphere(transform.position, 1.5f, 1 << LayerMask.NameToLayer("Player"));
  117. if (colliders != null && colliders.Length > 0)
  118. {
  119. for (int i = 0; i < colliders.Length; i++)
  120. {
  121. role = colliders[i].GetComponent<Role>();
  122. if(role == null || mItemVo == null)
  123. {
  124. continue;
  125. }
  126. if ((EItemSubType)mItemVo.nMo.subType == EItemSubType.BattleItem_HP)
  127. {
  128. role.mData.UpdateHP(mItemVo.nMo.GetPillsExt().number, true);
  129. role.UpdateHP(mItemVo.nMo.GetPillsExt().number);
  130. AudioManager.Instance.PlayUISoundEffect("捡起药水");
  131. }
  132. else if ((EItemSubType)mItemVo.nMo.subType == EItemSubType.BattleItem_MP)
  133. {
  134. role.mData.UpdateMP(mItemVo.nMo.GetPillsExt().number, true);
  135. AudioManager.Instance.PlayUISoundEffect("捡起药水");
  136. }
  137. else
  138. {
  139. ItemProxy.Instance.AddItem(int.Parse(mItemVo.typeId), mItemVo.count);
  140. if ((EItemSubType)mItemVo.nMo.subType == EItemSubType.Gold)
  141. {
  142. AudioManager.Instance.PlayUISoundEffect("捡起金币");
  143. }
  144. else if ((EItemSubType)mItemVo.nMo.subType == EItemSubType.Gem)
  145. {
  146. AudioManager.Instance.PlayUISoundEffect("捡起宝石");
  147. }
  148. else if ((EItemSubType)mItemVo.nMo.subType == EItemSubType.Weapon ||
  149. (EItemSubType)mItemVo.nMo.subType == EItemSubType.Segment ||
  150. (EItemSubType)mItemVo.nMo.subType == EItemSubType.YanlingBookSegement ||
  151. (EItemSubType)mItemVo.nMo.subType == EItemSubType.Hunqi ||
  152. (EItemSubType)mItemVo.nMo.subType == EItemSubType.YanLingAdvancedStone ||
  153. (EItemSubType)mItemVo.nMo.subType == EItemSubType.WuqiAdvancedStone ||
  154. (EItemSubType)mItemVo.nMo.subType == EItemSubType.Box)
  155. {
  156. AudioManager.Instance.PlayUISoundEffect("捡起装备");
  157. }
  158. else
  159. {
  160. AudioManager.Instance.PlayUISoundEffect("捡起材料");
  161. }
  162. }
  163. tweenFly = true;
  164. // 增加拾取特效
  165. ResourceHelper.Instance.LoadAssetBundle("shiqu_tuowei",(ab)=>
  166. {
  167. if (ab != null )
  168. {
  169. eff = (GameObject)Instantiate(ab.LoadAsset("shiqu_tuowei"));
  170. eff.transform.localPosition = this.transform.position;
  171. //CmptAutoDestory des = eff.AddComponent<CmptAutoDestory>();
  172. //des.LiveTime = 2.0f;
  173. var s = DOTween.Sequence();
  174. eff.transform.DOMove(this.transform.position + Vector3.up * 4, 0.7f).OnComplete(()=>
  175. {
  176. DropItemFollow follow = eff.AddComponent<DropItemFollow>();
  177. follow.target = role.gameObject;
  178. if ((EItemSubType)mItemVo.nMo.subType == EItemSubType.Gold)
  179. {
  180. follow.effType = DropItemFollow.AbsorbEffType.glod;
  181. }
  182. });
  183. //mtweener = eff.transform.DOMove(this.transform.position + Vector3.up * 5, 1.0f);
  184. //mtweener.OnComplete(()=>
  185. //{
  186. // tweenFly = true;
  187. //});
  188. DropManager.Instance.RemoveDropItem(this);
  189. }
  190. });
  191. return;
  192. }
  193. }
  194. }
  195. /// <summary>
  196. /// 延迟开始移动
  197. /// </summary>
  198. private void LateMove()
  199. {
  200. mFly = true;
  201. mUpdate = true;
  202. // 隐藏道具名称
  203. HpbarManager.Instance.HideItemName(transform);
  204. }
  205. }