2024-09-12 07:36:24 +00:00
|
|
|
using System;
|
2024-09-10 07:26:29 +00:00
|
|
|
using BlueWater.Items;
|
2024-11-12 12:07:23 +00:00
|
|
|
using BlueWater.Players;
|
2024-09-23 02:00:21 +00:00
|
|
|
using Sirenix.OdinInspector;
|
2024-09-10 07:26:29 +00:00
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
namespace BlueWater.Tycoons
|
|
|
|
{
|
2024-11-12 12:07:23 +00:00
|
|
|
public static class BarrelSpineAnimation
|
|
|
|
{
|
|
|
|
public const string Idle = "IdleNormal";
|
|
|
|
public const string Mold = "IdleDirty";
|
|
|
|
public const string ChangeIdle = "ChangeClean";
|
|
|
|
public const string ChangeMold = "ChangeDirty";
|
|
|
|
}
|
|
|
|
|
2024-09-10 07:26:29 +00:00
|
|
|
public class Barrel : InteractionFurniture
|
|
|
|
{
|
2024-11-12 12:07:23 +00:00
|
|
|
[SerializeField]
|
|
|
|
private SpineController _spineController;
|
|
|
|
|
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;
|
2024-11-11 06:09:32 +00:00
|
|
|
|
|
|
|
[SerializeField]
|
|
|
|
private string _interactionEntryName;
|
2024-09-10 07:26:29 +00:00
|
|
|
|
|
|
|
[SerializeField]
|
|
|
|
private LiquidData _liquidData;
|
|
|
|
|
2024-09-23 02:00:21 +00:00
|
|
|
[field: SerializeField]
|
|
|
|
public int CurrentAmount { get; private set; }
|
2024-11-12 12:07:23 +00:00
|
|
|
|
|
|
|
[field: SerializeField]
|
|
|
|
public bool IsStatue { 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-11-11 06:09:32 +00:00
|
|
|
[field: SerializeField]
|
|
|
|
public bool IsMoldy { get; private set; }
|
|
|
|
|
|
|
|
[SerializeField]
|
|
|
|
private float _playerHoldingTime = 3f;
|
|
|
|
|
2024-09-23 02:00:21 +00:00
|
|
|
private Material _instanceMaterial;
|
2024-11-11 06:09:32 +00:00
|
|
|
private float _supplyElapsedTime;
|
|
|
|
private bool _isPlayerInteracting;
|
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-11-12 12:07:23 +00:00
|
|
|
_spineController = GetComponent<SpineController>();
|
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-07 12:20:24 +00:00
|
|
|
_liquidImage.sprite = IsActivated ? _liquidData.Sprite : DataManager.Instance.SpriteDataSo.BarrelLock;
|
2024-09-23 02:00:21 +00:00
|
|
|
_instanceMaterial.SetColor(LiquidColorHash, _liquidData.Color * _colorIntensity);
|
2024-11-12 12:07:23 +00:00
|
|
|
|
|
|
|
if (_spineController)
|
|
|
|
{
|
|
|
|
_spineController.PlayAnimation(BarrelSpineAnimation.Idle, false);
|
|
|
|
}
|
2024-09-10 07:26:29 +00:00
|
|
|
}
|
|
|
|
|
2024-11-05 12:27:46 +00:00
|
|
|
private void Update()
|
|
|
|
{
|
2024-11-11 06:09:32 +00:00
|
|
|
if (IsShowing)
|
|
|
|
{
|
2024-11-11 09:51:12 +00:00
|
|
|
var holdingGauge = Mathf.Clamp(HoldingElapsedTime / _playerHoldingTime, 0f, 1f);
|
|
|
|
EventManager.InvokeHoldInteracting(holdingGauge);
|
2024-11-11 06:09:32 +00:00
|
|
|
}
|
2024-11-05 12:27:46 +00:00
|
|
|
|
2024-11-11 06:09:32 +00:00
|
|
|
if (IsMoldy)
|
2024-11-05 12:27:46 +00:00
|
|
|
{
|
2024-11-11 06:09:32 +00:00
|
|
|
if (HoldingElapsedTime >= _playerHoldingTime)
|
|
|
|
{
|
|
|
|
Recovery();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_isPlayerInteracting)
|
|
|
|
{
|
|
|
|
HoldingElapsedTime += Time.deltaTime;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (HoldingElapsedTime > 0f)
|
|
|
|
{
|
|
|
|
HoldingElapsedTime -= Time.deltaTime;
|
|
|
|
}
|
|
|
|
}
|
2024-11-05 12:27:46 +00:00
|
|
|
}
|
2024-11-11 06:09:32 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!IsAutoSupply) return;
|
|
|
|
|
|
|
|
if (_supplyElapsedTime >= 1f)
|
|
|
|
{
|
|
|
|
AddCurrentAmount(TycoonManager.Instance.TycoonStatus.BarrelAutoIncrease);
|
|
|
|
|
|
|
|
_supplyElapsedTime -= 1f;
|
|
|
|
}
|
2024-11-05 12:27:46 +00:00
|
|
|
|
2024-11-11 06:09:32 +00:00
|
|
|
_supplyElapsedTime += Time.deltaTime;
|
|
|
|
}
|
2024-11-05 12:27:46 +00:00
|
|
|
}
|
|
|
|
|
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-11-11 06:09:32 +00:00
|
|
|
if (!IsMoldy)
|
|
|
|
{
|
|
|
|
OnBarrelInteracted?.Invoke(this);
|
|
|
|
GameManager.Instance.CurrentTycoonPlayer.IsMakingCocktail = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_isPlayerInteracting = true;
|
|
|
|
}
|
2024-09-10 07:26:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public override void CancelInteraction()
|
|
|
|
{
|
2024-11-11 06:09:32 +00:00
|
|
|
if (!IsMoldy)
|
|
|
|
{
|
|
|
|
OnBarrelCancelInteracted?.Invoke();
|
|
|
|
GameManager.Instance.CurrentTycoonPlayer.IsMakingCocktail = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_isPlayerInteracting = false;
|
|
|
|
HoldingElapsedTime = 0f;
|
|
|
|
}
|
2024-09-10 07:26:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 1. 플레이어가 빈 잔을 들고 있거나 완성되지 않은 잔을 들고 있을 때
|
|
|
|
/// </summary>
|
|
|
|
public override bool CanInteraction()
|
|
|
|
{
|
2024-11-11 06:09:32 +00:00
|
|
|
return IsActivated && !CurrentTycoonPlayer.TycoonPickupHandler.IsPickedUpItem &&
|
|
|
|
((!IsMoldy && CanConsume(1)) || IsMoldy);
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void ShowInteractionUi()
|
|
|
|
{
|
|
|
|
if (IsMoldy)
|
|
|
|
{
|
|
|
|
UpdateLocalizedString("InteractionMold");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
UpdateLocalizedString(_interactionEntryName);
|
|
|
|
}
|
2024-11-12 12:07:23 +00:00
|
|
|
|
|
|
|
if (IsStatue)
|
|
|
|
{
|
|
|
|
base.ShowInteractionUi();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_spineController.EnableCustomMaterial();
|
|
|
|
EventManager.InvokeShowInteractionUi(InteractionMessage);
|
|
|
|
IsShowing = true;
|
|
|
|
}
|
2024-09-10 07:26:29 +00:00
|
|
|
}
|
|
|
|
|
2024-11-12 12:07:23 +00:00
|
|
|
public override void HideInteractionUi()
|
|
|
|
{
|
|
|
|
if (IsStatue)
|
|
|
|
{
|
|
|
|
base.HideInteractionUi();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_spineController.DisableCustomMaterial();
|
|
|
|
EventManager.InvokeHideInteractionUi();
|
|
|
|
IsShowing = false;
|
|
|
|
}
|
|
|
|
}
|
2024-11-11 06:09:32 +00:00
|
|
|
|
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-07 12:20:24 +00:00
|
|
|
_liquidImage.sprite = IsActivated ? _liquidData.Sprite : DataManager.Instance.SpriteDataSo.BarrelLock;
|
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-11-11 06:09:32 +00:00
|
|
|
|
|
|
|
public bool CanMold()
|
|
|
|
{
|
|
|
|
return _liquidData.Idx != "LiquidA" && IsActivated && !IsMoldy;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Mold()
|
|
|
|
{
|
|
|
|
IsMoldy = true;
|
2024-11-12 12:07:23 +00:00
|
|
|
_spineController.PlayAnimation(BarrelSpineAnimation.ChangeMold, false);
|
|
|
|
_spineController.AddAnimation(BarrelSpineAnimation.Mold, true);
|
2024-11-11 06:09:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Recovery()
|
|
|
|
{
|
|
|
|
CancelInteraction();
|
|
|
|
IsMoldy = false;
|
2024-11-12 12:07:23 +00:00
|
|
|
_spineController.PlayAnimation(BarrelSpineAnimation.ChangeIdle, false);
|
|
|
|
_spineController.AddAnimation(BarrelSpineAnimation.Idle, false);
|
2024-11-11 06:09:32 +00:00
|
|
|
}
|
2024-09-10 07:26:29 +00:00
|
|
|
}
|
|
|
|
}
|