71 lines
2.8 KiB
C#
71 lines
2.8 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using BITKit.Entities;
|
|
using BITKit.WorldNode;
|
|
using Cysharp.Threading.Tasks;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Logging;
|
|
using Net.Project.B.Interaction;
|
|
using Project.B.CharacterController;
|
|
using Project.B.Entities;
|
|
using Project.B.Map;
|
|
using UnityEngine;
|
|
|
|
namespace Net.Like.Xue.Tokyo.GameService
|
|
{
|
|
public class GamePortalPlayerService
|
|
{
|
|
private readonly ILogger<GamePortalPlayerService> _logger;
|
|
private readonly IWorldInteractionService _interactionService;
|
|
private readonly IPlayerFactory _playerFactory;
|
|
private readonly IWorldNodeService _worldNodeService;
|
|
private readonly IGameMapService _gameMapService;
|
|
private UniTaskCompletionSource<IEntity> _waitPlayer;
|
|
|
|
public GamePortalPlayerService(IWorldInteractionService interactionService, IWorldNodeService worldNodeService, IGameMapService gameMapService, ILogger<GamePortalPlayerService> logger, IPlayerFactory playerFactory)
|
|
{
|
|
_interactionService = interactionService;
|
|
_worldNodeService = worldNodeService;
|
|
_gameMapService = gameMapService;
|
|
_logger = logger;
|
|
_playerFactory = playerFactory;
|
|
|
|
_interactionService.OnInteraction += OnInteraction;
|
|
_playerFactory.OnEntityCreated += OnEntityCreated;
|
|
}
|
|
|
|
private UniTask OnEntityCreated(string arg1, IEntity arg2)
|
|
{
|
|
_waitPlayer?.TrySetResult(arg2);
|
|
return UniTask.CompletedTask;
|
|
}
|
|
|
|
private async void OnInteraction(object arg1, IWorldInteractable arg2, WorldInteractionProcess arg3, object arg4)
|
|
{
|
|
if(arg1 is IEntity entity is false)return;
|
|
if(arg3 is not WorldInteractionProcess.Performed)return;
|
|
if(entity.ServiceProvider.GetRequiredService<ICharacterController>() is not {} characterController)return;
|
|
if (_worldNodeService.WorldNodes.TryGetValue(arg2.Id, out var node) is false) return;
|
|
|
|
if(node.OfType<WorldPortalNode>().FirstOrDefault() is not {MapName:not null} portalNode)return;
|
|
|
|
if (string.IsNullOrEmpty(portalNode.MapName?.Value) is false)
|
|
{
|
|
_waitPlayer = new();
|
|
await _gameMapService.StartMapAsync(portalNode.MapName.Value);
|
|
|
|
var player = await _waitPlayer.Task;
|
|
characterController = player.ServiceProvider.GetRequiredService<ICharacterController>();
|
|
}
|
|
|
|
await Task.Delay(300);
|
|
|
|
characterController.Position = portalNode.Position;
|
|
Vector3 eulerAngle = portalNode.EulerAngle;
|
|
characterController.Rotation = Quaternion.Euler(eulerAngle);
|
|
}
|
|
}
|
|
}
|