150 lines
4.7 KiB
C#
150 lines
4.7 KiB
C#
using System;
|
|
using BlueWater.Interfaces;
|
|
using BlueWater.Players.Tycoons;
|
|
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("컴포넌트")]
|
|
public Canvas InteractionCanvas { get; private set; }
|
|
|
|
[field: SerializeField, BoxGroup("컴포넌트")]
|
|
public Transform InteractionUi { 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;
|
|
|
|
[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.OnTycoonOpenedEvent += OpenTycoonSwitch;
|
|
EventManager.OnTycoonClosedEvent += ClosedTycoonSwitch;
|
|
|
|
RegisterPlayerInteraction();
|
|
}
|
|
|
|
protected virtual void OnDisable()
|
|
{
|
|
if (IsQuitting) return;
|
|
|
|
EventManager.OnTycoonOpenedEvent -= OpenTycoonSwitch;
|
|
EventManager.OnTycoonClosedEvent -= ClosedTycoonSwitch;
|
|
|
|
UnregisterPlayerInteraction();
|
|
}
|
|
|
|
private void OnApplicationQuit()
|
|
{
|
|
IsQuitting = true;
|
|
}
|
|
|
|
[Button("컴포넌트 초기화")]
|
|
protected virtual void InitializeComponents()
|
|
{
|
|
if (!CenterTransform)
|
|
{
|
|
CenterTransform = transform;
|
|
}
|
|
|
|
VisualLook = transform.Find("VisualLook").GetComponent<SpriteRenderer>();
|
|
InteractionCanvas = transform.Find("InteractionCanvas").GetComponent<Canvas>();
|
|
InteractionCanvas.GetComponent<Canvas>().worldCamera = Camera.main;
|
|
InteractionUi = InteractionCanvas.transform.Find("InteractionUi");
|
|
InteractionUi.localScale = Vector3.one * (1 / transform.localScale.x);
|
|
|
|
CurrentTycoonPlayer = GameManager.Instance.CurrentTycoonPlayer;
|
|
}
|
|
|
|
public abstract void Interaction();
|
|
|
|
public virtual void CancelInteraction() { }
|
|
|
|
public virtual bool CanInteraction() => true;
|
|
|
|
public virtual void ShowInteractionUi()
|
|
{
|
|
// if (!InteractionCanvas) return;
|
|
//
|
|
// InteractionCanvas.gameObject.SetActive(true);
|
|
|
|
VisualLook.material = OutlineMaterial;
|
|
EventManager.OnShowInteractionUi?.Invoke(InteractionMessage);
|
|
}
|
|
|
|
public virtual void HideInteractionUi()
|
|
{
|
|
// if (!InteractionCanvas) return;
|
|
//
|
|
// InteractionCanvas.gameObject.SetActive(false);
|
|
VisualLook.material = OriginalMaterial;
|
|
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;
|
|
}
|
|
}
|
|
} |