88 lines
2.5 KiB
C#
88 lines
2.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Cysharp.Threading.Tasks;
|
|
using UnityEngine;
|
|
|
|
namespace DanieleMarotta.RiversongCodeShowcase
|
|
{
|
|
public class OnboardingPanelUIController : UIControllerSystem<OnboardingPanelUIView>, IDisposable, IUpdatable
|
|
{
|
|
[InjectService]
|
|
private ISignalBus _signalBus;
|
|
|
|
[InjectService]
|
|
private GameConfig _config;
|
|
|
|
[InjectService]
|
|
private ISoundPlayer _soundPlayer;
|
|
|
|
private readonly Queue<OnboardingEvents> _completedEvents = new();
|
|
|
|
private bool _isWaitingToShowMessage;
|
|
|
|
private float _currentMessageTime;
|
|
|
|
public OnboardingPanelUIController(IServiceLocator serviceLocator) : base(serviceLocator)
|
|
{
|
|
}
|
|
|
|
protected override OnboardingPanelUIView View => UIRoot.GetView<OnboardingPanelUIView>();
|
|
|
|
public override async UniTask InitializeAsync()
|
|
{
|
|
await base.InitializeAsync();
|
|
|
|
_signalBus.Subscribe<OnboardingEventCompleted>(OnOnboardingEventCompleted);
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
_signalBus.Unsubscribe<OnboardingEventCompleted>(OnOnboardingEventCompleted);
|
|
}
|
|
|
|
public void Update()
|
|
{
|
|
if (View.IsShowingAnyMessage())
|
|
{
|
|
_currentMessageTime += Time.unscaledDeltaTime;
|
|
if (_currentMessageTime > _config.Onboarding.MessageDuration) View.CloseCurrentMessage();
|
|
return;
|
|
}
|
|
|
|
_currentMessageTime = 0;
|
|
|
|
if (_isWaitingToShowMessage || !_completedEvents.TryDequeue(out var completedEvent)) return;
|
|
|
|
_ = ShowMessageAsync(completedEvent);
|
|
}
|
|
|
|
private async UniTask ShowMessageAsync(OnboardingEvents completedEvent)
|
|
{
|
|
_isWaitingToShowMessage = true;
|
|
|
|
await UniTask.WaitForSeconds(1, true);
|
|
|
|
View.ShowMessage(GetMessage(completedEvent));
|
|
|
|
_soundPlayer.Play(SystemSoundId.OnboardingMessage);
|
|
|
|
_isWaitingToShowMessage = false;
|
|
}
|
|
|
|
private string GetMessage(OnboardingEvents completedEvent)
|
|
{
|
|
foreach (var entry in _config.Onboarding.Messages.Entries)
|
|
{
|
|
if (entry.Event != completedEvent) continue;
|
|
return entry.Text ?? $"Unknown Event {completedEvent}";
|
|
}
|
|
|
|
return $"Unknown Event {completedEvent}";
|
|
}
|
|
|
|
private void OnOnboardingEventCompleted(OnboardingEventCompleted signal)
|
|
{
|
|
_completedEvents.Enqueue(signal.Event);
|
|
}
|
|
}
|
|
} |