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 _logger; public UnityInteractionService(ILogger 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 InternalOnInteraction; event Action IWorldInteractionService.OnInteraction { add => InternalOnInteraction += value; remove => InternalOnInteraction -= value; } public void Dispose() { } } }