DropItem2D.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4. public class DropItem2D
  5. {
  6. private DropItemType itemType;
  7. private Vector3 fromPos;
  8. private Vector3 toPos;
  9. private GameObject itemGo;
  10. private RectTransform rt = null;
  11. private TweenPosition tp = null;
  12. private bool bDroped = false;
  13. private string prefabName = "";
  14. private float lifeTime = 0.5f;
  15. private float delayTime = 0.3f;
  16. TweenScale ts = null;
  17. public DropItem2D(Vector3 worldPos, DropItemType type)
  18. {
  19. prefabName = BattleDropMgr.Instance.GetItemPrefabName(type);
  20. itemType = type;
  21. Vector3 tempPos = BattleCamera.Instance.RealCamera.WorldToViewportPoint(worldPos);
  22. itemGo = ResourceMgr.Instance.GetGoFromPool(Constants.UIPath, prefabName);
  23. fromPos = new Vector3((tempPos.x - 0.5f) * UIMgr.SCREEN_WIDTH, (tempPos.y - 0.5f) * UIMgr.SCREEN_HEIGHT, 0);
  24. bDroped = false;
  25. toPos = BattleMgr.s_reward_pos;
  26. lifeTime = GlobalConfig.Instance.GetConfigFloatValue(82);
  27. delayTime = GlobalConfig.Instance.GetConfigFloatValue(81);
  28. InitItem();
  29. BeginMove(0.0f);
  30. }
  31. public DropItem2D(Vector3 pos,Vector3 toPos,float minRangeX,float maxRangeX,float minRangeY,float maxRangeY,float lifeTime,DropItemType type,string icon = "")
  32. {
  33. itemType = type;
  34. prefabName = BattleDropMgr.Instance.GetItemPrefabName(type);
  35. itemGo = ResourceMgr.Instance.GetGoFromPool(Constants.UIPath, prefabName);
  36. this.lifeTime = lifeTime;
  37. //Vector3 tempPos = pos;
  38. //tempPos.x += Random.Range(minRangeX, maxRangeX);
  39. //tempPos.y += Random.Range(minRangeY, maxRangeY);
  40. this.fromPos = pos;
  41. this.toPos = toPos;
  42. bDroped = false;
  43. delayTime = 0.0f;
  44. InitItem();
  45. LoadItemIcon(icon);
  46. //BeginMove();
  47. if (itemGo)
  48. {
  49. Vector3 tempPos = pos;
  50. tempPos.x += Random.Range(minRangeX, maxRangeX);
  51. tp = TweenPosition.Begin(itemGo, 0.06f, tempPos);
  52. if (tp != null)
  53. {
  54. float dur = 0.06f;
  55. tp.delay = 0;
  56. tp.duration = dur;
  57. TimerManager.Instance.AddTimer((int)(dur * 1000), 1, OnChangeDropPos);
  58. }
  59. }
  60. }
  61. public void BeginMove(float scale)
  62. {
  63. if (itemGo)
  64. {
  65. rt.localScale = new Vector3(1, 1, 1);
  66. ts = TweenScale.Begin(itemGo, this.lifeTime, new Vector3(scale, scale, scale));
  67. if (ts != null)
  68. {
  69. ts.delay = delayTime;
  70. ts.duration = this.lifeTime;
  71. ts.enabled = true;
  72. }
  73. tp = TweenPosition.Begin(itemGo, this.lifeTime, toPos);
  74. if (tp != null)
  75. {
  76. tp.delay = delayTime;
  77. tp.duration = this.lifeTime;
  78. tp.onFinished = OnFinished;
  79. }
  80. }
  81. }
  82. void LoadItemIcon(string itemIcon)
  83. {
  84. if (itemGo == null || itemType != DropItemType.ITEM || string.IsNullOrEmpty(itemIcon)) return;
  85. Transform iconTrans = itemGo.transform.Find("Item/icon");
  86. if (iconTrans == null) return;
  87. Image icon = iconTrans.GetComponent<Image>();
  88. icon.sprite = ResourceMgr.Instance.LoadAssetSync<Sprite>(Constants.IconDir, itemIcon);
  89. }
  90. void OnChangeDropPos(int timerSeq)
  91. {
  92. BeginMove(0.5f);
  93. }
  94. void InitItem()
  95. {
  96. if (itemGo != null)
  97. {
  98. itemGo.transform.SetParent(BattleDropMgr.Instance.Root.transform);
  99. itemGo.transform.localScale = Vector3.one;
  100. itemGo.transform.localRotation = Quaternion.identity;
  101. rt = itemGo.GetComponent<RectTransform>();
  102. rt.localScale = Vector3.one;
  103. rt.anchorMin = new Vector2(0.5f,0.5f);
  104. rt.anchorMax = new Vector2(0.5f, 0.5f);
  105. rt.pivot = Vector2.zero;
  106. rt.anchoredPosition3D = fromPos;
  107. ts = itemGo.GetComponent<TweenScale>();
  108. if (ts != null)
  109. ts.enabled = false;
  110. tp = itemGo.GetComponent<TweenPosition>();
  111. if (tp != null)
  112. tp.enabled = false;
  113. }
  114. }
  115. void OnFinished()
  116. {
  117. bDroped = true;
  118. }
  119. public bool IsValid(float deltatime)
  120. {
  121. if (itemGo == null) return false;
  122. return !bDroped;
  123. }
  124. public void Dispose()
  125. {
  126. if (itemGo != null)
  127. {
  128. itemGo.transform.localScale = Vector3.one;
  129. if (ts != null)
  130. {
  131. ts.enabled = false;
  132. }
  133. ResourceMgr.Instance.RecycleGO(Constants.UIPath, prefabName, itemGo);
  134. itemGo = null;
  135. }
  136. }
  137. }