67 lines
2.3 KiB
C#
67 lines
2.3 KiB
C#
![]() |
using System;
|
||
|
using System.Collections;
|
||
|
using System.Collections.Concurrent;
|
||
|
using System.Collections.Generic;
|
||
|
using BITKit;
|
||
|
using BITKit.Entities;
|
||
|
using BITKit.UX;
|
||
|
using Net.Project.B.Quest;
|
||
|
using UnityEngine;
|
||
|
using UnityEngine.UIElements;
|
||
|
|
||
|
namespace Net.Project.B.UX
|
||
|
{
|
||
|
public class UXQuest<TPanel> : UIToolkitSubPanel<TPanel>, IUXQuest where TPanel : IUXPanel
|
||
|
{
|
||
|
private readonly IEntitiesService _entitiesService;
|
||
|
private readonly IQuestService _questService;
|
||
|
|
||
|
private readonly ConcurrentDictionary<int, VisualElement> _visualElements = new();
|
||
|
|
||
|
private VisualTreeAsset _template;
|
||
|
[UXBindPath("quest-container")]
|
||
|
private VisualElement _questContainer;
|
||
|
public UXQuest(IServiceProvider serviceProvider, IQuestService questService, IEntitiesService entitiesService) : base(serviceProvider)
|
||
|
{
|
||
|
_questService = questService;
|
||
|
_entitiesService = entitiesService;
|
||
|
|
||
|
_questService.OnQuestCreatedOrUpdated += OnQuestCreatedOrUpdated;
|
||
|
}
|
||
|
|
||
|
private void OnQuestCreatedOrUpdated(IQuestData arg1, QuestState arg2, QuestState arg3)
|
||
|
{
|
||
|
switch (arg3)
|
||
|
{
|
||
|
case QuestState.Active:
|
||
|
var container = _questContainer.Create(_template);
|
||
|
container.Get<Label>().text = arg1.Name;
|
||
|
container.Get<Label>(1).text = arg1.Description;
|
||
|
if (_entitiesService.TryGetEntity(arg1.Identity, out var questEntity) &&
|
||
|
questEntity.ServiceProvider.QueryComponents(out IQuestNode questNode))
|
||
|
{
|
||
|
container.AddToClassList(questNode.GetType().Name);
|
||
|
}
|
||
|
|
||
|
_visualElements[arg1.Identity] = container;
|
||
|
break;
|
||
|
default:
|
||
|
if (_visualElements.TryRemove(arg1.Identity, out var ve))
|
||
|
{
|
||
|
ve.RemoveFromHierarchy();
|
||
|
_visualElements.TryRemove(arg1.Identity, out _);
|
||
|
}
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
protected override void OnInitiated()
|
||
|
{
|
||
|
base.OnInitiated();
|
||
|
_template = _questContainer.Query<TemplateContainer>().First().templateSource;
|
||
|
|
||
|
_questContainer.Clear();
|
||
|
}
|
||
|
}
|
||
|
}
|