CapersProject/Assets/02.Scripts/LiquidController.cs

453 lines
15 KiB
C#
Raw Normal View History

2024-09-10 10:25:05 +00:00
using System.Collections;
2024-08-14 10:52:35 +00:00
using System.Collections.Generic;
using System.Linq;
2024-09-10 07:26:29 +00:00
using BlueWater.Items;
using BlueWater.Tycoons;
2024-09-09 12:27:15 +00:00
using DG.Tweening;
using Sirenix.OdinInspector;
2024-09-03 12:04:25 +00:00
using TMPro;
using UnityEngine;
2024-08-14 10:52:35 +00:00
using UnityEngine.Pool;
2024-09-10 10:25:05 +00:00
using UnityEngine.UI;
namespace BlueWater
{
public class LiquidController : MonoBehaviour
{
2024-08-22 10:39:15 +00:00
#region Variables
[Title("컴포넌트")]
2024-09-09 12:27:15 +00:00
[SerializeField]
private GameObject _liquidPanel;
2024-09-10 10:25:05 +00:00
[SerializeField]
private GameObject _shaker;
2024-09-09 12:27:15 +00:00
2024-08-19 08:14:35 +00:00
[SerializeField]
private Renderer _renderTexture;
2024-08-14 10:52:35 +00:00
[SerializeField]
2024-08-19 03:15:31 +00:00
private Renderer _liquidRenderer;
2024-08-14 10:52:35 +00:00
2024-08-22 10:39:15 +00:00
[SerializeField]
private Collider2D _reachedCollider;
2024-09-03 12:04:25 +00:00
[SerializeField]
private TMP_Text _amountText;
2024-09-10 10:25:05 +00:00
[SerializeField]
private Image _completeCocktailImage;
[SerializeField]
private TMP_Text _completeText;
2024-08-22 10:39:15 +00:00
[Title("스폰 데이터")]
[SerializeField, Required]
private Transform _spawnTransform;
2024-08-14 10:52:35 +00:00
[SerializeField]
private Transform _spawnLocation;
2024-08-19 08:14:35 +00:00
[SerializeField]
2024-08-22 10:39:15 +00:00
private Vector3 _pushDirection;
2024-08-14 10:52:35 +00:00
[SerializeField]
2024-08-22 10:39:15 +00:00
private float _pushPower;
[Title("액체")]
[SerializeField, Required, Tooltip("액체 프리팹")]
private Liquid _liquidObject;
2024-08-19 08:14:35 +00:00
2024-08-22 10:39:15 +00:00
[SerializeField, Tooltip("초당 생성되는 액체 수(ml)")]
private int _liquidsPerSecond = 80;
2024-08-19 08:14:35 +00:00
[SerializeField]
2024-08-22 10:39:15 +00:00
private int _maxLiquidCount = 400;
2024-08-19 10:56:07 +00:00
2024-08-22 10:39:15 +00:00
[SerializeField, Range(0f, 1f), Tooltip("목표 색상으로 변경되는데 걸리는 시간")]
2024-08-19 10:56:07 +00:00
private float _colorLerpSpeed = 0.5f;
2024-08-22 10:39:15 +00:00
[SerializeField, Range(1f, 5f), Tooltip("목표 색상 * 밝기")]
private float _colorIntensity = 2f;
[Title("오브젝트 풀링")]
[SerializeField, Tooltip("오브젝트 풀링 최대 개수")]
private int _objectPoolCount = 1000;
2024-08-14 10:52:35 +00:00
2024-09-09 12:27:15 +00:00
[Title("패널")]
[SerializeField]
private float _moveDuration = 0.5f;
2024-09-10 07:26:29 +00:00
[Title("실시간 정보")]
[SerializeField]
private Barrel _currentBarrel;
2024-08-14 10:52:35 +00:00
private IObjectPool<Liquid> _objectPool;
private List<Liquid> _activeLiquids = new();
2024-09-10 10:25:05 +00:00
private Dictionary<LiquidData, int> _liquidDataCounts = new(7);
2024-08-19 08:14:35 +00:00
private Material _instanceMaterial;
2024-09-09 12:27:15 +00:00
private Tween _showTween;
private Tween _hideTween;
2024-09-10 10:25:05 +00:00
private bool _isShowingPanel;
2024-08-14 10:52:35 +00:00
private bool _isPouring;
private float _startTime = float.PositiveInfinity;
2024-08-22 10:39:15 +00:00
private int _instanceLiquidCount;
private float _currentLiquidAmount;
private float _liquidReachedTime;
2024-08-19 08:14:35 +00:00
private float _timeInterval;
2024-08-19 10:56:07 +00:00
private Color _currentMixedColor = Color.black;
private Color _targetColor;
2024-08-22 10:39:15 +00:00
2024-08-14 10:52:35 +00:00
// Hashes
2024-09-23 02:00:21 +00:00
private static readonly int LiquidAmountHash = Shader.PropertyToID("_LiquidAmount");
private static readonly int LiquidColorHash = Shader.PropertyToID("_LiquidColor");
2024-08-22 10:39:15 +00:00
#endregion
// Unity events
#region Unity events
2024-08-14 10:52:35 +00:00
private void Awake()
{
_objectPool = new ObjectPool<Liquid>(CreateObject, OnGetObject, OnReleaseObject, OnDestroyObject, maxSize: _objectPoolCount);
2024-09-09 12:27:15 +00:00
2024-09-10 10:25:05 +00:00
_hideTween = _liquidPanel.transform.DOMoveX(-150f, _moveDuration).Pause()
2024-09-09 12:27:15 +00:00
.SetAutoKill(false);
2024-09-23 02:00:21 +00:00
_showTween = _liquidPanel.transform.DOMoveX(-250f, _moveDuration).Pause()
2024-09-09 12:27:15 +00:00
.SetAutoKill(false);
2024-08-14 10:52:35 +00:00
}
2024-08-19 03:15:31 +00:00
private void Start()
{
2024-09-12 07:53:16 +00:00
EventManager.OnLiquidRegionEntered += ShowPanel;
EventManager.OnLiquidRegionExited += HidePanel;
2024-09-23 02:00:21 +00:00
EventManager.OnCocktailDiscarded += ReleaseAllObject;
EventManager.OnPlaceOnServingTable += ReleaseAllObject;
2024-08-19 08:14:35 +00:00
_instanceMaterial = Instantiate(_liquidRenderer.material);
_liquidRenderer.material = _instanceMaterial;
2024-09-23 02:00:21 +00:00
_instanceMaterial.SetFloat(LiquidAmountHash, 0f);
2024-09-10 07:26:29 +00:00
_timeInterval = 1f / _liquidsPerSecond;
2024-09-10 10:25:05 +00:00
_shaker.SetActive(true);
_amountText.enabled = true;
_completeCocktailImage.enabled = false;
_completeText.enabled = false;
2024-09-10 07:26:29 +00:00
_instanceLiquidCount = 0;
2024-09-09 12:27:15 +00:00
SetCurrentAmount(0f);
2024-08-19 03:15:31 +00:00
}
private void Update()
{
2024-08-14 10:52:35 +00:00
if (_isPouring)
{
2024-09-10 07:26:29 +00:00
// 현재 술의 재고가 없을 때
if (!_currentBarrel.CanConsume(1))
2024-08-14 10:52:35 +00:00
{
2024-08-19 08:14:35 +00:00
InActiveIsPouring();
return;
2024-08-14 10:52:35 +00:00
}
2024-08-19 08:14:35 +00:00
2024-09-10 07:26:29 +00:00
// 술이 완성되었을 때
if (_instanceLiquidCount >= _maxLiquidCount)
{
2024-09-10 10:25:05 +00:00
StartCoroutine(nameof(CompleteCocktail));
2024-09-10 07:26:29 +00:00
return;
}
2024-08-19 08:14:35 +00:00
if (Time.time - _startTime >= _timeInterval)
2024-08-14 10:52:35 +00:00
{
2024-08-19 08:14:35 +00:00
_objectPool.Get();
2024-09-10 07:26:29 +00:00
if (!_liquidDataCounts.TryAdd(_currentBarrel.GetLiquidData(), 1))
2024-08-19 08:14:35 +00:00
{
2024-09-10 07:26:29 +00:00
_liquidDataCounts[_currentBarrel.GetLiquidData()] += 1;
2024-08-19 08:14:35 +00:00
}
2024-09-10 07:26:29 +00:00
_currentBarrel.Consume(1);
2024-08-19 08:14:35 +00:00
_startTime = Time.time;
2024-08-14 10:52:35 +00:00
}
}
2024-08-19 10:56:07 +00:00
2024-08-22 10:39:15 +00:00
if (_liquidReachedTime + _colorLerpSpeed >= Time.time)
2024-08-19 10:56:07 +00:00
{
_currentMixedColor = Color.Lerp(_currentMixedColor, _targetColor, _colorLerpSpeed * Time.deltaTime);
2024-09-23 02:00:21 +00:00
_instanceMaterial.SetColor(LiquidColorHash, _currentMixedColor * _colorIntensity);
2024-08-19 10:56:07 +00:00
}
}
private void OnDestroy()
{
2024-09-12 07:53:16 +00:00
EventManager.OnLiquidRegionEntered -= ShowPanel;
EventManager.OnLiquidRegionExited -= HidePanel;
2024-09-23 02:00:21 +00:00
EventManager.OnCocktailDiscarded -= ReleaseAllObject;
EventManager.OnPlaceOnServingTable -= ReleaseAllObject;
}
2024-08-22 10:39:15 +00:00
#endregion
// Object pooling system
#region Object pooling system
2024-08-14 10:52:35 +00:00
private Liquid CreateObject()
{
var instance = Instantiate(_liquidObject, _spawnTransform.position, Quaternion.identity, _spawnLocation);
instance.SetManagedPool(_objectPool);
return instance;
}
private void OnGetObject(Liquid liquid)
{
liquid.transform.position = _spawnTransform.position;
liquid.transform.rotation = Quaternion.identity;
2024-08-22 10:39:15 +00:00
liquid.gameObject.SetActive(true);
_instanceLiquidCount++;
2024-09-10 07:26:29 +00:00
var liquidColor = _currentBarrel.GetLiquidData().Color;
liquid.Initialize(this, _reachedCollider, liquidColor, _pushDirection.normalized * _pushPower);
2024-08-14 10:52:35 +00:00
_activeLiquids.Add(liquid);
}
private void OnReleaseObject(Liquid liquid)
{
liquid.gameObject.SetActive(false);
_activeLiquids.Remove(liquid);
}
private void OnDestroyObject(Liquid liquid)
{
Destroy(liquid.gameObject);
_activeLiquids.Remove(liquid);
}
2024-08-19 08:14:35 +00:00
2024-08-22 10:39:15 +00:00
#endregion
2024-08-19 08:14:35 +00:00
2024-08-22 10:39:15 +00:00
// Custom methods
#region Custom methods
2024-09-23 02:00:21 +00:00
2024-08-22 10:39:15 +00:00
/// <summary>
/// 술 제조 과정 초기화 함수
/// </summary>
2024-08-14 10:52:35 +00:00
public void ReleaseAllObject()
{
2024-08-22 10:39:15 +00:00
// 리스트 삭제는 뒤에서부터 해야 오류가 없음
2024-08-14 10:52:35 +00:00
for (var i = _activeLiquids.Count - 1; i >= 0; i--)
{
_activeLiquids[i].Destroy();
}
2024-09-23 02:00:21 +00:00
2024-09-10 07:26:29 +00:00
_liquidDataCounts.Clear();
2024-08-22 10:39:15 +00:00
_instanceLiquidCount = 0;
2024-09-23 02:00:21 +00:00
_instanceMaterial.SetFloat(LiquidAmountHash, 0f);
2024-09-03 12:04:25 +00:00
SetCurrentAmount(0f);
2024-09-23 02:00:21 +00:00
HidePanel();
2024-08-14 10:52:35 +00:00
}
2024-09-23 02:00:21 +00:00
public void ActiveIsPouring(Barrel barrel)
2024-09-10 07:26:29 +00:00
{
_currentBarrel = barrel;
if (_instanceLiquidCount == 0)
{
2024-09-23 02:00:21 +00:00
ShowPanelFast();
2024-09-10 10:25:05 +00:00
_shaker.SetActive(true);
_amountText.enabled = true;
_completeCocktailImage.enabled = false;
_completeText.enabled = false;
2024-09-10 07:26:29 +00:00
_currentMixedColor = _currentBarrel.GetLiquidData().Color;
2024-09-23 02:00:21 +00:00
_instanceMaterial.SetColor(LiquidColorHash, _currentMixedColor * _colorIntensity);
EventManager.OnCocktailStarted?.Invoke();
2024-09-10 07:26:29 +00:00
}
2024-08-14 10:52:35 +00:00
_startTime = Time.time;
_isPouring = true;
}
public void InActiveIsPouring()
{
_isPouring = false;
}
2024-09-03 12:04:25 +00:00
private void SetCurrentAmount(float value)
{
_currentLiquidAmount = value;
if (_amountText)
{
var percent = (int)(_currentLiquidAmount / _maxLiquidCount * 100);
_amountText.text = $"{percent}%";
}
else
{
if (_amountText.enabled)
{
_amountText.enabled = false;
}
}
}
2024-09-10 07:26:29 +00:00
/// <summary>
/// 술을 완성한 경우
/// </summary>
2024-09-10 10:25:05 +00:00
private IEnumerator CompleteCocktail()
2024-09-10 07:26:29 +00:00
{
InActiveIsPouring();
2024-09-10 10:25:05 +00:00
yield return new WaitUntil(() => _currentLiquidAmount >= _maxLiquidCount);
var currentCocktailIngredients = new List<CocktailIngredient>(7);
2024-09-10 07:26:29 +00:00
foreach (var element in _liquidDataCounts)
{
var idx = element.Key.Idx;
var amount = element.Value;
currentCocktailIngredients.Add(new CocktailIngredient(idx, amount));
}
// ItemManager를 통해 모든 CocktailData 가져오기
2024-09-12 07:36:24 +00:00
var cocktailDatas = ItemManager.Instance.CocktailDataSo.GetData();
2024-09-10 07:26:29 +00:00
CocktailData matchingCocktail = null;
// 모든 칵테일 데이터를 순회하면서 조건에 맞는 칵테일 찾기
foreach (var cocktailData in cocktailDatas.Values)
{
var validIngredients = cocktailData.GetValidIngredients();
// 조건 1: 재료 개수 동일 체크
if (validIngredients.Count != currentCocktailIngredients.Count)
continue;
var allIngredientsMatch = true;
// 현재 음료 재료를 하나씩 validIngredients와 비교
foreach (var currentIngredient in currentCocktailIngredients)
{
// 동일한 Idx를 가진 재료가 있는지 찾음
var matchingValidIngredient = validIngredients.FirstOrDefault(ingredient => ingredient.Idx == currentIngredient.Idx);
// 만약 Idx가 일치하는 재료가 없으면 조건 불충족
if (matchingValidIngredient == null)
{
allIngredientsMatch = false;
break;
}
// 조건 2: Amount 값이 RatioRange에 따른 오차 범위 내에 있는지 체크
var validAmount = matchingValidIngredient.Amount;
//var maxLiquidCount = cocktailData.GetCocktailAmount(validIngredients);
var range = _maxLiquidCount / 100 * cocktailData.RatioRange;
var minAmount = validAmount - range;
var maxAmount = validAmount + range;
if (currentIngredient.Amount < minAmount || currentIngredient.Amount > maxAmount)
{
allIngredientsMatch = false;
break;
}
}
// 조건이 모두 만족하면 매칭되는 칵테일을 찾음
if (allIngredientsMatch)
{
matchingCocktail = cocktailData;
break;
}
}
// 조건에 만족하는 칵테일이 없음
if (matchingCocktail == null)
{
2024-09-12 07:36:24 +00:00
matchingCocktail = ItemManager.Instance.CocktailDataSo.GetDataByIdx("Cocktail000");
2024-09-10 10:25:05 +00:00
_completeText.text = "실패";
}
else
{
_completeText.text = $"성공!\n{matchingCocktail.Name}";
2024-09-10 07:26:29 +00:00
}
2024-09-10 10:25:05 +00:00
// TODO : 음료 제조 성공, 실패 연출 추가
_shaker.SetActive(false);
_amountText.enabled = false;
_completeCocktailImage.sprite = matchingCocktail.Sprite;
_completeCocktailImage.enabled = true;
_completeText.enabled = true;
2024-09-10 07:26:29 +00:00
2024-09-10 10:25:05 +00:00
// 1. 플레이어 음료 들기
2024-09-12 07:53:16 +00:00
EventManager.OnCocktailCompleted?.Invoke(matchingCocktail);
2024-09-10 10:25:05 +00:00
yield return new WaitForSeconds(1f);
HidePanel();
2024-09-10 07:26:29 +00:00
}
2024-08-14 10:52:35 +00:00
2024-08-22 10:39:15 +00:00
/// <summary>
/// 사용된 색상의 비율에 맞게 색을 혼합시키는 함수
/// </summary>
private Color MixColorsByTime()
{
2024-09-10 07:26:29 +00:00
var totalCounts = _liquidDataCounts.Values.Sum();
2024-08-22 10:39:15 +00:00
var mixedColor = Color.black;
2024-09-10 07:26:29 +00:00
foreach (var element in _liquidDataCounts)
2024-08-22 10:39:15 +00:00
{
2024-09-10 07:26:29 +00:00
var color = element.Key.Color;
2024-08-22 10:39:15 +00:00
var count = element.Value;
var ratio = count / (float)totalCounts;
mixedColor += color * ratio;
}
mixedColor.a = 1f;
return mixedColor;
}
/// <summary>
/// 액체가 특정 오브젝트에 충돌했을 때, 실행해야하는 과정
/// </summary>
2024-08-19 08:14:35 +00:00
public void OnLiquidReached()
2024-08-14 10:52:35 +00:00
{
2024-08-22 10:39:15 +00:00
_liquidReachedTime = Time.time;
2024-09-03 12:04:25 +00:00
SetCurrentAmount(++_currentLiquidAmount);
2024-08-22 10:39:15 +00:00
var liquidAmount = Mathf.Clamp(_currentLiquidAmount / _maxLiquidCount, 0f, 1f);
2024-09-23 02:00:21 +00:00
_instanceMaterial.SetFloat(LiquidAmountHash, liquidAmount);
2024-08-19 10:56:07 +00:00
_targetColor = MixColorsByTime();
2024-08-19 08:14:35 +00:00
2024-08-22 10:39:15 +00:00
if (liquidAmount >= 1f)
2024-08-14 10:52:35 +00:00
{
2024-08-19 08:14:35 +00:00
InActiveIsPouring();
2024-08-14 10:52:35 +00:00
}
}
2024-08-22 10:39:15 +00:00
2024-09-23 02:00:21 +00:00
public void ShowPanelFast()
2024-09-09 12:27:15 +00:00
{
2024-09-10 10:25:05 +00:00
if (_isShowingPanel) return;
2024-09-23 02:00:21 +00:00
_liquidPanel.transform.position = new Vector3(-250f, 0f, 0f);
_liquidPanel.SetActive(true);
_isShowingPanel = true;
_hideTween.Pause();
_showTween.Pause();
}
public void ShowPanel()
{
if (_isShowingPanel || _instanceLiquidCount <= 0) return;
2024-09-10 10:25:05 +00:00
_isShowingPanel = true;
2024-09-09 12:27:15 +00:00
_hideTween.Pause();
_showTween.Restart();
}
public void HidePanel()
{
2024-09-10 10:25:05 +00:00
if (!_isShowingPanel) return;
_isShowingPanel = false;
2024-09-09 12:27:15 +00:00
_showTween.Pause();
_hideTween.Restart();
}
2024-08-22 10:39:15 +00:00
#endregion
}
}