您最多选择25个主题
主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
33 行
823 B
33 行
823 B
using UnityEngine;
|
|
|
|
public class HungerComponent : MonoBehaviour
|
|
{
|
|
[SerializeField] private float _eatAmount = 10f;
|
|
[SerializeField] private float _maxFullness = 100f;
|
|
[SerializeField] private float _hungerPerSecond = 1f;
|
|
[SerializeField] [Range(0.01f, 0.99f)] private float _isHungryThreshold = 0.4f;
|
|
private bool _getHungry;
|
|
private float _currentFullness;
|
|
public bool IsHungry => _currentFullness <= _maxFullness * _isHungryThreshold;
|
|
|
|
private void Awake()
|
|
{
|
|
_currentFullness = _maxFullness;
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (_getHungry)
|
|
_currentFullness = Mathf.Max(_currentFullness - _hungerPerSecond * Time.deltaTime, 0f);
|
|
}
|
|
|
|
public void Eat()
|
|
{
|
|
_currentFullness = Mathf.Min(_currentFullness + _eatAmount, _maxFullness);
|
|
}
|
|
|
|
public void ToggleHunger(bool value)
|
|
{
|
|
_getHungry = value;
|
|
}
|
|
}
|