83 lines
3.2 KiB
C#
83 lines
3.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Sirenix.OdinInspector;
|
|
using UnityEngine;
|
|
using Random = UnityEngine.Random;
|
|
|
|
namespace BlueWater.Items
|
|
{
|
|
public class ItemManager : Singleton<ItemManager>
|
|
{
|
|
[SerializeField, Required]
|
|
private Item _defaultItemPrefab;
|
|
|
|
[field: SerializeField, Required]
|
|
public ItemDataSo ItemDataSo { get; private set; }
|
|
|
|
[field: SerializeField, Required]
|
|
public ItemDropTableSo ItemDropTableSo { get; private set; }
|
|
|
|
[field: SerializeField, Required]
|
|
public FoodDataSo FoodDataSo { get; private set; }
|
|
|
|
[field: SerializeField, Required]
|
|
public CocktailDataSo CocktailDataSo { get; private set; }
|
|
|
|
[field: SerializeField, Required]
|
|
public LiquidDataSo LiquidDataSo { get; private set; }
|
|
|
|
[field: SerializeField, Required]
|
|
public ItemSlotDataSo ItemSlotDataSo { get; private set; }
|
|
|
|
[Title("드롭 아이템 설정")]
|
|
[SerializeField]
|
|
private float _randomDropRadius = 3f;
|
|
|
|
[SerializeField]
|
|
private float _minSeparationDistance = 1.5f;
|
|
|
|
private const int MaxAttempts = 1000;
|
|
|
|
public void ItemDropRandomPosition(string idx, Vector3 dropPosition, float randomDropRadius = float.PositiveInfinity)
|
|
{
|
|
var itemDropTable = ItemDropTableSo.GetDataByIdx(idx);
|
|
if (itemDropTable == null) return;
|
|
|
|
var droppedItemList = itemDropTable.GetDroppedItemList();
|
|
var droppedPositions = new List<Vector3>();
|
|
foreach (var element in droppedItemList)
|
|
{
|
|
Vector3 newDropPosition;
|
|
var attempts = 0;
|
|
do
|
|
{
|
|
var radius = float.IsPositiveInfinity(randomDropRadius) ? _randomDropRadius : randomDropRadius;
|
|
attempts++;
|
|
var newDropPositionX = Random.Range(dropPosition.x - radius, dropPosition.x + radius);
|
|
var newDropPositionZ = Random.Range(dropPosition.z - radius, dropPosition.z + radius);
|
|
newDropPosition = new Vector3(newDropPositionX, dropPosition.y, newDropPositionZ);
|
|
} while (!IsDropPositionAvailable(newDropPosition, droppedPositions) && attempts < MaxAttempts);
|
|
|
|
droppedPositions.Add(newDropPosition);
|
|
|
|
var itemPrefab = ItemDataSo.GetDataByIdx(element.Idx).ItemPrefab;
|
|
if (!itemPrefab)
|
|
{
|
|
itemPrefab = _defaultItemPrefab;
|
|
}
|
|
|
|
var instantiateItem = Instantiate(itemPrefab, dropPosition, itemPrefab.transform.rotation);
|
|
instantiateItem.Initialize(element);
|
|
|
|
var pushPower = Vector3.up * 30f + (newDropPosition - dropPosition) * 10f;
|
|
instantiateItem.AddForce(pushPower, ForceMode.Impulse);
|
|
}
|
|
}
|
|
|
|
private bool IsDropPositionAvailable(Vector3 position, List<Vector3> positions)
|
|
{
|
|
return positions.Any(pos => Vector3.Distance(position, pos) > _minSeparationDistance);
|
|
}
|
|
}
|
|
} |