115 lines
3.3 KiB
C#
115 lines
3.3 KiB
C#
using UnityEngine;
|
|
using System.Collections;
|
|
|
|
namespace MedievalShopkeeper
|
|
{
|
|
public class ShopDoor : Interactable
|
|
{
|
|
public static ShopDoor Instance { get; private set; }
|
|
|
|
[Header("Door Settings")]
|
|
[Tooltip("The child object containing the physical door mesh.")]
|
|
public Transform doorModel;
|
|
|
|
[Tooltip("Rotation when the shop is OPEN.")]
|
|
public Vector3 openEulerAngles = new Vector3(0, 90, 0);
|
|
|
|
[Tooltip("Rotation when the shop is CLOSED.")]
|
|
public Vector3 closedEulerAngles = new Vector3(0, 0, 0);
|
|
|
|
public float swingSpeed = 2f;
|
|
|
|
public override string InteractionPrompt
|
|
{
|
|
get
|
|
{
|
|
if (GameManager.Instance != null && GameManager.Instance.IsShopOpen)
|
|
{
|
|
return "Close Shop";
|
|
}
|
|
else if (GameManager.Instance != null && GameManager.Instance.IsNightTime)
|
|
{
|
|
return "Open Shop (Risk Robbery!)";
|
|
}
|
|
return "Open Shop";
|
|
}
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
if (Instance != null && Instance != this) Destroy(this);
|
|
else Instance = this;
|
|
|
|
if (GameManager.Instance != null)
|
|
{
|
|
GameManager.Instance.OnShopStateChanged += UpdateDoorPhysicalState;
|
|
// Snap to current state immediately
|
|
UpdateDoorPhysicalState(GameManager.Instance.IsShopOpen, true);
|
|
}
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
if (GameManager.Instance != null)
|
|
{
|
|
GameManager.Instance.OnShopStateChanged -= UpdateDoorPhysicalState;
|
|
}
|
|
}
|
|
|
|
public override void Interact(PlayerController player)
|
|
{
|
|
base.Interact(player);
|
|
|
|
if (GameManager.Instance == null) return;
|
|
|
|
if (GameManager.Instance.IsShopOpen)
|
|
{
|
|
GameManager.Instance.CloseShop();
|
|
}
|
|
else
|
|
{
|
|
GameManager.Instance.OpenShop();
|
|
}
|
|
}
|
|
|
|
private void UpdateDoorPhysicalState(bool isOpen)
|
|
{
|
|
UpdateDoorPhysicalState(isOpen, false);
|
|
}
|
|
|
|
private void UpdateDoorPhysicalState(bool isOpen, bool instant)
|
|
{
|
|
StopAllCoroutines();
|
|
|
|
Vector3 targetEuler = isOpen ? openEulerAngles : closedEulerAngles;
|
|
|
|
if (instant)
|
|
{
|
|
if (doorModel != null) doorModel.localRotation = Quaternion.Euler(targetEuler);
|
|
}
|
|
else
|
|
{
|
|
StartCoroutine(SwingDoor(targetEuler));
|
|
}
|
|
}
|
|
|
|
private IEnumerator SwingDoor(Vector3 targetEuler)
|
|
{
|
|
if (doorModel == null) yield break;
|
|
|
|
Quaternion startRot = doorModel.localRotation;
|
|
Quaternion endRot = Quaternion.Euler(targetEuler);
|
|
float t = 0;
|
|
|
|
while (t < 1f)
|
|
{
|
|
t += Time.deltaTime * swingSpeed;
|
|
doorModel.localRotation = Quaternion.Slerp(startRot, endRot, t);
|
|
yield return null;
|
|
}
|
|
doorModel.localRotation = endRot;
|
|
}
|
|
}
|
|
}
|
|
|