using System; using System.Collections; using System.Collections.Generic; using System.Threading; using BITKit; using BITKit.Entities; using BITKit.Tween; using BITKit.UX; using Cysharp.Threading.Tasks; using Net.Project.B.Level; using Unity.Mathematics; using UnityEngine; using UnityEngine.UIElements; namespace Net.Project.B.UX { public class UXLevelUp : UIToolkitSubPanel,IDisposable,IUXLevelUp where TPanel:IUXPanel { private readonly IEntitiesService _entitiesService; [UXBindPath("level-progress")] private ProgressBar _levelProgress; [UXBindPath("exp-label")] private Label _expLabel; private CancellationTokenSource _cancellationTokenSource = new(); private void SetActive(bool active) { _levelProgress?.SetActive(active); _expLabel?.SetActive(active); } public UXLevelUp(IServiceProvider serviceProvider, IEntitiesService entitiesService) : base(serviceProvider) { _entitiesService = entitiesService; _entitiesService.OnAdd += OnAdd; } protected override void OnInitiated() { base.OnInitiated(); SetActive(false); } private void OnAdd(IEntity obj) { if(obj.ServiceProvider.QueryComponents(out LocalPlayerComponent _,out LevelComponent levelComponent) is false)return; levelComponent.OnExpChanged += OnExpChanged; return; async void OnExpChanged(int prev, int next) { Debug.Log($"On Exp Changed,{prev} to {next},current is {levelComponent.Exp.x}/{levelComponent.Exp.y}"); SetActive(true); try { try { if (_cancellationTokenSource.IsCancellationRequested) { _cancellationTokenSource = new(); _levelProgress.title = $"Level:{levelComponent.Level}\t{prev}/{levelComponent.Exp.y}"; _levelProgress.value = next; _levelProgress.highValue = levelComponent.Exp.y; _expLabel.text = $"+{math.abs(next - prev)}"; await BITween.Lerp(x => { _expLabel.SetOpacity(x); _levelProgress.SetOpacity(x); }, 0f, 1,0.25f, Mathf.Lerp, _cancellationTokenSource.Token); await UniTask.Delay(TimeSpan.FromSeconds(0.25f), cancellationToken: _cancellationTokenSource.Token); } else { _cancellationTokenSource.Cancel(); _cancellationTokenSource.Dispose(); _cancellationTokenSource = new(); _expLabel.SetOpacity(1); _levelProgress.SetOpacity(1); } } finally { } await UniTask.WhenAll( BITween.MoveToForward(x => { _levelProgress.title = $"Level:{levelComponent.Level}\t{(int)x}/{levelComponent.Exp.y}"; _levelProgress.value = x; }, (float)prev, next, Time.deltaTime*1000, Mathf.MoveTowards, _cancellationTokenSource.Token), BITween.MoveToForward(x=>_expLabel.text = $"+{(int)x}",(float)math.abs(next-prev),0,Time.deltaTime*1000,Mathf.MoveTowards,_cancellationTokenSource.Token) ); await BITween.Lerp(x => { _expLabel.SetOpacity(x); _levelProgress.SetOpacity(x); }, 1f, 0,2, Mathf.Lerp, _cancellationTokenSource.Token); SetActive(false); _cancellationTokenSource.Cancel(); } catch (OperationCanceledException) { }catch(Exception exception) { Debug.LogException(exception); } } } public void Dispose() { _cancellationTokenSource?.Cancel(); _cancellationTokenSource?.Dispose(); } } }