Files
Net.Like.Xue.Tokyo/Packages-Local/Com.Project.B.Unity/Interaction/UnityInteractionService.cs
2025-06-24 23:49:13 +08:00

67 lines
2.1 KiB
C#

using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using BITKit;
using BITKit.Entities;
using Microsoft.Extensions.Logging;
using UnityEngine;
namespace Net.Project.B.Interaction
{
public sealed class UnityInteractionService:IWorldInteractionService,IDisposable
{
private readonly IEntitiesService _entitiesService;
private readonly ILogger<UnityInteractionService> _logger;
public UnityInteractionService(ILogger<UnityInteractionService> logger, IEntitiesService entitiesService)
{
_logger = logger;
_entitiesService = entitiesService;
_entitiesService.OnAdd += OnAdd;
}
private void OnAdd(IEntity obj)
{
if (obj.ServiceProvider.QueryComponents(out IWorldInteractable worldInteractable))
{
if (worldInteractable.WorldObject is null)
{
obj.ServiceProvider.QueryComponents(out GameObject gameObject);
worldInteractable.WorldObject = gameObject;
worldInteractable.Id = obj.Id;
}
}
}
public bool Interaction(object sender, IWorldInteractable interactable,
WorldInteractionProcess process = WorldInteractionProcess.Performed)
{
try
{
InternalOnInteraction?.Invoke(sender, interactable, process, null);
}
catch (Exception e)
{
//_logger.LogWarning(e,$"互动失败:{e.Message}");
_logger.LogCritical(e,e.Message);
return false;
}
return true;
}
private static event Action<object, IWorldInteractable, WorldInteractionProcess, object> InternalOnInteraction;
event Action<object, IWorldInteractable, WorldInteractionProcess, object> IWorldInteractionService.OnInteraction
{
add => InternalOnInteraction += value;
remove => InternalOnInteraction -= value;
}
public void Dispose()
{
}
}
}