2024-09-12 07:36:24 +00:00
|
|
|
using System;
|
2024-09-10 07:26:29 +00:00
|
|
|
using BlueWater.Items;
|
2024-09-23 02:00:21 +00:00
|
|
|
using Sirenix.OdinInspector;
|
2024-09-10 07:26:29 +00:00
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
namespace BlueWater.Tycoons
|
|
|
|
{
|
|
|
|
public class Barrel : InteractionFurniture
|
|
|
|
{
|
2024-09-23 02:00:21 +00:00
|
|
|
[SerializeField, Required]
|
|
|
|
private SpriteRenderer _liquidImage;
|
|
|
|
|
|
|
|
[SerializeField, Required]
|
|
|
|
private SpriteRenderer _fill;
|
|
|
|
|
|
|
|
[SerializeField, Range(1f, 5f), Tooltip("목표 색상 * 밝기")]
|
|
|
|
private float _colorIntensity = 2f;
|
|
|
|
|
2024-09-10 07:26:29 +00:00
|
|
|
[SerializeField]
|
|
|
|
private string _idx;
|
|
|
|
|
|
|
|
[SerializeField]
|
|
|
|
private LiquidData _liquidData;
|
|
|
|
|
2024-09-23 02:00:21 +00:00
|
|
|
[field: SerializeField]
|
|
|
|
public int CurrentAmount { get; private set; }
|
2024-10-27 09:44:22 +00:00
|
|
|
|
2024-11-05 12:27:46 +00:00
|
|
|
[field: SerializeField]
|
|
|
|
public bool IsActivated { get; private set; }
|
|
|
|
|
|
|
|
[field: SerializeField]
|
|
|
|
public bool IsAutoSupply { get; private set; }
|
2024-09-30 12:05:19 +00:00
|
|
|
|
2024-09-23 02:00:21 +00:00
|
|
|
private Material _instanceMaterial;
|
2024-11-05 12:27:46 +00:00
|
|
|
private float _elapsedTime;
|
2024-09-23 02:00:21 +00:00
|
|
|
|
2024-09-24 10:35:49 +00:00
|
|
|
public static event Action<Barrel> OnBarrelInteracted;
|
|
|
|
public static event Action OnBarrelCancelInteracted;
|
2024-09-23 02:00:21 +00:00
|
|
|
|
|
|
|
// Hashes
|
|
|
|
private static readonly int LiquidAmountHash = Shader.PropertyToID("_LiquidAmount");
|
|
|
|
private static readonly int LiquidColorHash = Shader.PropertyToID("_LiquidColor");
|
2024-09-10 07:26:29 +00:00
|
|
|
|
|
|
|
protected override void Awake()
|
|
|
|
{
|
|
|
|
base.Awake();
|
2024-09-30 12:05:19 +00:00
|
|
|
|
2024-09-23 02:00:21 +00:00
|
|
|
_instanceMaterial = Instantiate(_fill.material);
|
|
|
|
_fill.material = _instanceMaterial;
|
2024-09-30 12:05:19 +00:00
|
|
|
_liquidData = ItemManager.Instance.LiquidDataSo.GetDataByIdx(_idx);
|
|
|
|
SetCurrentAmount(0);
|
2024-09-12 07:36:24 +00:00
|
|
|
}
|
|
|
|
|
2024-10-27 09:44:22 +00:00
|
|
|
protected override void Start()
|
2024-09-12 07:36:24 +00:00
|
|
|
{
|
2024-10-27 09:44:22 +00:00
|
|
|
base.Start();
|
2024-10-28 05:09:06 +00:00
|
|
|
|
|
|
|
EventManager.OnAddBarrels += AddCurrentAmount;
|
2024-11-05 12:27:46 +00:00
|
|
|
EventManager.OnAutoSupplyBarrels += AutoSupply;
|
2024-10-27 09:44:22 +00:00
|
|
|
|
2024-11-05 12:27:46 +00:00
|
|
|
_liquidImage.sprite = IsActivated ? _liquidData.Sprite : DataManager.Instance.SpriteDataSo.Lock;
|
2024-09-24 10:35:49 +00:00
|
|
|
InteractionMessage = $"{_liquidData.Name} 따르기";
|
2024-09-23 02:00:21 +00:00
|
|
|
_instanceMaterial.SetColor(LiquidColorHash, _liquidData.Color * _colorIntensity);
|
2024-09-10 07:26:29 +00:00
|
|
|
}
|
|
|
|
|
2024-11-05 12:27:46 +00:00
|
|
|
private void Update()
|
|
|
|
{
|
|
|
|
if (!IsAutoSupply) return;
|
|
|
|
|
|
|
|
if (_elapsedTime >= 1f)
|
|
|
|
{
|
|
|
|
AddCurrentAmount(TycoonManager.Instance.TycoonStatus.BarrelAutoIncrease);
|
|
|
|
|
|
|
|
_elapsedTime -= 1f;
|
|
|
|
}
|
|
|
|
|
|
|
|
_elapsedTime += Time.deltaTime;
|
|
|
|
}
|
|
|
|
|
2024-10-28 05:09:06 +00:00
|
|
|
private void OnDestroy()
|
|
|
|
{
|
|
|
|
EventManager.OnAddBarrels -= AddCurrentAmount;
|
2024-11-05 12:27:46 +00:00
|
|
|
EventManager.OnAutoSupplyBarrels -= AutoSupply;
|
2024-10-28 05:09:06 +00:00
|
|
|
}
|
|
|
|
|
2024-09-10 07:26:29 +00:00
|
|
|
public override void Interaction()
|
|
|
|
{
|
2024-09-24 10:35:49 +00:00
|
|
|
OnBarrelInteracted?.Invoke(this);
|
2024-10-14 11:13:08 +00:00
|
|
|
GameManager.Instance.CurrentTycoonPlayer.IsMakingCocktail = true;
|
2024-09-10 07:26:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public override void CancelInteraction()
|
|
|
|
{
|
2024-09-24 10:35:49 +00:00
|
|
|
OnBarrelCancelInteracted?.Invoke();
|
2024-10-14 11:13:08 +00:00
|
|
|
GameManager.Instance.CurrentTycoonPlayer.IsMakingCocktail = false;
|
2024-09-10 07:26:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 1. 플레이어가 빈 잔을 들고 있거나 완성되지 않은 잔을 들고 있을 때
|
|
|
|
/// </summary>
|
|
|
|
public override bool CanInteraction()
|
|
|
|
{
|
2024-11-04 12:22:07 +00:00
|
|
|
return IsActivated && !CurrentTycoonPlayer.TycoonPickupHandler.IsPickedUpItem && CanConsume(1);
|
2024-09-10 07:26:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public bool CanConsume(int amount)
|
|
|
|
{
|
2024-09-23 02:00:21 +00:00
|
|
|
return CurrentAmount - amount > 0;
|
2024-09-10 07:26:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Consume(int amount)
|
|
|
|
{
|
2024-09-23 02:00:21 +00:00
|
|
|
if (CurrentAmount == int.MaxValue)
|
2024-09-10 07:26:29 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2024-09-23 02:00:21 +00:00
|
|
|
|
|
|
|
var consumeAmount = CurrentAmount - amount;
|
2024-11-04 12:22:07 +00:00
|
|
|
SetCurrentAmount(consumeAmount);
|
2024-09-10 07:26:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public bool TryConsume(int amount)
|
|
|
|
{
|
|
|
|
if (!CanConsume(amount)) return false;
|
|
|
|
|
|
|
|
Consume(amount);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public LiquidData GetLiquidData() => _liquidData;
|
2024-09-23 02:00:21 +00:00
|
|
|
|
2024-11-04 12:22:07 +00:00
|
|
|
public void SetCurrentAmount(int amount)
|
2024-09-23 02:00:21 +00:00
|
|
|
{
|
2024-09-30 12:05:19 +00:00
|
|
|
if (CurrentAmount == int.MaxValue)
|
2024-09-23 02:00:21 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CurrentAmount = amount;
|
|
|
|
var liquidAmount = CurrentAmount / 4000f;
|
|
|
|
_instanceMaterial.SetFloat(LiquidAmountHash, liquidAmount);
|
|
|
|
}
|
2024-09-30 12:05:19 +00:00
|
|
|
|
|
|
|
public void Activate()
|
|
|
|
{
|
|
|
|
IsActivated = true;
|
2024-11-05 12:27:46 +00:00
|
|
|
_liquidImage.sprite = IsActivated ? _liquidData.Sprite : DataManager.Instance.SpriteDataSo.Lock;
|
2024-09-30 12:05:19 +00:00
|
|
|
SetCurrentAmount(_liquidData.GetMaxAmount());
|
|
|
|
}
|
2024-10-28 05:09:06 +00:00
|
|
|
|
2024-11-05 12:27:46 +00:00
|
|
|
public void AutoSupply()
|
|
|
|
{
|
|
|
|
IsAutoSupply = true;
|
|
|
|
}
|
|
|
|
|
2024-10-28 05:09:06 +00:00
|
|
|
public void AddCurrentAmount(int addedValue)
|
|
|
|
{
|
2024-11-05 12:27:46 +00:00
|
|
|
if (!IsActivated) return;
|
2024-10-28 05:09:06 +00:00
|
|
|
|
2024-11-04 12:22:07 +00:00
|
|
|
SetCurrentAmount(CurrentAmount + addedValue);
|
2024-10-28 05:09:06 +00:00
|
|
|
}
|
2024-09-10 07:26:29 +00:00
|
|
|
}
|
|
|
|
}
|