82 lines
2.8 KiB
C#
82 lines
2.8 KiB
C#
![]() |
using System;
|
||
|
using System.Collections;
|
||
|
using System.Collections.Generic;
|
||
|
using BITKit;
|
||
|
using BITKit.Entities;
|
||
|
using BITKit.Tween;
|
||
|
using Cysharp.Threading.Tasks;
|
||
|
using Net.Project.B.Interaction;
|
||
|
using UnityEngine;
|
||
|
using UnityEngine.Serialization;
|
||
|
|
||
|
namespace Net.Project.B.WorldNode
|
||
|
{
|
||
|
public class UnityLootContainerVisualService
|
||
|
{
|
||
|
private readonly IEntitiesService _entitiesService;
|
||
|
|
||
|
private readonly IWorldInteractionService _interactionService;
|
||
|
|
||
|
private readonly HashSet<int> _triggered = new();
|
||
|
|
||
|
public UnityLootContainerVisualService(IEntitiesService entitiesService, IWorldInteractionService interactionService)
|
||
|
{
|
||
|
_entitiesService = entitiesService;
|
||
|
_interactionService = interactionService;
|
||
|
|
||
|
_entitiesService.OnAdd += OnAdd;
|
||
|
|
||
|
_interactionService.OnInteraction += OnInteraction;
|
||
|
}
|
||
|
|
||
|
private void OnInteraction(object arg1, IWorldInteractable arg2, WorldInteractionProcess arg3, object arg4)
|
||
|
{
|
||
|
if(arg3 is not WorldInteractionProcess.Performed)return;
|
||
|
|
||
|
if(_entitiesService.TryGetEntity(arg2.Id,out var entity) is false)return;
|
||
|
if(entity.ServiceProvider.QueryComponents(out IRuntimeItemContainer runtimeItemContainer,
|
||
|
out LootContainerVisualNode lootContainerVisualNode) is false)return;
|
||
|
|
||
|
var transform = lootContainerVisualNode.visualJoint;
|
||
|
|
||
|
if (transform)
|
||
|
{
|
||
|
if (_triggered.Add(entity.Id))
|
||
|
{
|
||
|
BITween.Lerp(x => transform.localPosition = x, transform.localPosition,
|
||
|
lootContainerVisualNode.emptyJointPosition, 0.5f, Vector3.Lerp, entity.CancellationToken).Forget();
|
||
|
|
||
|
BITween.Lerp(x => transform.localRotation = x, transform.localRotation,
|
||
|
Quaternion.Euler(lootContainerVisualNode.emptyJointEuler), 0.5f, Quaternion.Lerp,
|
||
|
entity.CancellationToken).Forget();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
private void OnAdd(IEntity entity)
|
||
|
{
|
||
|
if (entity.ServiceProvider.QueryComponents(out IRuntimeItemContainer runtimeItemContainer,
|
||
|
out LootContainerVisualNode lootContainerVisualNode))
|
||
|
{
|
||
|
runtimeItemContainer.OnRelease += OnRelease;
|
||
|
}
|
||
|
|
||
|
return;
|
||
|
void OnRelease(bool release)
|
||
|
{
|
||
|
if(!release)return;
|
||
|
|
||
|
if (lootContainerVisualNode.visualModel)
|
||
|
{
|
||
|
lootContainerVisualNode.visualModel.gameObject.SetActive(runtimeItemContainer.ItemDictionary.Count>0);
|
||
|
}
|
||
|
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
}
|
||
|
}
|
||
|
|