Фабрика противников
This commit is contained in:
1 parent
93819c1dd3
commit
653342d080
35 files changed
+527
-406
No files matched your search
@@ -3,7 +3,6 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Code.Infrastructure.Configs;
|
||||
using Code.Infrastructure.EventBus;
|
||||
using Code.Infrastructure.Generators;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
using UnityEngine.AddressableAssets;
|
||||
@@ -11,40 +10,24 @@ using UnityEngine.ResourceManagement.AsyncOperations;
|
||||
|
||||
namespace Code.Example.Platformer.Entity
|
||||
{
|
||||
[Config("Configs/Gameplay/Platformer/EntitySettings", true)]
|
||||
public class PlatformerEntitySettings
|
||||
public interface IPlatformerEnemyFactory
|
||||
{
|
||||
[ToConfig] public PlatformerEntityData EntityData;
|
||||
UniTaskVoid LoadEnemy(PlatformerEntityType enemyType);
|
||||
void ReleaseEnemy(PlatformerEntityType racerType);
|
||||
|
||||
[ToConfig] public AssetReference prefabReference;
|
||||
UniTaskVoid SpawnEnemy(
|
||||
PlatformerEntityType enemyType,
|
||||
Vector3 position);
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public struct PlatformerEntityData
|
||||
{
|
||||
public string name;
|
||||
public float speed;
|
||||
public float acceleration;
|
||||
public int damage;
|
||||
public PlatformerEntityType type;
|
||||
public int maxHealth;
|
||||
}
|
||||
|
||||
public enum PlatformerEntityType
|
||||
{
|
||||
Player,
|
||||
Boar,
|
||||
Bee,
|
||||
Snail
|
||||
}
|
||||
public class PlatformerEnemyFactory
|
||||
public class PlatformerEnemyFactory : IPlatformerEnemyFactory, IDisposable
|
||||
{
|
||||
private EventBus _eventBus;
|
||||
|
||||
|
||||
private string _enemyConfigsLabel;
|
||||
private AsyncOperationHandle<IList<PlatformerEntitySettingsConfig>> _enemyConfigsHandle;
|
||||
private Dictionary<PlatformerEntityType, AsyncOperationHandle<GameObject>> _enemyPrefabsHandle = new();
|
||||
|
||||
|
||||
private Dictionary<PlatformerEntityType, PlatformerEntitySettingsConfig> _enemyConfigs;
|
||||
|
||||
public PlatformerEnemyFactory(IConfigLocator configLocator, EventBus eventBus)
|
||||
@@ -52,62 +35,75 @@ namespace Code.Example.Platformer.Entity
|
||||
_eventBus = eventBus;
|
||||
_enemyConfigsLabel = configLocator.TryGetManyConfigAssetReferences<PlatformerEntitySettingsConfig>();
|
||||
|
||||
LoadConfigs().Forget();
|
||||
LoadConfigs().Forget();
|
||||
}
|
||||
|
||||
|
||||
private async UniTaskVoid LoadConfigs()
|
||||
{
|
||||
_enemyConfigsHandle = Addressables.LoadAssetsAsync<PlatformerEntitySettingsConfig>(_enemyConfigsLabel);
|
||||
|
||||
await _enemyConfigsHandle.ToUniTask();
|
||||
|
||||
_enemyConfigs = _enemyConfigsHandle.Result.ToDictionary(x => x.racerData.type);
|
||||
_enemyConfigs = _enemyConfigsHandle.Result.ToDictionary(x => x.EntityData.type);
|
||||
_enemyConfigs.Remove(PlatformerEntityType.Player);
|
||||
|
||||
_eventBus.RaiseEvent<IPlatformerFactoryInitializedHandle>(x => x.PlatformerFactoryInitialized());
|
||||
|
||||
_eventBus.RaiseEvent<IPlatformerEnemyFactoryInitializedHandle>(x => x.PlatformerEnemyFactoryInitialized());
|
||||
}
|
||||
|
||||
public async UniTaskVoid LoadRacer(PlatformerEntityType racerType)
|
||||
public async UniTaskVoid LoadEnemy(PlatformerEntityType enemyType)
|
||||
{
|
||||
if (racerType == PlatformerEntityType.Player) return;
|
||||
|
||||
if (_enemyPrefabsHandle.ContainsKey(racerType))
|
||||
if (enemyType == PlatformerEntityType.Player) return;
|
||||
|
||||
if (_enemyPrefabsHandle.ContainsKey(enemyType))
|
||||
{
|
||||
_eventBus.RaiseEvent<IPlatformerEnemyLoadedHandle>(x => x.PlatformerEnemyLoaded(racerType, null));
|
||||
_eventBus.RaiseEvent<IPlatformerEnemyLoadedHandle>(x => x.PlatformerEnemyLoaded(enemyType, null));
|
||||
|
||||
return;
|
||||
}
|
||||
UnityEngine.Debug.LogWarning($"Racer loaded {racerType}");
|
||||
_enemyPrefabsHandle.Add(racerType,
|
||||
Addressables.LoadAssetAsync<GameObject>(_enemyConfigs[racerType].prefabReference));
|
||||
|
||||
await _enemyPrefabsHandle[racerType].ToUniTask();
|
||||
var racer = _enemyPrefabsHandle[racerType].Result.GetComponent<Entity>();
|
||||
racer.SetData(_enemyConfigs[racerType].racerData);
|
||||
|
||||
_eventBus.RaiseEvent<IPlatformerEnemyLoadedHandle>(x => x.PlatformerEnemyLoaded(racerType, racer));
|
||||
}
|
||||
|
||||
UnityEngine.Debug.LogWarning($"Enemy loaded {enemyType}");
|
||||
_enemyPrefabsHandle.Add(enemyType,
|
||||
Addressables.LoadAssetAsync<GameObject>(_enemyConfigs[enemyType].prefabReference));
|
||||
|
||||
await _enemyPrefabsHandle[enemyType].ToUniTask();
|
||||
var racer = _enemyPrefabsHandle[enemyType].Result.GetComponent<Entity>();
|
||||
racer.SetData(_enemyConfigs[enemyType].EntityData);
|
||||
|
||||
_eventBus.RaiseEvent<IPlatformerEnemyLoadedHandle>(x => x.PlatformerEnemyLoaded(enemyType, racer));
|
||||
}
|
||||
|
||||
public void ReleaseRacer(PlatformerEntityType racerType)
|
||||
public void ReleaseEnemy(PlatformerEntityType racerType)
|
||||
{
|
||||
_enemyPrefabsHandle[racerType].Release();
|
||||
_enemyPrefabsHandle.Remove(racerType);
|
||||
}
|
||||
|
||||
public async UniTaskVoid SpawnEnemy(
|
||||
PlatformerEntityType enemyType,
|
||||
Vector3 position)
|
||||
{
|
||||
if (enemyType == PlatformerEntityType.Player) return;
|
||||
|
||||
await Addressables.InstantiateAsync(_enemyConfigs[enemyType].prefabReference, position, Quaternion.identity);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
UnityEngine.Debug.LogWarning($"Racers released");
|
||||
foreach (var prefab in _enemyPrefabsHandle)
|
||||
UnityEngine.Debug.LogWarning($"Enemies released");
|
||||
foreach (var prefab in _enemyPrefabsHandle)
|
||||
prefab.Value.Release();
|
||||
|
||||
_enemyPrefabsHandle.Clear();
|
||||
|
||||
|
||||
_enemyConfigsHandle.Release();
|
||||
}
|
||||
}
|
||||
public interface IPlatformerFactoryInitializedHandle : IGlobalSubscriber
|
||||
|
||||
public interface IPlatformerEnemyFactoryInitializedHandle : IGlobalSubscriber
|
||||
{
|
||||
public void PlatformerFactoryInitialized();
|
||||
public void PlatformerEnemyFactoryInitialized();
|
||||
}
|
||||
|
||||
public interface IPlatformerEnemyLoadedHandle : IGlobalSubscriber
|
||||
{
|
||||
public void PlatformerEnemyLoaded(PlatformerEntityType type, Entity entity);
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
using System;
|
||||
using Code.Infrastructure.Generators;
|
||||
using UnityEngine.AddressableAssets;
|
||||
|
||||
namespace Code.Example.Platformer.Entity
|
||||
{
|
||||
|
||||
[Serializable]
|
||||
public struct PlatformerEntityData
|
||||
{
|
||||
public string name;
|
||||
public float speed;
|
||||
public float acceleration;
|
||||
public int damage;
|
||||
public PlatformerEntityType type;
|
||||
public int maxHealth;
|
||||
}
|
||||
|
||||
public enum PlatformerEntityType
|
||||
{
|
||||
Player,
|
||||
Boar,
|
||||
Bee,
|
||||
Snail
|
||||
}
|
||||
[Config("Configs/Gameplay/Platformer/EntitySettings", true)]
|
||||
public class PlatformerEntitySettings
|
||||
{
|
||||
[ToConfig] public PlatformerEntityData EntityData;
|
||||
|
||||
[ToConfig] public AssetReference prefabReference;
|
||||
}
|
||||
|
||||
[Config("Configs/Gameplay/Platformer/PlayerSettings")]
|
||||
public class PlatformerPlayerSettings
|
||||
{
|
||||
[ToConfig] public PlatformerEntityData EntityData;
|
||||
|
||||
[ToConfig] public AssetReference prefabReference;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6116fb7cbe9c49a9863b83f85375096a
|
||||
timeCreated: 1773066580
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 50745373fe25451191a647170efe16d9
|
||||
timeCreated: 1773067033
|
||||
@@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Code.Example.Platformer.Level
|
||||
{
|
||||
[Serializable]
|
||||
public struct PlatformerFirstLevelData
|
||||
{
|
||||
[SerializeField] public List<Transform> enemySpawnPoints;
|
||||
[SerializeField] public List<Transform> playerCheckPoints;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 23a7f57640a141dc866ceb89058c2f71
|
||||
timeCreated: 1773067046
|
||||
@@ -1,28 +1,55 @@
|
||||
using System;
|
||||
using Code.Example.Platformer.Entity;
|
||||
using Code.Example.Platformer.Level;
|
||||
using Code.Infrastructure.EventBus;
|
||||
using Code.Infrastructure.Loadings;
|
||||
using Code.Infrastructure.SceneLoader;
|
||||
using Reflex.Core;
|
||||
using Reflex.Enums;
|
||||
using UnityEngine;
|
||||
using Resolution = Reflex.Enums.Resolution;
|
||||
|
||||
namespace Code.Example.Platformer
|
||||
{
|
||||
public class PlatformerSceneInstaller : MonoBehaviour, IInstaller
|
||||
public class PlatformerEnemySceneInstaller : MonoBehaviour, IInstaller, IPlatformerEnemyFactoryInitializedHandle
|
||||
{
|
||||
// [SerializeField] private PlatformerCanvas gameplayPlatformerCanvas;
|
||||
[SerializeField] private PlatformerFirstLevelData firstLevelData;
|
||||
|
||||
public void InstallBindings(ContainerBuilder containerBuilder)
|
||||
private Loading _sceneInit;
|
||||
|
||||
private void LoadingComplete()
|
||||
{
|
||||
// containerBuilder.RegisterValue(gameplayPlatformerCanvas);
|
||||
|
||||
containerBuilder.OnContainerBuilt += OnContainerBuild;
|
||||
_container.Resolve<EventBus>()
|
||||
.RaiseEvent<IOnPlatformerSceneLoadedHandler>(x => x.OnPlatformerSceneInitialized(_container));
|
||||
}
|
||||
|
||||
public void InstallBindings(ContainerBuilder containerBuilder)
|
||||
{
|
||||
// Enemies
|
||||
// Container
|
||||
_sceneInit = new(2, LoadingComplete);
|
||||
|
||||
// containerBuilder.RegisterValue(gameplayPlatformerCanvas);
|
||||
containerBuilder.Parent.Resolve<EventBus>().Subscribe(this);
|
||||
|
||||
containerBuilder.RegisterValue(firstLevelData);
|
||||
containerBuilder.RegisterType(typeof(PlatformerEnemyFactory), new []{typeof(IPlatformerEnemyFactory)}, Lifetime.Scoped, Resolution.Eager);
|
||||
containerBuilder.OnContainerBuilt += OnContainerBuild;
|
||||
}
|
||||
private Container _container;
|
||||
private void OnContainerBuild(Container container)
|
||||
{
|
||||
// gameplayPlatformerCanvas.GameplayPlatformerCanvas.worldCamera =
|
||||
// container.Resolve<CameraStateMachineContext>().cameraLoadedData.Camera;
|
||||
//
|
||||
container.Resolve<EventBus>()
|
||||
.RaiseEvent<IOnPlatformerSceneLoadedHandler>(x => x.OnPlatformerSceneInitialized(container));
|
||||
|
||||
_container = container;
|
||||
_sceneInit.DoProgress();
|
||||
}
|
||||
|
||||
public void PlatformerEnemyFactoryInitialized()
|
||||
{
|
||||
_sceneInit.DoProgress();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
using Code.Example.Platformer.Entity;
|
||||
using Code.Example.Platformer.Level;
|
||||
using Code.Infrastructure.App;
|
||||
using Code.Infrastructure.Audio;
|
||||
using Code.Infrastructure.Camera;
|
||||
@@ -15,7 +17,7 @@ namespace Code.Example.Platformer
|
||||
{
|
||||
[State(typeof(AppStates),typeof(AppStateMachineContext))]
|
||||
[StateRaisingEvent]
|
||||
public partial class PlatformerState : IOnPlatformerSceneLoadedHandler, ICinematicMachineHandle
|
||||
public partial class PlatformerState : IOnPlatformerSceneLoadedHandler, ICinematicMachineHandle, IPlatformerEnemyLoadedHandle
|
||||
{
|
||||
public PlatformerState(AppStateMachineContext context, EventBus eventBus) : base(context, eventBus)
|
||||
{
|
||||
@@ -26,7 +28,12 @@ namespace Code.Example.Platformer
|
||||
{
|
||||
EventBus.Subscribe(this);
|
||||
|
||||
_loading = new(2, LoadingCompleted);
|
||||
// Scene
|
||||
// Camera
|
||||
// Enemy snail
|
||||
// Enemy bee
|
||||
// Enemy boar
|
||||
_loading = new(5, LoadingCompleted);
|
||||
|
||||
Context.UIService.OpenPanel(UIPanelType.Loading);
|
||||
Context.SceneLoader.LoadSceneAsync(SceneId.Platformer, LoadSceneMode.Single).Forget();
|
||||
@@ -39,14 +46,20 @@ namespace Code.Example.Platformer
|
||||
{
|
||||
Context.AudioService.CancelMusic(AudioNames.UISounds,0);
|
||||
Context.UIService.ClosePanel(UIPanelType.Loading);
|
||||
|
||||
_enemyFactory.SpawnEnemy(PlatformerEntityType.Snail, _firstLevelData.enemySpawnPoints[0].position);
|
||||
}
|
||||
|
||||
|
||||
protected override void OnExitState()
|
||||
{
|
||||
{
|
||||
_enemyFactory.ReleaseEnemy(PlatformerEntityType.Bee);
|
||||
_enemyFactory.ReleaseEnemy(PlatformerEntityType.Snail);
|
||||
_enemyFactory.ReleaseEnemy(PlatformerEntityType.Boar);
|
||||
|
||||
Context.CameraFactory.ReleaseCinemachineCamera(CameraMode.Platformer);
|
||||
Context.InputService.DisableMap(ActionMappings.Platformer);
|
||||
EventBus.Unsubscribe(this);
|
||||
Context.InputService.DisableMap(ActionMappings.Platformer);
|
||||
}
|
||||
|
||||
public void OnPlatformerSceneLoaded()
|
||||
@@ -54,14 +67,30 @@ namespace Code.Example.Platformer
|
||||
|
||||
}
|
||||
|
||||
private IPlatformerEnemyFactory _enemyFactory;
|
||||
private PlatformerFirstLevelData _firstLevelData;
|
||||
public void OnPlatformerSceneInitialized(Container container)
|
||||
{
|
||||
_firstLevelData = container.Resolve<PlatformerFirstLevelData>();
|
||||
_enemyFactory = container.Resolve<IPlatformerEnemyFactory>();
|
||||
|
||||
_enemyFactory.LoadEnemy(PlatformerEntityType.Bee);
|
||||
_enemyFactory.LoadEnemy(PlatformerEntityType.Boar);
|
||||
_enemyFactory.LoadEnemy(PlatformerEntityType.Snail);
|
||||
|
||||
_loading.DoProgress();
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void CinemachineLoaded(CinemachineCamera cinemachineCamera)
|
||||
{
|
||||
_loading.DoProgress();
|
||||
}
|
||||
|
||||
public void PlatformerEnemyLoaded(PlatformerEntityType type, Entity.Entity entity)
|
||||
{
|
||||
if(entity) _loading.DoProgress();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user