Files
Infratest/Assets/Code/Example/Platformer/PlatformerState.cs
T

297 lines
9.5 KiB
C#

using Code.Example.Platformer.Entity;
using Code.Example.Platformer.Level;
using Code.Infrastructure.App;
using Code.Infrastructure.Audio;
using Code.Infrastructure.Camera;
using Code.Infrastructure.EventBus;
using Code.Infrastructure.Generators;
using Code.Infrastructure.Input;
using Code.Infrastructure.Loadings;
using Code.Infrastructure.SceneLoader;
using Code.Infrastructure.UI;
using Code.Infrastructure.UI.Panels;
using Reflex.Core;
using Unity.Cinemachine;
using UnityEngine.SceneManagement;
namespace Code.Example.Platformer
{
public interface IPlayerReachedFinishHandle : IGlobalSubscriber
{
void PlayerReachedFinish();
}
[State(typeof(AppStates),typeof(AppStateMachineContext))]
[StateRaisingEvent]
public partial class PlatformerState :
IOnPlatformerSceneLoadedHandler,
IOnPlatformer2SceneLoadedHandler,
IOnPlatformer3SceneLoadedHandler,
ICinematicMachineHandle,
IPlatformerEnemyLoadedHandle,
IPlatformerPlayerLoadedHandle,
IPlatformerPlayerSpawnedHandle,
IReadyToPlayHandle,
IPlayerReachedFinishHandle,
ILoadingPanelOpenedHandle,
ILoadingPanelClosedHandle,
IPlatformerEnemySpawnedHandle,
IPlayerDiedHandle
{
private IPlatformerEnemyFactory _enemyFactory;
private IPlatformerPlayerFactory _playerFactory;
private PlatformerFirstLevelData _currentLevelData;
private Entity.Entity _playerEntity;
private IAudioFactory _audioFactory;
private Container _fixdamncontainer;
private Loading _loading;
public PlatformerState(AppStateMachineContext context, EventBus eventBus) : base(context, eventBus)
{
}
public override AppStates myState => AppStates.Platformer;
protected override void OnEnterState()
{
EventBus.Subscribe(this);
// LoadingScreen
_loading = new(1, StartInitialLoading);
Context.UIService.OpenPanel(UIPanelType.Loading);
}
protected override void OnExitState()
{
_enemyFactory.ReleaseEnemy(PlatformerEntityType.Bee);
_enemyFactory.ReleaseEnemy(PlatformerEntityType.Snail);
_enemyFactory.ReleaseEnemy(PlatformerEntityType.Boar);
_playerFactory.ReleasePlayer();
Context.CameraFactory.ReleaseCinemachineCamera(CameraMode.Platformer);
Context.InputService.DisableMap(ActionMappings.Platformer);
Context.AudioService.ChangeGroupVolume(
AudioGroupType.Music,
0f,
0.5f,
ReleasePlatformerMusic);
EventBus.Unsubscribe(this);
}
private void ReleasePlatformerMusic()
{
Context.AudioService.CancelMusic(AudioNames.Platformer, 0);
Context.AudioService.CancelMusic(AudioNames.Platformer, 7);
_audioFactory.ReleaseAudios(AudioNames.Platformer);
}
private void StartInitialLoading()
{
// Dependency scene
// Level scene
// Camera
// Enemy snail
// Enemy bee
// Enemy boar
// Player
// Music
_loading = new(8, InitialLoadingCompleted);
Context.AudioService.ChangeGroupVolume(
AudioGroupType.Music,
1f,
0.5f,
() => Context.AudioService.PlayRepeatedly(AudioNames.Platformer,0, AudioGroupType.Music));
Context.SceneLoader.LoadSceneAsync(SceneId.Platformer, LoadSceneMode.Single).Forget();
Context.InputService.EnableMap(ActionMappings.Platformer);
Context.CameraFactory.LoadCinemachineCamera(CameraMode.Platformer).Forget();
}
private void InitialLoadingCompleted()
{
_loading = new Loading(2, CloseLevel1LoadingScreen);
_enemyFactory.SpawnEnemy(PlatformerEntityType.Snail, _currentLevelData.enemySpawnPoints[0].position);
_playerFactory.SpawnPlayer(_currentLevelData.playerCheckPoints[0].position);
}
private void CloseLevel1LoadingScreen()
{
Context.UIService.ClosePanel(UIPanelType.Loading);
EventBus.RaiseEvent<IGameplayFreezeable>(x=> x.UnFreeze());
}
public void CinemachineLoaded(CinemachineCamera cinemachineCamera)
{
_loading.DoProgress();
}
public void PlatformerEnemyLoaded(PlatformerEntityType type, Entity.Entity entity)
{
if(entity) _loading.DoProgress();
}
public void PlatformerPlayerLoaded()
{
_loading.DoProgress();
}
public void PlatformerPlayerSpawned(Entity.Entity entity)
{
_playerEntity = entity;
Context.CameraFactory.GetCinematicCamera(CameraMode.Platformer).Follow = entity.transform;
_loading.DoProgress();
}
public void PlatformerEnemySpawned(Entity.Entity entity)
{
_loading.DoProgress();
}
public void OnReadyToPlay(AudioNames groupName)
{
if(groupName == AudioNames.Platformer) _loading.DoProgress();
}
public void PlayerReachedFinish()
{
_loading = new Loading(1, StartLevel2Loading);
Context.AudioService.ChangeGroupVolume(
AudioGroupType.Music,
0f,
0.5f,
() => Context.AudioService.CancelMusic(AudioNames.Platformer, 0));
Context.UIService.OpenPanel(UIPanelType.Loading);
EventBus.RaiseEvent<IGameplayFreezeable>(x=> x.Freeze());
SceneManager.MoveGameObjectToScene(_playerEntity.gameObject, Context.SceneLoader.GetScene(SceneId.Platformer));
}
private void StartLevel2Loading()
{
_loading = new(2, StartLevel2Gameplay);
Context.SceneLoader.UnloadSceneAsync(SceneId.Platformer2).Forget();
Context.SceneLoader.LoadSceneAsync(SceneId.Platformer3, LoadSceneMode.Additive).Forget();
}
private void StartLevel2Gameplay()
{
Context.AudioService.ChangeGroupVolume(
AudioGroupType.Music,
1f,
0.5f,
() => Context.AudioService.PlayRepeatedly(AudioNames.Platformer,7, AudioGroupType.Music));
Context.UIService.ClosePanel(UIPanelType.Loading);
EventBus.RaiseEvent<IGameplayFreezeable>(x=> x.UnFreeze());
}
public void OnPlatformerSceneLoaded()
{
Context.SceneLoader.LoadSceneAsync(SceneId.Platformer2, LoadSceneMode.Additive).Forget();
}
public void OnPlatformerSceneInitialized(Container container)
{
_fixdamncontainer = container;
_audioFactory = container.Resolve<IAudioFactory>();
_audioFactory.LoadAudioClips(AudioNames.Platformer);
_enemyFactory = container.Resolve<IPlatformerEnemyFactory>();
_enemyFactory.WarmEnemy(PlatformerEntityType.Bee);
_enemyFactory.WarmEnemy(PlatformerEntityType.Boar);
_enemyFactory.WarmEnemy(PlatformerEntityType.Snail);
_playerFactory = container.Resolve<IPlatformerPlayerFactory>();
_playerFactory.WarmPlayer();
_loading.DoProgress();
}
public void OnPlatformer2SceneLoaded()
{
SceneManager.SetActiveScene(Context.SceneLoader.GetScene(SceneId.Platformer2));
}
public void OnPlatformer2SceneInitialized(Container container)
{
_currentLevelData = container.Resolve<PlatformerFirstLevelData>();
_loading.DoProgress();
}
public void OnPlatformer3SceneLoaded()
{
SceneManager.SetActiveScene(Context.SceneLoader.GetScene(SceneId.Platformer3));
_enemyFactory.SpawnEnemy(PlatformerEntityType.Boar, _currentLevelData.enemySpawnPoints[0].position);
_playerEntity.transform.position = _currentLevelData.playerCheckPoints[0].position;
_loading.DoProgress();
}
public void OnPlatformer3SceneInitialized(Container container)
{
_currentLevelData = container.Resolve<PlatformerFirstLevelData>();
_loading.DoProgress();
}
public void LoadingPanelOpened(UIPanel panel)
{
_loading.DoProgress();
}
public void LoadingPanelClosed()
{
}
public void PlayerDied()
{
_loading = new(2, EndGameLoading);
Context.AudioService.ChangeGroupVolume(
AudioGroupType.Music,
0f,
0.5f,
OffTheMusic);
Context.UIService.OpenPanel(UIPanelType.Loading);
}
private void OffTheMusic()
{
ReleasePlatformerMusic();
_loading.DoProgress();
}
private void EndGameLoading()
{
Context.StateMachine.SwitchState(AppStates.MainMenu)
.AllowStateTransition(AppStates.Platformer);
}
}
}