riversong code showcase
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
using System;
|
||||
using Cysharp.Threading.Tasks;
|
||||
|
||||
namespace DanieleMarotta.RiversongCodeShowcase
|
||||
{
|
||||
public class TitleScreenUIController : UIControllerSystem<TitleScreenUIView>, IDisposable
|
||||
{
|
||||
[InjectService]
|
||||
private BuildVersionAsset _buildVersion;
|
||||
|
||||
[InjectService]
|
||||
private ISignalBus _signalBus;
|
||||
|
||||
public TitleScreenUIController(IServiceLocator serviceLocator) : base(serviceLocator)
|
||||
{
|
||||
}
|
||||
|
||||
protected override TitleScreenUIView View => UIRoot.GetView<TitleScreenUIView>();
|
||||
|
||||
public override async UniTask InitializeAsync()
|
||||
{
|
||||
await base.InitializeAsync();
|
||||
|
||||
_signalBus.Subscribe<GameInitializationCompletedSignal>(OnGameInitializationCompleted);
|
||||
_signalBus.Subscribe<WorldReadySignal>(OnWorldReady);
|
||||
|
||||
View.StartGame += OnStartGame;
|
||||
View.SetVersion($"v{_buildVersion.CurrentVersion}");
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_signalBus.Unsubscribe<GameInitializationCompletedSignal>(OnGameInitializationCompleted);
|
||||
_signalBus.Unsubscribe<WorldReadySignal>(OnWorldReady);
|
||||
|
||||
View.StartGame -= OnStartGame;
|
||||
}
|
||||
|
||||
private void OnGameInitializationCompleted(GameInitializationCompletedSignal signal)
|
||||
{
|
||||
_ = View.PlayIntroAsync();
|
||||
}
|
||||
|
||||
private void OnStartGame()
|
||||
{
|
||||
FadeAndStartGameAsync().Forget();
|
||||
}
|
||||
|
||||
private async UniTask FadeAndStartGameAsync()
|
||||
{
|
||||
await View.PlayStartFadeAsync(1);
|
||||
|
||||
_signalBus.Raise(new GameStartedSignal());
|
||||
}
|
||||
|
||||
private void OnWorldReady(WorldReadySignal signal)
|
||||
{
|
||||
_ = HideWithDelay();
|
||||
}
|
||||
|
||||
private async UniTask HideWithDelay()
|
||||
{
|
||||
await UniTask.NextFrame();
|
||||
|
||||
View.Show(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
118
Source/Riversong/Game/UI/Panels/TitleScreen/TitleScreenUIView.cs
Normal file
118
Source/Riversong/Game/UI/Panels/TitleScreen/TitleScreenUIView.cs
Normal file
@@ -0,0 +1,118 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using PrimeTween;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace DanieleMarotta.RiversongCodeShowcase
|
||||
{
|
||||
[UIView("title-screen")]
|
||||
public class TitleScreenUIView : UIView
|
||||
{
|
||||
private VisualElement _fader;
|
||||
|
||||
private VisualElement _logo;
|
||||
|
||||
private VisualElement _panel;
|
||||
|
||||
private Button _startGameButton;
|
||||
|
||||
private Label _versionLabel;
|
||||
|
||||
private CancellationTokenSource _animationCancellation;
|
||||
|
||||
public event Action StartGame;
|
||||
|
||||
public override UniTask InitializeAsync(UIService uiService, VisualElement rootElement)
|
||||
{
|
||||
base.InitializeAsync(uiService, rootElement);
|
||||
|
||||
_fader = rootElement.Q(className: "title-screen__fader");
|
||||
_logo = rootElement.Q(className: "title-screen__logo-image");
|
||||
_panel = rootElement.Q(className: "dialog");
|
||||
_startGameButton = rootElement.Q<Button>(className: "title-screen__start-button");
|
||||
_versionLabel = rootElement.Q<Label>(className: "title-screen__version");
|
||||
|
||||
_fader.style.display = DisplayStyle.None;
|
||||
_fader.style.opacity = 0;
|
||||
_logo.style.display = DisplayStyle.None;
|
||||
_logo.style.opacity = 0;
|
||||
_panel.style.display = DisplayStyle.None;
|
||||
_panel.style.opacity = 0;
|
||||
|
||||
_startGameButton.RegisterCallbackOnce<ClickEvent>(_ => StartGame?.Invoke());
|
||||
|
||||
return UniTask.CompletedTask;
|
||||
}
|
||||
|
||||
public override void Dispose()
|
||||
{
|
||||
_animationCancellation?.Cancel();
|
||||
_animationCancellation?.Dispose();
|
||||
_animationCancellation = null;
|
||||
|
||||
base.Dispose();
|
||||
}
|
||||
|
||||
public void SetVersion(string versionText)
|
||||
{
|
||||
_versionLabel.text = versionText;
|
||||
}
|
||||
|
||||
public async UniTask PlayIntroAsync()
|
||||
{
|
||||
Show(true);
|
||||
|
||||
await UniTask.NextFrame();
|
||||
|
||||
_logo.style.display = DisplayStyle.Flex;
|
||||
_logo.style.top = Length.Percent(50);
|
||||
await UIVisibilityAnimation.PlayShowTween(_logo);
|
||||
|
||||
await UniTask.WaitForSeconds(1);
|
||||
|
||||
await Tween.Custom(50, 33, 0.95f, value => _logo.style.top = Length.Percent(value), Ease.OutCubic);
|
||||
|
||||
await UniTask.WaitForSeconds(1);
|
||||
|
||||
_panel.style.display = DisplayStyle.Flex;
|
||||
await UIVisibilityAnimation.PlayShowTween(_panel);
|
||||
|
||||
_logo.style.translate = new Translate(Length.Percent(-50), Length.Percent(-50));
|
||||
_logo.SetScale(1);
|
||||
|
||||
_animationCancellation = new CancellationTokenSource();
|
||||
PlayLogoIdleAnimationAsync(_animationCancellation.Token).Forget();
|
||||
}
|
||||
|
||||
public async UniTask PlayStartFadeAsync(float duration)
|
||||
{
|
||||
_fader.style.display = DisplayStyle.Flex;
|
||||
await Tween.Custom(0, 1, duration, value => _fader.style.opacity = value, Ease.OutQuad);
|
||||
}
|
||||
|
||||
private async UniTask PlayLogoIdleAnimationAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
float t = 0;
|
||||
|
||||
while (!cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
var sin = Mathf.Sin(t);
|
||||
|
||||
_logo.style.translate = new Translate(Length.Percent(-50), Length.Percent(-50 + 3 * sin));
|
||||
_logo.SetScale(1 - 0.025f * sin);
|
||||
|
||||
t += Time.deltaTime;
|
||||
|
||||
await UniTask.NextFrame(cancellationToken);
|
||||
}
|
||||
}
|
||||
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user