Update
This commit is contained in:
90
Assets/Scripts/Interaction/DoorInteractable.cs
Normal file
90
Assets/Scripts/Interaction/DoorInteractable.cs
Normal file
@@ -0,0 +1,90 @@
|
||||
using UnityEngine;
|
||||
using DoorScript; // Namespace của gói Wood Door Pack
|
||||
|
||||
namespace OnlyScove.Scripts
|
||||
{
|
||||
public class DoorInteractable : BaseInteractable
|
||||
{
|
||||
[Header("Door Component (Optional)")]
|
||||
[SerializeField] private Door woodDoorScript;
|
||||
|
||||
[Header("Animator (Optional)")]
|
||||
[SerializeField] private Animator animator;
|
||||
[SerializeField] private string boolParameterName = "IsOpen";
|
||||
|
||||
private bool isOpen = false;
|
||||
|
||||
// Ghi đè Prompt để hiện trạng thái Mở/Đóng tùy vào cửa đang như thế nào
|
||||
public override string InteractionPrompt
|
||||
{
|
||||
get
|
||||
{
|
||||
bool currentOpen = woodDoorScript != null ? woodDoorScript.open : isOpen;
|
||||
return (currentOpen ? "Đóng " : "Mở ") + (interactionData != null ? interactionData.promptText : "Cửa");
|
||||
}
|
||||
}
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
Debug.Log($"[DoorInteractable] Initializing on {gameObject.name}");
|
||||
|
||||
// 1. Tìm Door script (Tìm mọi nơi: bản thân, con, cha)
|
||||
if (woodDoorScript == null) woodDoorScript = GetComponent<Door>();
|
||||
if (woodDoorScript == null) woodDoorScript = GetComponentInChildren<Door>(true);
|
||||
if (woodDoorScript == null) woodDoorScript = GetComponentInParent<Door>();
|
||||
|
||||
if (woodDoorScript != null)
|
||||
{
|
||||
Debug.Log($"[DoorInteractable] SUCCESS: Found Door script on {woodDoorScript.gameObject.name}");
|
||||
|
||||
// Đảm bảo có AudioSource
|
||||
var source = woodDoorScript.GetComponent<AudioSource>();
|
||||
if (source == null) source = woodDoorScript.gameObject.AddComponent<AudioSource>();
|
||||
woodDoorScript.asource = source;
|
||||
|
||||
isOpen = woodDoorScript.open;
|
||||
}
|
||||
|
||||
// 2. Tìm Animator (Tìm mọi nơi)
|
||||
if (animator == null) animator = GetComponent<Animator>();
|
||||
if (animator == null) animator = GetComponentInChildren<Animator>(true);
|
||||
if (animator == null) animator = GetComponentInParent<Animator>();
|
||||
|
||||
// 3. TỰ ĐỘNG TẮT SCRIPT XUNG ĐỘT (CameraOpenDoor) nếu nó đang tồn tại trên Camera
|
||||
// Điều này giúp hệ thống của bạn chiếm quyền điều khiển hoàn toàn
|
||||
var conflictingScript = Object.FindFirstObjectByType<CameraDoorScript.CameraOpenDoor>();
|
||||
if (conflictingScript != null)
|
||||
{
|
||||
Debug.Log("[DoorInteractable] Disabling conflicting CameraOpenDoor script to take full control.");
|
||||
conflictingScript.enabled = false;
|
||||
}
|
||||
}
|
||||
|
||||
protected override void PerformInteraction(PlayerStateMachine player)
|
||||
{
|
||||
Debug.Log($"[Interaction] PerformInteraction CALLED on {gameObject.name}!");
|
||||
|
||||
// 1. Ưu tiên script của Door Pack (Wood Door Script)
|
||||
if (woodDoorScript != null)
|
||||
{
|
||||
Debug.Log($"[Interaction] Calling woodDoorScript.OpenDoor() on {gameObject.name}. Previous state: {woodDoorScript.open}");
|
||||
woodDoorScript.OpenDoor();
|
||||
isOpen = woodDoorScript.open;
|
||||
Debug.Log($"[Interaction] New state: {woodDoorScript.open}");
|
||||
return;
|
||||
}
|
||||
|
||||
// 2. Nếu không có script Pack mới dùng Animator
|
||||
if (animator != null)
|
||||
{
|
||||
isOpen = !isOpen;
|
||||
animator.SetBool(boolParameterName, isOpen);
|
||||
Debug.Log($"[Interaction] Triggered Animator: {boolParameterName} = {isOpen}");
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError($"[Interaction] FAILED: No woodDoorScript or animator found on {gameObject.name}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user