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

125 lines
4.1 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 Reflex.Core;
using Unity.Cinemachine;
using UnityEngine.SceneManagement;
namespace Code.Example.Platformer
{
[State(typeof(AppStates),typeof(AppStateMachineContext))]
[StateRaisingEvent]
public partial class PlatformerState :
IOnPlatformerSceneLoadedHandler,
ICinematicMachineHandle,
IPlatformerEnemyLoadedHandle,
IPlatformerPlayerLoadedHandle,
IPlatformerPlayerSpawnedHandle
{
public PlatformerState(AppStateMachineContext context, EventBus eventBus) : base(context, eventBus)
{
}
public override AppStates myState => AppStates.Platformer;
protected override void OnEnterState()
{
EventBus.Subscribe(this);
// Scene
// Camera
// Enemy snail
// Enemy bee
// Enemy boar
// Player
_loading = new(6, LoadingCompleted);
Context.UIService.OpenPanel(UIPanelType.Loading);
Context.SceneLoader.LoadSceneAsync(SceneId.Platformer, LoadSceneMode.Single).Forget();
Context.InputService.EnableMap(ActionMappings.Platformer);
Context.CameraFactory.LoadCinemachineCamera(CameraMode.Platformer).Forget();
}
private Loading _loading;
private void LoadingCompleted()
{
Context.AudioService.CancelMusic(AudioNames.UISounds,0);
Context.UIService.ClosePanel(UIPanelType.Loading);
_enemyFactory.SpawnEnemy(PlatformerEntityType.Snail, _firstLevelData.enemySpawnPoints[0].position);
_playerFactory.SpawnPlayer(_firstLevelData.playerCheckPoints[0].position);
}
protected override void OnExitState()
{
_enemyFactory.ReleaseEnemy(PlatformerEntityType.Bee);
_enemyFactory.ReleaseEnemy(PlatformerEntityType.Snail);
_enemyFactory.ReleaseEnemy(PlatformerEntityType.Boar);
_playerFactory.ReleasePlayer();
Context.CameraFactory.ReleaseCinemachineCamera(CameraMode.Platformer);
EventBus.Unsubscribe(this);
Context.InputService.DisableMap(ActionMappings.Platformer);
}
public void OnPlatformerSceneLoaded()
{
}
private IPlatformerEnemyFactory _enemyFactory;
private IPlatformerPlayerFactory _playerFactory;
private PlatformerFirstLevelData _firstLevelData;
private Container _fixdamncontainer;
public void OnPlatformerSceneInitialized(Container container)
{
_fixdamncontainer = container;
_firstLevelData = container.Resolve<PlatformerFirstLevelData>();
_enemyFactory = container.Resolve<IPlatformerEnemyFactory>();
_enemyFactory.WarmEnemy(PlatformerEntityType.Bee);
_enemyFactory.WarmEnemy(PlatformerEntityType.Boar);
_enemyFactory.WarmEnemy(PlatformerEntityType.Snail);
_playerFactory = container.Resolve<IPlatformerPlayerFactory>();
_playerFactory.LoadPlayer();
_loading.DoProgress();
}
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)
{
Context.CameraFactory.GetCinematicCamera(CameraMode.Platformer).Follow = entity.transform;
}
}
}