using System.Collections;
|
using System.Collections.Generic;
|
using UnityEngine;
|
|
public class ClientDropItemUtility : Singleton<ClientDropItemUtility>
|
{
|
public static UnityEngine.Events.UnityAction<int> OnItemPickup;
|
private const float OFFSET = 1;
|
private int m_TotalDropCount;
|
private int m_DropedCount;
|
private Vector3 m_DropPos;
|
private Vector3 m_Dir;
|
private List<int> m_IndexList = new List<int>();
|
|
private List<DropItem> m_DropItemList = new List<DropItem>();
|
|
public bool TryGetDropItem(out DropItem item)
|
{
|
var _hero = PlayerDatas.Instance.hero;
|
|
if (_hero == null || m_DropItemList.Count == 0)
|
{
|
item = null;
|
return false;
|
}
|
|
m_DropItemList.Sort((i1, i2) =>
|
{
|
var _d1 = MathUtility.DistanceSqrtXZ(_hero.Pos, i1.transform.position);
|
var _d2 = MathUtility.DistanceSqrtXZ(_hero.Pos, i2.transform.position);
|
if (i1.AutoPickUp && !i2.AutoPickUp)
|
{
|
return -1;
|
}
|
else if (i2.AutoPickUp && !i1.AutoPickUp)
|
{
|
return 1;
|
}
|
else
|
{
|
if (_d2 > _d1)
|
{
|
return -1;
|
}
|
else if (_d2 < _d1)
|
{
|
return 1;
|
}
|
return 0;
|
}
|
});
|
|
if (m_DropItemList[0].AutoPickUp)
|
{
|
item = null;
|
return false;
|
}
|
else
|
{
|
item = m_DropItemList[0];
|
return true;
|
}
|
}
|
|
public bool HasDropItem()
|
{
|
return m_DropItemList.Count > 0;
|
}
|
|
public void Update()
|
{
|
var _hero = PlayerDatas.Instance.hero;
|
if (_hero == null)
|
{
|
return;
|
}
|
|
float _dis;
|
DropItem _item;
|
for (int i = m_DropItemList.Count - 1; i >= 0; --i)
|
{
|
_item = m_DropItemList[i];
|
_dis = MathUtility.DistanceSqrtXZ(_item.transform.position, _hero.Pos);
|
if (_dis < .25f)
|
{
|
if (OnItemPickup != null)
|
{
|
OnItemPickup(_item.itemId);
|
}
|
|
Release(_item);
|
}
|
}
|
|
CheckAndRemoveOutdatedJiaDropInfo();
|
}
|
|
public void ReleaseAll()
|
{
|
for (int i = m_DropItemList.Count - 1; i >= 0; --i)
|
{
|
Release(m_DropItemList[i]);
|
}
|
}
|
|
public void Release(DropItem item)
|
{
|
DropItem.Reycle(item);
|
m_DropItemList.Remove(item);
|
}
|
|
private float m_LastDropTime = 0;
|
|
public void DropInShortTime(Vector3 center, int itemID)
|
{
|
Vector3 _nextDir;
|
Vector3 _nextPos;
|
int _x;
|
int _z;
|
int _index;
|
|
if (Time.time - m_LastDropTime > 0.5f)
|
{
|
m_LastDropTime = Time.time;
|
|
m_IndexList.Clear();
|
_nextDir = m_Dir = Vector3.forward;
|
_nextPos = m_DropPos = center;
|
}
|
else
|
{
|
_nextDir = MathUtility.Rotate90_XZ_CW(m_Dir);
|
_nextPos = m_DropPos + _nextDir * OFFSET;
|
}
|
|
_x = Mathf.CeilToInt(_nextPos.x * 100000);
|
_z = Mathf.CeilToInt(_nextPos.z * 10);
|
_index = _x + _z;
|
|
if (m_IndexList.Contains(_index))
|
{
|
m_DropPos = m_DropPos + m_Dir * OFFSET;
|
|
_x = Mathf.CeilToInt(m_DropPos.x * 100000);
|
_z = Mathf.CeilToInt(m_DropPos.z * 10);
|
_index = _x + _z;
|
}
|
else
|
{
|
m_Dir = _nextDir;
|
m_DropPos = _nextPos;
|
}
|
|
m_IndexList.Add(_index);
|
|
float _time = 2f;
|
if (PlayerDatas.Instance.baseData.MapID == 31190)
|
{
|
_time = 3f;
|
}
|
|
RecordJiaDrop(itemID, Time.time);
|
var _item = DropItem.DropJia(itemID, m_DropPos, CameraController.Instance.CameraObject, _time);
|
m_DropItemList.Add(_item);
|
}
|
|
public void RemoveDropItem(DropItem item)
|
{
|
if (m_DropItemList.Contains(item))
|
{
|
m_DropItemList.Remove(item);
|
}
|
}
|
|
public void Drop(Vector3 center, int[] itemIds)
|
{
|
if (itemIds == null || itemIds.Length == 0)
|
{
|
return;
|
}
|
|
RaycastHit _hit;
|
if (!Physics.Raycast(center + new Vector3(0, 50, 0),
|
Vector3.down,
|
out _hit,
|
80,
|
LayerUtility.WalkbleMask))
|
{
|
return;
|
}
|
|
m_IndexList.Clear();
|
m_DropPos = center;
|
m_DropedCount = 0;
|
m_TotalDropCount = itemIds.Length;
|
m_Dir = Vector3.forward;
|
|
DropItem _item = DropItem.Drop(itemIds[m_DropedCount],
|
_hit.point,
|
CameraController.Instance.CameraObject);
|
m_DropItemList.Add(_item);
|
m_DropedCount += 1;
|
|
int _x = Mathf.CeilToInt(m_DropPos.x * 100000);
|
int _z = Mathf.CeilToInt(m_DropPos.z * 10);
|
m_IndexList.Add(_x + _z);
|
|
DropNext(itemIds);
|
}
|
|
public void DropNext(int[] itemIds)
|
{
|
if (m_DropedCount < m_TotalDropCount)
|
{
|
var _nextDir = MathUtility.Rotate90_XZ_CW(m_Dir);
|
var _nextPos = m_DropPos + _nextDir * OFFSET;
|
int _x = Mathf.CeilToInt(_nextPos.x * 100000);
|
int _z = Mathf.CeilToInt(_nextPos.z * 10);
|
int _index = _x + _z;
|
|
if (m_IndexList.Contains(_index))
|
{
|
m_DropPos = m_DropPos + m_Dir * OFFSET;
|
|
_x = Mathf.CeilToInt(m_DropPos.x * 100000);
|
_z = Mathf.CeilToInt(m_DropPos.z * 10);
|
_index = _x + _z;
|
}
|
else
|
{
|
m_Dir = _nextDir;
|
m_DropPos = _nextPos;
|
}
|
|
m_IndexList.Add(_index);
|
|
RaycastHit _hit;
|
if (Physics.Raycast(m_DropPos + new Vector3(0, 50, 0),
|
Vector3.down,
|
out _hit,
|
80,
|
LayerUtility.WalkbleMask))
|
{
|
DropItem _item = DropItem.Drop(itemIds[m_DropedCount],
|
_hit.point,
|
CameraController.Instance.CameraObject);
|
m_DropItemList.Add(_item);
|
m_DropedCount += 1;
|
}
|
|
DropNext(itemIds);
|
}
|
}
|
|
List<JiaDropInfo> jiaDropInfos = new List<JiaDropInfo>();
|
|
void RecordJiaDrop(int itemId, float time)
|
{
|
jiaDropInfos.Add(new JiaDropInfo() { itemId = itemId, dropTime = time });
|
}
|
|
public float GetJiaDropItemShowDelay(float itemId)
|
{
|
if (jiaDropInfos.IsNullOrEmpty())
|
{
|
return 0f;
|
}
|
|
var info = jiaDropInfos.Find(x => { return x.itemId == itemId; });
|
if (info.itemId != 0 && info.dropTime + 2.5f > Time.time)
|
{
|
return info.dropTime + 2.5f - Time.time;
|
}
|
else
|
{
|
return 0f;
|
}
|
}
|
|
void CheckAndRemoveOutdatedJiaDropInfo()
|
{
|
for (var i = jiaDropInfos.Count - 1; i >= 0; i--)
|
{
|
var info = jiaDropInfos[i];
|
if (Time.time - info.dropTime > 5f)
|
{
|
jiaDropInfos.RemoveAt(i);
|
}
|
}
|
}
|
|
public struct JiaDropInfo
|
{
|
public int itemId;
|
public float dropTime;
|
}
|
|
}
|