using System.Collections; using System.Collections.Generic; using UnityEngine; using DG.Tweening; public class DropItemFollow : MonoBehaviour { public enum AbsorbEffType { empty = 0, glod = 1, } public GameObject target = null; public AbsorbEffType effType = AbsorbEffType.empty; private void Update() { if(target) { this.transform.DOMove(target.transform.position, 1.0f); if(Vector3.Distance( this.transform.position, target.transform.position) < 1.0f) { // 增加拾取特效 if(effType == AbsorbEffType.glod) { ResourceHelper.Instance.LoadAssetBundle("jinbi_shouji_tishi", (ab) => { if (ab != null) { GameObject eff = (GameObject)Instantiate(ab.LoadAsset("jinbi_shouji_tishi")); eff.transform.parent = target.transform.Find("FBX/Effect_000/Top").transform; eff.transform.localPosition = Vector3.zero; } }); } GameObject.Destroy(this.gameObject); } } } } public class DropItemAuto : MonoBehaviour { /// /// 方向 /// private Vector3 mDir = Vector3.zero; public float mSpeedX = 2; public float mSpeedY = 10; public float gravity = 20.0F; private Vector3 moveDirection = Vector3.zero; private bool mFly = false; private bool mUpdate = false; public ItemVo mItemVo = null; /// /// 自身 /// private Transform mTransform = null; /// /// 目标 /// private Transform mTargetTransform = null; private Tween mtweener = null; private bool tweenFly = false; private Role role = null; private GameObject eff = null; public void Init(Transform role, ItemVo vo) { mTargetTransform = role; mTransform = transform; mFly = false; mUpdate = false; mFly = false; mUpdate = false; mItemVo = vo; if ((EItemSubType)mItemVo.nMo.subType == EItemSubType.BattleItem_HP || (EItemSubType)mItemVo.nMo.subType == EItemSubType.BattleItem_MP) { // do nothing } else { Invoke("LateMove", 6.0f); } InvokeRepeating("CheckPlayer", 1.0f, 0.3f); Invoke("LateDestroy", 30); } public void Init(ItemVo vo) { mFly = false; mUpdate = false; mItemVo = vo; InvokeRepeating("CheckPlayer", 0.1f, 0.3f); Invoke("LateDestroy", 30); } /// /// 延迟销毁 /// private void LateDestroy() { DropManager.Instance.RemoveDropItem(this); } private void OnDisable() { if (IsInvoking("CheckPlayer")) { CancelInvoke("CheckPlayer"); } } /// /// 检测玩家 /// private void CheckPlayer() { if(tweenFly) { return; } // 检测身边是否有道具 Collider[] colliders = Physics.OverlapSphere(transform.position, 1.5f, 1 << LayerMask.NameToLayer("Player")); if (colliders != null && colliders.Length > 0) { for (int i = 0; i < colliders.Length; i++) { role = colliders[i].GetComponent(); if(role == null || mItemVo == null) { continue; } if ((EItemSubType)mItemVo.nMo.subType == EItemSubType.BattleItem_HP) { role.mData.UpdateHP(mItemVo.nMo.GetPillsExt().number, true); role.UpdateHP(mItemVo.nMo.GetPillsExt().number); AudioManager.Instance.PlayUISoundEffect("捡起药水"); } else if ((EItemSubType)mItemVo.nMo.subType == EItemSubType.BattleItem_MP) { role.mData.UpdateMP(mItemVo.nMo.GetPillsExt().number, true); AudioManager.Instance.PlayUISoundEffect("捡起药水"); } else { ItemProxy.Instance.AddItem(int.Parse(mItemVo.typeId), mItemVo.count); if ((EItemSubType)mItemVo.nMo.subType == EItemSubType.Gold) { AudioManager.Instance.PlayUISoundEffect("捡起金币"); } else if ((EItemSubType)mItemVo.nMo.subType == EItemSubType.Gem) { AudioManager.Instance.PlayUISoundEffect("捡起宝石"); } else if ((EItemSubType)mItemVo.nMo.subType == EItemSubType.Weapon || (EItemSubType)mItemVo.nMo.subType == EItemSubType.Segment || (EItemSubType)mItemVo.nMo.subType == EItemSubType.YanlingBookSegement || (EItemSubType)mItemVo.nMo.subType == EItemSubType.Hunqi || (EItemSubType)mItemVo.nMo.subType == EItemSubType.YanLingAdvancedStone || (EItemSubType)mItemVo.nMo.subType == EItemSubType.WuqiAdvancedStone || (EItemSubType)mItemVo.nMo.subType == EItemSubType.Box) { AudioManager.Instance.PlayUISoundEffect("捡起装备"); } else { AudioManager.Instance.PlayUISoundEffect("捡起材料"); } } tweenFly = true; // 增加拾取特效 ResourceHelper.Instance.LoadAssetBundle("shiqu_tuowei",(ab)=> { if (ab != null ) { eff = (GameObject)Instantiate(ab.LoadAsset("shiqu_tuowei")); eff.transform.localPosition = this.transform.position; //CmptAutoDestory des = eff.AddComponent(); //des.LiveTime = 2.0f; var s = DOTween.Sequence(); eff.transform.DOMove(this.transform.position + Vector3.up * 4, 0.7f).OnComplete(()=> { DropItemFollow follow = eff.AddComponent(); follow.target = role.gameObject; if ((EItemSubType)mItemVo.nMo.subType == EItemSubType.Gold) { follow.effType = DropItemFollow.AbsorbEffType.glod; } }); //mtweener = eff.transform.DOMove(this.transform.position + Vector3.up * 5, 1.0f); //mtweener.OnComplete(()=> //{ // tweenFly = true; //}); DropManager.Instance.RemoveDropItem(this); } }); return; } } } /// /// 延迟开始移动 /// private void LateMove() { mFly = true; mUpdate = true; // 隐藏道具名称 HpbarManager.Instance.HideItemName(transform); } }