131 lines
4.7 KiB
C#
131 lines
4.7 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using BITKit;
|
|
using BITKit.Entities;
|
|
using Cysharp.Threading.Tasks;
|
|
using Microsoft.Extensions.Logging;
|
|
using Net.BITKit.Chat;
|
|
using Net.BITKit.Localization;
|
|
using Net.BITKit.UX.SnackBar;
|
|
using Net.Project.B.AI;
|
|
using Net.Project.B.AI.Dialogue;
|
|
using Net.Project.B.Damage;
|
|
using Newtonsoft.Json;
|
|
using Project.B.CharacterController;
|
|
using UnityEngine;
|
|
|
|
namespace Net.Project.B.Dialogue
|
|
{
|
|
public class WorldRangedDialogueService
|
|
{
|
|
private readonly ILocalizationService _localizationService;
|
|
private readonly IDialogueService _dialogueService;
|
|
private readonly IEntitiesService _entitiesService;
|
|
private readonly IChatService _chatService;
|
|
private readonly IAiDialogueService _dialogueAiService;
|
|
private readonly IDamageService _damageService;
|
|
private readonly ILogger<WorldRangedDialogueService> _logger;
|
|
private readonly ISnackBar _snackBar;
|
|
|
|
public WorldRangedDialogueService(IDialogueService dialogueService, IEntitiesService entitiesService, IChatService chatService, IAiDialogueService dialogueAiService, IDamageService damageService, ILogger<WorldRangedDialogueService> logger, ISnackBar snackBar, ILocalizationService localizationService)
|
|
{
|
|
_dialogueService = dialogueService;
|
|
_entitiesService = entitiesService;
|
|
_chatService = chatService;
|
|
_dialogueAiService = dialogueAiService;
|
|
_damageService = damageService;
|
|
_logger = logger;
|
|
_snackBar = snackBar;
|
|
_localizationService = localizationService;
|
|
|
|
_dialogueService.OnDialogueStart += OnDialogueStart;
|
|
_chatService.OnMessageReceived += OnMessageReceived;
|
|
}
|
|
|
|
private void OnMessageReceived(ChatMessage obj)
|
|
{
|
|
if(obj.FromUserId != Environment.UserName)return;
|
|
|
|
foreach (var (entity,_,transform) in _entitiesService.QueryComponents<IEntity,LocalPlayerComponent,Transform>())
|
|
{
|
|
var playerPos = transform.position;
|
|
|
|
var closes = _entitiesService
|
|
.QueryComponents<IEntity, AIHuman, ICharacterController>()
|
|
.ToArray()
|
|
.OrderBy(x => Vector3.Distance(playerPos, x.Item3.Position));
|
|
|
|
if(closes.FirstOrDefault() is var (closeEntity, _, characterController))
|
|
{
|
|
var distance = Vector3.Distance(playerPos, characterController.Position);
|
|
|
|
if (distance > 3f) continue;
|
|
|
|
Chat(entity,closeEntity,obj.Content);
|
|
|
|
return;
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
private async void Chat(IEntity player,IEntity other, string message)
|
|
{
|
|
var reply = await _dialogueAiService.DialogueAsync(0, message);
|
|
|
|
try
|
|
{
|
|
var data = JsonConvert.DeserializeObject<dynamic>(reply);
|
|
|
|
int score = data.score;
|
|
|
|
if (score < 60)
|
|
{
|
|
_damageService.CreateDamageAsync(other.Id, player.Id, score, null, default);
|
|
}
|
|
|
|
_dialogueService.CreateDialogue(new DialogueData()
|
|
{
|
|
ActorIdentity = other.Id,
|
|
Text = data.message,
|
|
});
|
|
|
|
var placerHolder = _localizationService.GetLocalizedString("#Try_Speak");
|
|
|
|
_snackBar.Add($"<b>{placerHolder}:</b>\n{(string)data.suggestion}");
|
|
|
|
_logger.LogInformation(reply);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Debug.LogWarning(reply);
|
|
throw;
|
|
}
|
|
|
|
}
|
|
|
|
|
|
private UniTask OnDialogueStart(IDialogueData arg)
|
|
{
|
|
/*
|
|
if(_entitiesService.TryGetEntity(arg.ActorIdentity,out var dialogueEntity) is false)return UniTask.CompletedTask;
|
|
|
|
if(dialogueEntity.ServiceProvider.QueryComponents(out ICharacterController dialogueCc) is false)return UniTask.CompletedTask;
|
|
|
|
foreach (var (entity,_,characterController) in _entitiesService.QueryComponents<IEntity,LocalPlayerComponent,ICharacterController>())
|
|
{
|
|
|
|
var distance = Vector3.Distance(dialogueCc.Position, characterController.Position);
|
|
if(distance > 2f)continue;
|
|
|
|
return UniTask.CompletedTask;
|
|
}
|
|
*/
|
|
return UniTask.CompletedTask;
|
|
}
|
|
}
|
|
|
|
}
|