50 lines
1.3 KiB
C#
50 lines
1.3 KiB
C#
using UnityEngine;
|
|
using UnityGameFramework.Runtime;
|
|
|
|
namespace SepCore.Entity
|
|
{
|
|
public class DropEntity : EntityBase
|
|
{
|
|
private DropData _dropData;
|
|
private bool _isCollected;
|
|
|
|
protected override void OnShow(object userData)
|
|
{
|
|
base.OnShow(userData);
|
|
_isCollected = false;
|
|
|
|
if (userData is DropData dropData)
|
|
{
|
|
_dropData = dropData;
|
|
}
|
|
else
|
|
{
|
|
Log.Error("DropEntity.OnShow() called with wrong type: {0}.", userData?.GetType().Name);
|
|
}
|
|
}
|
|
|
|
public bool TryCollect(Player player, float collectDistance = 0f)
|
|
{
|
|
if (_isCollected || player == null || !player.Available || _dropData == null)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if (collectDistance > 0f)
|
|
{
|
|
float distance = Vector3.Distance(player.CachedTransform.position, CachedTransform.position);
|
|
if (distance > collectDistance)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
_isCollected = true;
|
|
player.Coin += _dropData.CoinCount;
|
|
player.Exp += _dropData.ExpCount;
|
|
GameEntry.Entity.HideEntity(this);
|
|
return true;
|
|
}
|
|
}
|
|
}
|