125 lines
4.4 KiB
C#
125 lines
4.4 KiB
C#
![]() |
using System.Collections;
|
||
|
using System.Collections.Generic;
|
||
|
using System.Linq;
|
||
|
using BITKit;
|
||
|
using BITKit.Entities;
|
||
|
using BITKit.Mod;
|
||
|
using BITKit.UX;
|
||
|
using BITKit.WorldNode;
|
||
|
using Cysharp.Threading.Tasks;
|
||
|
using Microsoft.Extensions.DependencyInjection;
|
||
|
using Net.BITKit.Localization;
|
||
|
using Net.Project.B.Interaction;
|
||
|
using Net.Project.B.Inventory;
|
||
|
using Net.Project.B.Item;
|
||
|
using Net.Project.B.WorldNode;
|
||
|
using Project.B.Entities;
|
||
|
using Project.B.UX;
|
||
|
using UnityEngine;
|
||
|
using UnityEngine.UIElements;
|
||
|
|
||
|
namespace Net.Project.B.UX
|
||
|
{
|
||
|
public class UXBuyStation : UIToolKitPanel, IUXBuyStation
|
||
|
{
|
||
|
private readonly ILocalizationService _localizationService;
|
||
|
private readonly IEntitiesService _entitiesService;
|
||
|
private readonly IWorldInteractionService _interactionService;
|
||
|
private readonly IBuyStationService _buyStationService;
|
||
|
|
||
|
protected override string DocumentPath => "ui_buy_station";
|
||
|
public override bool AllowCursor => true;
|
||
|
public override bool CloseWhenClickOutside => true;
|
||
|
|
||
|
private VisualTreeAsset _itemTemplate;
|
||
|
|
||
|
[UXBindPath("weapon-container")]
|
||
|
private VisualElement _weaponContainer;
|
||
|
[UXBindPath("item-container")]
|
||
|
private VisualElement _itemContainer;
|
||
|
|
||
|
private IEntity _entity;
|
||
|
|
||
|
|
||
|
public UXBuyStation(IUXService uxService, IPlayerFactory playerFactory, IWorldInteractionService interactionService, IBuyStationService buyStationService, IEntitiesService entitiesService, ILocalizationService localizationService) : base(uxService)
|
||
|
{
|
||
|
_interactionService = interactionService;
|
||
|
_buyStationService = buyStationService;
|
||
|
_entitiesService = entitiesService;
|
||
|
_localizationService = localizationService;
|
||
|
|
||
|
playerFactory.OnEntityCreated += OnEntityCreated;
|
||
|
|
||
|
_interactionService.OnInteraction += OnInteraction;
|
||
|
|
||
|
OnInitiatedAsync += InitiatedAsync;
|
||
|
}
|
||
|
|
||
|
private async UniTask InitiatedAsync()
|
||
|
{
|
||
|
_itemTemplate = await ModService.LoadAsset<VisualTreeAsset>("ux_item_template");
|
||
|
}
|
||
|
|
||
|
private void OnInteraction(object arg1, IWorldInteractable arg2, WorldInteractionProcess arg3, object arg4)
|
||
|
{
|
||
|
if (arg3 is not WorldInteractionProcess.Performed) return;
|
||
|
if(arg2.WorldObject is not GameObject gameObject)return;
|
||
|
|
||
|
if (_entitiesService.TryGetEntity(gameObject.GetInstanceID(), out var entity) is false) return;
|
||
|
if (entity.ServiceProvider.GetService<UnityBuyStationNode>() is not { } node) return;
|
||
|
|
||
|
UXService.Entry(this);
|
||
|
}
|
||
|
|
||
|
private UniTask OnEntityCreated(string arg1, IEntity arg2)
|
||
|
{
|
||
|
arg2.Inject(this);
|
||
|
|
||
|
_entity = arg2;
|
||
|
|
||
|
return UniTask.CompletedTask;
|
||
|
}
|
||
|
|
||
|
public override async UniTask EntryAsync()
|
||
|
{
|
||
|
await base.EntryAsync();
|
||
|
_weaponContainer.Clear();
|
||
|
_itemContainer.Clear();
|
||
|
foreach (var buyStationComponent in await _buyStationService.GetAllItemList())
|
||
|
{
|
||
|
var item =await UXInventoryUtils.GetScriptableItem(buyStationComponent.ScriptableId);
|
||
|
|
||
|
var container = _itemTemplate.CloneTree();
|
||
|
|
||
|
var isWide = item is ScriptableWeapon { WeaponSlot: (int)PlayerWeaponSlot.Primary or (int)PlayerWeaponSlot.Secondary or (int)PlayerWeaponSlot.Sidearm};
|
||
|
|
||
|
if (isWide)
|
||
|
{
|
||
|
_weaponContainer.Add(container);
|
||
|
container.style.width = 200;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
_itemContainer.Add(container);
|
||
|
}
|
||
|
|
||
|
container.Get<VisualElement>().style.backgroundImage =new StyleBackground(isWide ?item.RectangleIcon ? item.RectangleIcon : item.Icon:item.Icon);
|
||
|
|
||
|
container.Get<Label>().text=_localizationService.GetLocalizedString(item.Name);
|
||
|
|
||
|
container.Get<Button>().tooltip = _localizationService.GetLocalizedString(item.Description);
|
||
|
|
||
|
container.Get<Label>(1).text = buyStationComponent.Price.ToString();
|
||
|
|
||
|
container.Get<Button>().clicked += () =>
|
||
|
{
|
||
|
_buyStationService.BuyItem(_entity.Id, buyStationComponent);
|
||
|
};
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|