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 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);
|
}
|
}
|
}
|
|
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);
|
}
|
|
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);
|
}
|
}
|
}
|