Files
MojSadUnity/Assets/Code/Infrastructure/App/PapaInstaller.cs
T
2026-02-26 13:19:15 +03:00

142 lines
4.9 KiB
C#

using Code.Infrastructure.Audio;
using Code.Infrastructure.Configs;
using Code.Infrastructure.Input;
using Code.Infrastructure.Loadings;
using Code.Infrastructure.SceneLoader;
using Code.Infrastructure.UI;
using Reflex.Core;
using Reflex.Enums;
using Reflex.Injectors;
using UnityEngine;
using UnityEngine.Rendering.Universal;
using Resolution = Reflex.Enums.Resolution;
namespace Code.Infrastructure.App
{
public class PapaInstaller : MonoBehaviour, IInstaller,
ISceneConfigsLoadedHandle,
IInputServiceInitializedHandle,
IUIInitializedHandle,
IUIPanelLoaded,
IAudioSourcePoolInitializedHandle,
IAudioConfigsLoadedHandle,
ILayerMasksInitializedHandle
{
public void InstallBindings(ContainerBuilder containerBuilder)
{
InitLoadings();
containerBuilder.RegisterType(typeof(EventBus.EventBus), Lifetime.Singleton, Resolution.Eager);
containerBuilder.RegisterType(typeof(ConfigLocator), new[] { typeof(IConfigLocator) }, Lifetime.Singleton,
Resolution.Eager);
containerBuilder.RegisterType(typeof(SceneLoader.SceneLoader), Lifetime.Singleton, Resolution.Eager);
containerBuilder.RegisterType(typeof(InputService), new[] { typeof(IInputService) }, Lifetime.Singleton,
Resolution.Eager);
containerBuilder.OnContainerBuilt += Bootstrap;
containerBuilder.RegisterType(typeof(AppStateMachine), Lifetime.Singleton, Resolution.Lazy);
containerBuilder.RegisterType(typeof(AppStateMachineContext), Lifetime.Singleton, Resolution.Lazy);
containerBuilder.RegisterType(typeof(UIFactory), new[] { typeof(IUIFactory) }, Lifetime.Singleton,
Resolution.Eager);
containerBuilder.RegisterType(typeof(UIService), new[] { typeof(IUIService) }, Lifetime.Singleton,
Resolution.Eager);
containerBuilder.RegisterType(typeof(AudioFactory), new[] { typeof(IAudioFactory) }, Lifetime.Singleton,
Resolution.Eager);
containerBuilder.RegisterType(typeof(AudioSourcePool), new[] { typeof(IAudioSourcePool) }, Lifetime.Singleton,
Resolution.Eager);
containerBuilder.RegisterType(typeof(AudioService), new[] { typeof(IAudioService) }, Lifetime.Singleton,
Resolution.Eager);
}
private void InitLoadings()
{
// UIFactory
// Logo
// Scene configs
// Input service
// Audio Factory
// Audio Pool
// Layer masks
_initialLoading = new Loading(7, () => EssentialLoadEnded(_thatRightContainer));
}
private Loading _initialLoading;
private Container _thatRightContainer;
private void Bootstrap(Container container)
{
_thatRightContainer = container;
container.Resolve<EventBus.EventBus>().Subscribe(this);
ConstructorInjector.Construct(typeof(LayerMasks), container);
}
private void EssentialLoadEnded(Container container)
{
container.Resolve<EventBus.EventBus>().Unsubscribe(this);
container.Resolve<AppStateMachine>().SwitchState(AppStates.Bootstrap)
.AllowStateTransition(AppStates.Gameplay)
.AllowStateTransition(AppStates.Platformer);
container.Resolve<AppStateMachineContext>().StateMachine = container.Resolve<AppStateMachine>();
}
public void SceneConfigsLoaded()
{
_initialLoading.DoProgress();
}
public void InputServiceInitialized()
{
_initialLoading.DoProgress();
}
public void UIInitialized()
{
_initialLoading.DoProgress();
_thatRightContainer.Resolve<IUIService>().TryLoadPanel(UIPanelType.Logo);
_thatRightContainer.Resolve<IUIService>().TryLoadPanel(UIPanelType.Loading);
}
public void UILoaded(UIPanelType uiPanelType)
{
switch (uiPanelType)
{
case UIPanelType.Logo:
_thatRightContainer.Resolve<IUIService>().OpenPanel(UIPanelType.Logo);
_initialLoading.DoProgress();
break;
case UIPanelType.Loading:
// _thatRightContainer.Resolve<IUIService>().OpenPanel(UIPanelType.Logo);
break;
default:
Application.Quit();
break;
}
}
public void AudioSourcePoolInitialized()
{
_initialLoading.DoProgress();
}
public void AudioConfigsLoaded()
{
_initialLoading.DoProgress();
}
public void LayerMasksInitialized()
{
_initialLoading.DoProgress();
}
}
}