121 lines
3.5 KiB
C#
121 lines
3.5 KiB
C#
using System;
|
|
using BlueWater.Items;
|
|
using Sirenix.OdinInspector;
|
|
using UnityEngine;
|
|
|
|
namespace BlueWater.Tycoons
|
|
{
|
|
public class Barrel : InteractionFurniture
|
|
{
|
|
[SerializeField, Required]
|
|
private SpriteRenderer _liquidImage;
|
|
|
|
[SerializeField, Required]
|
|
private SpriteRenderer _fill;
|
|
|
|
[SerializeField, Range(1f, 5f), Tooltip("목표 색상 * 밝기")]
|
|
private float _colorIntensity = 2f;
|
|
|
|
[SerializeField]
|
|
private string _idx;
|
|
|
|
[SerializeField]
|
|
private LiquidData _liquidData;
|
|
|
|
[field: SerializeField]
|
|
public int CurrentAmount { get; private set; }
|
|
|
|
private LiquidController _liquidController;
|
|
private Material _instanceMaterial;
|
|
|
|
public event Action<int> OnAmountChanged;
|
|
public static event Action<Barrel> OnBarrelInteracted;
|
|
public static event Action OnBarrelCancelInteracted;
|
|
|
|
// Hashes
|
|
private static readonly int LiquidAmountHash = Shader.PropertyToID("_LiquidAmount");
|
|
private static readonly int LiquidColorHash = Shader.PropertyToID("_LiquidColor");
|
|
|
|
protected override void Awake()
|
|
{
|
|
base.Awake();
|
|
|
|
_liquidController = FindAnyObjectByType<LiquidController>();
|
|
_instanceMaterial = Instantiate(_fill.material);
|
|
_fill.material = _instanceMaterial;
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
_liquidData = ItemManager.Instance.LiquidDataSo.GetDataByIdx(_idx);
|
|
|
|
InteractionMessage = $"{_liquidData.Name} 따르기";
|
|
_liquidImage.sprite = _liquidData.Sprite;
|
|
_instanceMaterial.SetColor(LiquidColorHash, _liquidData.Color * _colorIntensity);
|
|
SetCurrentAmount(_liquidData.GetMaxAmount());
|
|
}
|
|
|
|
public override void Interaction()
|
|
{
|
|
OnBarrelInteracted?.Invoke(this);
|
|
//_liquidController.ActiveIsPouring(this);
|
|
}
|
|
|
|
public override void CancelInteraction()
|
|
{
|
|
OnBarrelCancelInteracted?.Invoke();
|
|
//_liquidController.InActiveIsPouring();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 1. 플레이어가 빈 잔을 들고 있거나 완성되지 않은 잔을 들고 있을 때
|
|
/// </summary>
|
|
public override bool CanInteraction()
|
|
{
|
|
return !CurrentTycoonPlayer.TycoonPickupHandler.IsPickedUpItem() && CanConsume(1);
|
|
}
|
|
|
|
public bool CanConsume(int amount)
|
|
{
|
|
return CurrentAmount - amount > 0;
|
|
}
|
|
|
|
public void Consume(int amount)
|
|
{
|
|
if (CurrentAmount == int.MaxValue)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var consumeAmount = CurrentAmount - amount;
|
|
SetCurrentAmount(consumeAmount);
|
|
}
|
|
|
|
public bool TryConsume(int amount)
|
|
{
|
|
if (!CanConsume(amount)) return false;
|
|
|
|
Consume(amount);
|
|
return true;
|
|
}
|
|
|
|
public LiquidData GetLiquidData() => _liquidData;
|
|
|
|
public void SetCurrentAmount(int amount, bool isFromAction = false)
|
|
{
|
|
if (CurrentAmount == amount || CurrentAmount == int.MaxValue)
|
|
{
|
|
return;
|
|
}
|
|
|
|
CurrentAmount = amount;
|
|
var liquidAmount = CurrentAmount / 4000f;
|
|
_instanceMaterial.SetFloat(LiquidAmountHash, liquidAmount);
|
|
|
|
if (!isFromAction)
|
|
{
|
|
OnAmountChanged?.Invoke(CurrentAmount);
|
|
}
|
|
}
|
|
}
|
|
} |