Net.Like.Xue.Tokyo/Assets/Artists/Scripts/UX/UXHud.cs

274 lines
9.3 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using BITKit;
using BITKit.Entities;
using BITKit.Mod;
using BITKit.UX;
using BITKit.UX.Hotkey;
using BITKit.WorldNode;
using Cysharp.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Net.Project.B.Buff;
using Net.Project.B.Emoji;
using Net.Project.B.Health;
using Net.Project.B.Interaction;
using Net.Project.B.Quest;
using Net.Project.B.UX;
using Project.B.Entities;
using Project.B.Map;
using Project.B.Player;
using UnityEditor;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.UIElements;
namespace Net.Like.Xue.Tokyo.UX
{
public class UXHud : UIToolKitPanel,IUXHud
{
private readonly IEntitiesService _entitiesService;
private readonly IPlayerFactory _playerFactory;
private readonly IGameMapService _gameMapService;
private readonly IWorldInteractionService _interactionService;
private readonly IQuestService _questService;
private readonly UXRadialMenu _radialMenu;
private readonly IUXKeyMap<InputAction> _uxKeyMap;
[Inject]
private IEmojiService<AnimationClip> _emojiService;
[UXBindPath("health-bar")]
private ProgressBar _healthBar;
[UXBindPath("hunger-bar")]
private ProgressBar _hungerBar;
[UXBindPath("thirsty-bar")]
private ProgressBar _thirstyBar;
public UXHud(IUXService uxService, IGameMapService gameMapService, IWorldInteractionService interactionService, IQuestService questService, UXRadialMenu radialMenu, IPlayerFactory playerFactory, IEntitiesService entitiesService, IUXKeyMap<InputAction> uxKeyMap) : base(uxService)
{
_gameMapService = gameMapService;
_interactionService = interactionService;
_questService = questService;
_radialMenu = radialMenu;
_playerFactory = playerFactory;
_entitiesService = entitiesService;
_uxKeyMap = uxKeyMap;
_gameMapService.OnMapChanged += OnMapChanged;
_interactionService.OnInteraction += OnInteraction;
Data.AddListener<float>("Time",x=>
{
_timeSlider?.SetValueWithoutNotify(x);
});
_questService.OnQuestCreatedOrUpdated +=(x,y,z)=>_questQueue.Enqueue(new(x,y,z));
foreach (var (index,quest) in _questService.Quests)
{
_questQueue.Enqueue(new(quest,QuestState.Inactive,quest.CurrentState));
}
_playerFactory.OnEntityCreated += OnEntityCreated;
OnInitiatedAsync += InitiatedAsync;
}
private async UniTask InitiatedAsync()
{
_timeSlider.RegisterValueChangedCallback(x => { Data.Set("Time", x.newValue); });
_questContainer.Clear();
_questTemplate =await ModService.LoadAsset<VisualTreeAsset>("ux_quest_template");
_timeSlider.value = Data.Get<float>("Time");
}
private UniTask OnEntityCreated(string arg1, IEntity arg2)
{
arg2.Inject(this);
arg2.ServiceProvider.QueryComponents(out IHealthComponent healthComponent);
healthComponent.OnHealthChanged += OnHealthChanged;
OnHealthChanged(healthComponent.HealthPoint,healthComponent.HealthPoint);
return UniTask.CompletedTask;
}
private void OnHealthChanged(int arg1, int arg2)
{
if (_healthBar is not null)
{
_healthBar.value = arg2;
}
}
private void OnInteraction(object arg1, IWorldInteractable arg2, WorldInteractionProcess arg3, object arg4)
{
switch (arg3)
{
case WorldInteractionProcess.Hover:
_interactionTips.SetActive(true);
if (_entitiesService.Entities.TryGetValue( arg2.WorldObject.As<GameObject>().GetInstanceID(), out var entity) &&
entity.ServiceProvider.GetService<WorldInfoNode>() is { } infoNode && string.IsNullOrEmpty(infoNode.Name) is false)
{
_interactionTips.text = infoNode.Name;
}
else
{
_interactionTips.text = "互动";
}
break;
default:
_interactionTips.SetActive(false);
break;
}
}
private void OnMapChanged(Guid arg1, string arg2)
{
if (string.IsNullOrEmpty(arg2) is false)
{
UXService.Entry<IUXHud>();
}
}
protected override string DocumentPath => "ux_hud";
public override bool AllowInput => true;
public override bool AllowCursor => false;
[UXBindPath("interaction-tips")]
private Label _interactionTips;
[UXBindPath("time-slider")]
private Slider _timeSlider;
[UXBindPath("quest-container")]
private VisualElement _questContainer;
private Camera _minimapCamera;
private Camera _mainCamera;
private VisualTreeAsset _questTemplate;
private readonly Queue<(IQuestData questData, QuestState oldQuestState, QuestState newQuestState)> _questQueue =
new();
private readonly Dictionary<int, VisualElement> _questContainers = new();
public override async UniTask EntryAsync()
{
await base.EntryAsync();
_interactionTips.SetActive(false);
}
public override void OnTick(float deltaTime)
{
base.OnTick(deltaTime);
if(RootVisualElement is null)return;
foreach (var (_,buffComponent) in _entitiesService.QueryComponents<LocalPlayerComponent,IBuffComponent>())
{
foreach (var buff in buffComponent.Buffs.Span)
{
switch (buff)
{
case Hunger:
_hungerBar.value = buff.Value;
break;
case Thirsty:
_thirstyBar.value = buff.Value;
break;
}
}
}
if (BITInputSystem.AllowInput.Allow)
{
if (Keyboard.current is { capsLockKey: { wasPressedThisFrame: true } } or {mKey:{wasPressedThisFrame:true}})
{
UXService.Entry<IUXMap>();
}else if (Keyboard.current is { f1Key: { wasPressedThisFrame: true } })
{
CreateEmojis();
}
}
if (_questContainer is not null && _questTemplate!=null)
{
while (_questQueue.TryDequeue(out var quest))
{
if (quest.questData.IsCompleted)
{
if (_questContainers.TryGetValue(quest.questData.Identity, out var visualElement))
{
visualElement.RemoveFromHierarchy();
}
}
else
{
var visualElement = _questContainer.Create(_questTemplate);
visualElement.Get<Label>(0).text = quest.questData.Name;
visualElement.Get<Label>(1).text = quest.questData.Description;
_questContainers.TryAdd(quest.questData.Identity, visualElement);
}{}
}
}
}
private async void CreateEmojis()
{
var collection = new HotkeyCollection();
_radialMenu.HotkeyCollection = collection;
foreach (var emojiData in await _emojiService.GetAllEmojis())
{
collection.Register(new HotkeyProvider()
{
Name = emojiData.Name,
Description = emojiData.Description,
Enabled = true,
OnPerform = () => _emojiService.Play(emojiData),
});
}
UXService.Entry(_radialMenu);
}
protected override void OnPanelEntry()
{
base.OnPanelEntry();
InputActionGroup.RegisterCallback(_uxKeyMap.CancelKey, OnCancel);
InputActionGroup.RegisterCallback(_uxKeyMap.InventoryKey,OnInventory);
}
private void OnInventory(InputAction.CallbackContext obj)
{
if(obj.JustPressed() is false)return;
UXService.Entry<IUXInventory>();
}
private void OnCancel(InputAction.CallbackContext obj)
{
if (obj.JustPressed() is false) return;
UXService.Entry<UXEscMenu>();
}
protected override void OnPanelExit()
{
base.OnPanelExit();
InputActionGroup.UnRegisterCallback(_uxKeyMap.CancelKey, OnCancel);
InputActionGroup.UnRegisterCallback(_uxKeyMap.InventoryKey, OnInventory);
}
}
}