Фабрика игрока
This commit is contained in:
1 parent
653342d080
commit
a7872a0f8c
45 files changed
+395
-168
No files matched your search
@@ -31,11 +31,7 @@ namespace Code.Example.Platformer.Entity
|
||||
[ToConfig] public AssetReference prefabReference;
|
||||
}
|
||||
|
||||
[Config("Configs/Gameplay/Platformer/PlayerSettings")]
|
||||
[Config("", false, true)]
|
||||
public class PlatformerPlayerSettings
|
||||
{
|
||||
[ToConfig] public PlatformerEntityData EntityData;
|
||||
|
||||
[ToConfig] public AssetReference prefabReference;
|
||||
}
|
||||
{ }
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Code.Infrastructure.Configs;
|
||||
using Code.Infrastructure.EventBus;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using Reflex.Core;
|
||||
using Reflex.Injectors;
|
||||
using UnityEngine;
|
||||
using UnityEngine.AddressableAssets;
|
||||
using UnityEngine.ResourceManagement.AsyncOperations;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
namespace Code.Example.Platformer.Entity
|
||||
{
|
||||
public interface IPlatformerPlayerFactory
|
||||
{
|
||||
UniTaskVoid LoadPlayer();
|
||||
void ReleasePlayer();
|
||||
|
||||
UniTaskVoid SpawnPlayer(Vector3 position);
|
||||
}
|
||||
|
||||
public class PlatformerPlayerFactory : IPlatformerPlayerFactory, IDisposable
|
||||
{
|
||||
private EventBus _eventBus;
|
||||
|
||||
private AssetReference _playerConfigReference;
|
||||
private AsyncOperationHandle<PlatformerEntitySettingsConfig> _playerConfigHandle;
|
||||
private AsyncOperationHandle<GameObject> _playerPrefabHandle;
|
||||
|
||||
private PlatformerEntitySettingsConfig _playerConfig;
|
||||
|
||||
public PlatformerPlayerFactory(IConfigLocator configLocator, EventBus eventBus)
|
||||
{
|
||||
_eventBus = eventBus;
|
||||
_playerConfigReference = configLocator.TryGetConfigAssetReference<PlatformerPlayerSettingsConfig>();
|
||||
|
||||
LoadConfigs().Forget();
|
||||
}
|
||||
|
||||
private async UniTaskVoid LoadConfigs()
|
||||
{
|
||||
_playerConfigHandle = Addressables.LoadAssetAsync<PlatformerEntitySettingsConfig>(_playerConfigReference);
|
||||
|
||||
await _playerConfigHandle.ToUniTask();
|
||||
|
||||
_playerConfig = _playerConfigHandle.Result;
|
||||
|
||||
_eventBus.RaiseEvent<IPlatformerPlayerFactoryInitializedHandle>(x => x.PlatformerPlayerFactoryInitialized());
|
||||
}
|
||||
|
||||
public async UniTaskVoid LoadPlayer()
|
||||
{
|
||||
if (_playerPrefabHandle.IsValid())
|
||||
{
|
||||
_eventBus.RaiseEvent<IPlatformerPlayerLoadedHandle>(x => x.PlatformerPlayerLoaded());
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
UnityEngine.Debug.LogWarning($"Player loaded");
|
||||
_playerPrefabHandle = Addressables.LoadAssetAsync<GameObject>(_playerConfig.prefabReference);
|
||||
|
||||
await _playerPrefabHandle.ToUniTask();
|
||||
var racer = _playerPrefabHandle.Result.GetComponent<Entity>();
|
||||
racer.SetData(_playerConfig.EntityData);
|
||||
|
||||
_eventBus.RaiseEvent<IPlatformerPlayerLoadedHandle>(x => x.PlatformerPlayerLoaded());
|
||||
}
|
||||
|
||||
public void ReleasePlayer()
|
||||
{
|
||||
_playerPrefabHandle.Release();
|
||||
}
|
||||
|
||||
public async UniTaskVoid SpawnPlayer(Vector3 position)
|
||||
{
|
||||
var player = await Addressables.InstantiateAsync(_playerConfig.prefabReference, position, Quaternion.identity).ToUniTask();
|
||||
GameObjectInjector.InjectObject(player, Container.RootContainer); // TODO САМОЕ НЕСТАБИЛЬНОЕ ДЕРЬМО ВО ВСЕЛЕННОЙ КОНТЕЙНЕР ДОЛЖЕН БЫТЬ КОНТЕКСТНЫМ
|
||||
_eventBus.RaiseEvent<IPlatformerPlayerSpawnedHandle>(x => x.PlatformerPlayerSpawned(player.GetComponent<Entity>()));
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
UnityEngine.Debug.LogWarning($"Enemies released");
|
||||
_playerPrefabHandle.Release();
|
||||
|
||||
_playerConfigHandle.Release();
|
||||
}
|
||||
}
|
||||
|
||||
public interface IPlatformerPlayerFactoryInitializedHandle : IGlobalSubscriber
|
||||
{
|
||||
public void PlatformerPlayerFactoryInitialized();
|
||||
}
|
||||
|
||||
public interface IPlatformerPlayerLoadedHandle : IGlobalSubscriber
|
||||
{
|
||||
public void PlatformerPlayerLoaded();
|
||||
}
|
||||
public interface IPlatformerPlayerSpawnedHandle : IGlobalSubscriber
|
||||
{
|
||||
public void PlatformerPlayerSpawned(Entity entity);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 70a3deb8fbb44749b5aeea7b3997a459
|
||||
timeCreated: 1773136472
|
||||
@@ -62,7 +62,7 @@ namespace Code.Example.Platformer.Entity
|
||||
private void JumpPerformed(InputAction.CallbackContext callbackContext)
|
||||
{
|
||||
if (!onGround) return;
|
||||
rigidbody2D.AddForceY(150f);
|
||||
rigidbody2D.AddForceY(100f);
|
||||
|
||||
_acceleration = 1f;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user