using System; using System.Threading; using BriarQueen.Data.Identifiers; using BriarQueen.Framework.Events.Save; using BriarQueen.Framework.Events.UI; using BriarQueen.Framework.Managers.Levels.Data; using BriarQueen.Framework.Managers.Player.Data; using BriarQueen.Framework.Managers.UI; using Cysharp.Threading.Tasks; using PrimeTween; using UnityEngine; namespace BriarQueen.Game.Items.Environment.General.Book { public class BookTrigger : BaseItem { [Header("References")] [SerializeField] private AssetItemKey _bookAssetID; [SerializeField] private BookEntryID _bookEntryID; [Header("Book Interface")] [SerializeField] private BookInterface _bookInterface; private CancellationTokenSource _displayCts; private Sequence _displaySequence; public override string InteractableName => "Worn Book"; public override UICursorService.CursorStyle ApplicableCursorStyle => UICursorService.CursorStyle.Inspect; public override async UniTask OnInteract(ItemDataSo item = null) { if (item != null) { EventCoordinator.Publish(new DisplayInteractEvent(InteractEventIDs.Get(ItemInteractKey.CantUseItem))); return; } if (!CheckEmptyHands()) return; await InstantiateBookInterface(); } private async UniTask InstantiateBookInterface() { Debug.Log($"Instantiating {_bookAssetID}"); if (!AssetRegistry.TryGetReference(AssetKeyIdentifiers.Get(_bookAssetID), out var assetReference) || assetReference == null) return; var bookObj = await AddressableManager.InstantiateAsync(assetReference); if (bookObj == null) return; _bookInterface = bookObj.GetComponent(); if (_bookInterface == null) return; _bookInterface.CanvasGroup.alpha = 0f; _bookInterface.CanvasGroup.blocksRaycasts = false; _bookInterface.CanvasGroup.interactable = false; _bookInterface.Initialise(this); Debug.Log($"Instantiated {_bookAssetID}"); CancelTweenIfRunning(); _displayCts = new CancellationTokenSource(); _displaySequence = Sequence.Create(useUnscaledTime: true) .Group(Tween.Alpha(_bookInterface.CanvasGroup, new TweenSettings { endValue = 1f, settings = new TweenSettings { duration = 0.8f } })); try { await _displaySequence.ToUniTask(cancellationToken: _displayCts.Token); } catch (OperationCanceledException) { } _bookInterface.CanvasGroup.blocksRaycasts = true; _bookInterface.CanvasGroup.interactable = true; ShowTutorialIfNeeded(); UnlockCodexEntry(); } private void ShowTutorialIfNeeded() { TutorialService.DisplayTutorial(TutorialPopupID.ExitItems); } private void UnlockCodexEntry() { PlayerManager.UnlockCodexEntry(CodexEntryIDs.Get(_bookEntryID)); } public async UniTask CloseBookInterface() { if (_bookInterface == null) return; CancelTweenIfRunning(); _displayCts = new CancellationTokenSource(); _displaySequence = Sequence.Create(useUnscaledTime: true) .Group(Tween.Alpha(_bookInterface.CanvasGroup, new TweenSettings { startValue = _bookInterface.CanvasGroup.alpha, endValue = 0f, settings = new TweenSettings { duration = 0.6f } })); try { await _displaySequence.ToUniTask(cancellationToken: _displayCts.Token); } catch (OperationCanceledException) { } await DestructionService.Destroy(_bookInterface.gameObject); _bookInterface = null; } private void CancelTweenIfRunning() { if (_displaySequence.isAlive) _displaySequence.Complete(); DisposeCts(); } private void DisposeCts() { if (_displayCts == null) return; _displayCts.Cancel(); _displayCts.Dispose(); _displayCts = null; } } }