CapersProject/Assets/02.Scripts/Prop/Tycoon/InteractionFuniture.cs

140 lines
4.2 KiB
C#
Raw Normal View History

using BlueWater.Interfaces;
using BlueWater.Players.Tycoons;
2024-09-26 11:50:39 +00:00
using BlueWater.Uis;
using Sirenix.OdinInspector;
using UnityEngine;
namespace BlueWater.Tycoons
{
public abstract class InteractionFurniture : MonoBehaviour, IPlayerInteraction
{
[BoxGroup("컴포넌트")]
[field: SerializeField, BoxGroup("컴포넌트")]
public Transform CenterTransform { get; private set; }
[field: SerializeField, BoxGroup("컴포넌트")]
public SpriteRenderer VisualLook { get; private set; }
[field: SerializeField, BoxGroup("컴포넌트")]
2024-09-26 11:50:39 +00:00
public InteractionCanvas InteractionCanvas { get; private set; }
[field: SerializeField, Required, BoxGroup("컴포넌트")]
public Material OutlineMaterial { get; private set; }
[BoxGroup("변수")]
[field: SerializeField, BoxGroup("변수")]
public bool EnableInteraction { get; private set; } = true;
[field: SerializeField, BoxGroup("변수")]
public float InteractionRadius { get; private set; } = 2f;
2024-09-24 10:35:49 +00:00
[field: SerializeField, BoxGroup("변수")]
public string InteractionMessage { get; protected set; }
[Title("실시간 데이터")]
[SerializeField]
protected bool IsOpened;
protected TycoonPlayer CurrentTycoonPlayer;
protected bool IsQuitting;
protected Material OriginalMaterial;
private void OnDrawGizmosSelected()
{
if (!CenterTransform) return;
Gizmos.color = Color.blue;
Gizmos.DrawWireSphere(CenterTransform.position, InteractionRadius);
}
protected virtual void Awake()
{
InitializeComponents();
OriginalMaterial = VisualLook.material;
}
protected virtual void OnEnable()
{
EventManager.OnTycoonGameStarted += OpenTycoonSwitch;
EventManager.OnTycoonGameOvered += ClosedTycoonSwitch;
RegisterPlayerInteraction();
}
protected virtual void OnDisable()
{
if (IsQuitting) return;
EventManager.OnTycoonGameStarted -= OpenTycoonSwitch;
EventManager.OnTycoonGameOvered -= ClosedTycoonSwitch;
UnregisterPlayerInteraction();
}
private void OnApplicationQuit()
{
IsQuitting = true;
}
[Button("컴포넌트 초기화")]
protected virtual void InitializeComponents()
{
if (!CenterTransform)
{
CenterTransform = transform;
}
VisualLook = transform.Find("VisualLook").GetComponent<SpriteRenderer>();
2024-09-26 11:50:39 +00:00
InteractionCanvas = transform.GetComponentInChildren<InteractionCanvas>();
CurrentTycoonPlayer = GameManager.Instance.CurrentTycoonPlayer;
}
public abstract void Interaction();
2024-09-09 12:27:15 +00:00
public virtual void CancelInteraction() { }
public virtual bool CanInteraction() => true;
public virtual void ShowInteractionUi()
{
VisualLook.material = OutlineMaterial;
2024-09-24 10:35:49 +00:00
EventManager.OnShowInteractionUi?.Invoke(InteractionMessage);
}
public virtual void HideInteractionUi()
{
2024-10-06 23:41:09 +00:00
if (VisualLook)
{
VisualLook.material = OriginalMaterial;
}
2024-09-24 10:35:49 +00:00
EventManager.OnHideInteractionUi?.Invoke();
}
protected void RegisterPlayerInteraction()
{
if (EnableInteraction)
{
GameManager.Instance.CurrentTycoonPlayer.TycoonInput.RegisterPlayerInteraction(this);
}
}
protected void UnregisterPlayerInteraction()
{
if (EnableInteraction)
{
GameManager.Instance.CurrentTycoonPlayer.TycoonInput.UnregisterPlayerInteraction(this);
}
}
protected virtual void OpenTycoonSwitch()
{
IsOpened = true;
}
protected virtual void ClosedTycoonSwitch()
{
IsOpened = false;
}
}
}