Files
vampire-like/Assets/GameMain/Scripts/Runtime/EntityLogic/DropEntity.cs
T

58 lines
1.5 KiB
C#

using SepCore.Entity;
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);
}
CachedTransform.localScale = new Vector3(0.3f, 0.3f, 0.3f);
}
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;
}
private void OnTriggerEnter(Collider other)
{
TryCollect(other.GetComponent<Player>());
}
}
}