mirror of
https://github.com/Bragin-Stepan/project-entity.git
synced 2026-03-05 07:41:10 +00:00
init: add project
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace _Project.Develop.Runtime.Entities
|
||||
{
|
||||
public class CollidersRegistryService
|
||||
{
|
||||
private readonly Dictionary<Collider, Entity> _colliderToEntity = new();
|
||||
|
||||
public void Register(Collider collider, Entity entity)
|
||||
{
|
||||
_colliderToEntity.Add(collider, entity);
|
||||
}
|
||||
|
||||
public void Unregister(Collider collider)
|
||||
{
|
||||
_colliderToEntity.Remove(collider);
|
||||
}
|
||||
|
||||
public Entity GetBy(Collider collider)
|
||||
{
|
||||
if (_colliderToEntity.TryGetValue(collider, out Entity entity))
|
||||
return entity;
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e43e968a40dc46699c23f272909cf9cb
|
||||
timeCreated: 1771425427
|
||||
@@ -0,0 +1,50 @@
|
||||
using _Project.Develop.Runtime.Logic.Gameplay.Features.Movement;
|
||||
using _Project.Develop.Runtime.Utilities.InputManagement;
|
||||
using _Project.Develop.Runtime.Utils.ReactiveManagement;
|
||||
using Assets._Project.Develop.Runtime.Infrastructure.DI;
|
||||
using Assets._Project.Develop.Runtime.Utilities.SceneManagement;
|
||||
using UnityEngine;
|
||||
|
||||
namespace _Project.Develop.Runtime.Entities
|
||||
{
|
||||
public class EntitiesFactory
|
||||
{
|
||||
private readonly EntitiesLifeContext _entitiesLifeContext;
|
||||
private readonly MonoEntitiesFactory _monoEntitiesFactory;
|
||||
private readonly IPlayerInputService _playerInput;
|
||||
|
||||
public EntitiesFactory(DIContainer container)
|
||||
{
|
||||
_entitiesLifeContext = container.Resolve<EntitiesLifeContext>();
|
||||
_monoEntitiesFactory = container.Resolve<MonoEntitiesFactory>();
|
||||
_playerInput = container.Resolve<IPlayerInputService>();
|
||||
}
|
||||
|
||||
public Entity CreateTestEntity(Vector3 position)
|
||||
{
|
||||
Entity entity = CreateEmpty();
|
||||
|
||||
_monoEntitiesFactory.Create(entity, position, PathToResources.Entity.TestEntity);
|
||||
|
||||
entity
|
||||
.AddMoveDirection()
|
||||
.AddRotateDirection()
|
||||
.AddMoveSpeed(new ReactiveVariable<float>(10))
|
||||
.AddRotationSpeed(new ReactiveVariable<float>(800));
|
||||
|
||||
entity
|
||||
.AddSystem(new CharacterControllerMovementSystem())
|
||||
.AddSystem(new TransformRotationSystem())
|
||||
// .AddSystem(new RigidbodyMovementSystem())
|
||||
// .AddSystem(new RigidbodyRotationSystem())
|
||||
.AddSystem(new MoveDirectionByInputSystem(_playerInput))
|
||||
.AddSystem(new RotateDirectionByInputSystem(_playerInput));
|
||||
|
||||
_entitiesLifeContext.Add(entity);
|
||||
|
||||
return entity;
|
||||
}
|
||||
|
||||
private Entity CreateEmpty() => new();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: af347dbf800d4edfadf180cf91c44b1a
|
||||
timeCreated: 1771427064
|
||||
@@ -0,0 +1,54 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace _Project.Develop.Runtime.Entities
|
||||
{
|
||||
public class EntitiesLifeContext : IDisposable
|
||||
{
|
||||
public event Action<Entity> Added;
|
||||
public event Action<Entity> Released;
|
||||
|
||||
private readonly List<Entity> _entities = new();
|
||||
private readonly List<Entity> _releaseRequests = new();
|
||||
|
||||
public IReadOnlyList<Entity> Entities => _entities;
|
||||
|
||||
public void Add(Entity entity)
|
||||
{
|
||||
_entities.Add(entity);
|
||||
|
||||
entity.Initialize();
|
||||
|
||||
Added?.Invoke(entity);
|
||||
}
|
||||
|
||||
public void Update(float deltaTime)
|
||||
{
|
||||
for (int i = 0; i < _entities.Count; i++)
|
||||
_entities[i].OnUpdate(deltaTime);
|
||||
|
||||
foreach (Entity entity in _releaseRequests)
|
||||
{
|
||||
_entities.Remove(entity);
|
||||
entity.Dispose();
|
||||
Released?.Invoke(entity);
|
||||
}
|
||||
|
||||
_releaseRequests.Clear();
|
||||
}
|
||||
|
||||
public void Release(Entity entity)
|
||||
{
|
||||
_releaseRequests.Add(entity);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
foreach (Entity entity in _entities)
|
||||
entity.Dispose();
|
||||
|
||||
_entities.Clear();
|
||||
_releaseRequests.Clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6cee7e44950c49d180ae94ab120fad37
|
||||
timeCreated: 1771425427
|
||||
@@ -0,0 +1,104 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace _Project.Develop.Runtime.Entities
|
||||
{
|
||||
public partial class Entity : IDisposable
|
||||
{
|
||||
public event Action<Entity> Initialized;
|
||||
|
||||
private readonly Dictionary<Type, IEntityComponent> _components = new();
|
||||
|
||||
private readonly List<IEntitySystem> _systems = new();
|
||||
|
||||
private readonly List<IInitializableSystem> _initializables = new();
|
||||
private readonly List<IUpdatableSystem> _updatables = new();
|
||||
private readonly List<IDisposableSystem> _disposables = new();
|
||||
|
||||
private bool _isInit;
|
||||
|
||||
public bool IsInit => _isInit;
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
foreach (IInitializableSystem initializable in _initializables)
|
||||
initializable.OnInit(this);
|
||||
|
||||
_isInit = true;
|
||||
|
||||
Initialized?.Invoke(this);
|
||||
}
|
||||
|
||||
public void OnUpdate(float deltaTime)
|
||||
{
|
||||
if (_isInit == false)
|
||||
return;
|
||||
|
||||
foreach (IUpdatableSystem updatable in _updatables)
|
||||
updatable.OnUpdate(deltaTime);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
foreach (IDisposableSystem disposable in _disposables)
|
||||
disposable.OnDispose();
|
||||
|
||||
_isInit = false;
|
||||
}
|
||||
|
||||
public Entity AddComponent<TComponent>(TComponent component) where TComponent : class, IEntityComponent
|
||||
{
|
||||
_components.Add(typeof(TComponent), component);
|
||||
return this;
|
||||
}
|
||||
|
||||
public bool HasComponent<TComponent>() where TComponent : class, IEntityComponent
|
||||
{
|
||||
return _components.ContainsKey(typeof(TComponent));
|
||||
}
|
||||
|
||||
public bool TryGetComponent<TComponent>(out TComponent component) where TComponent : class, IEntityComponent
|
||||
{
|
||||
if(_components.TryGetValue(typeof(TComponent), out IEntityComponent findedObject))
|
||||
{
|
||||
component = (TComponent)findedObject;
|
||||
return true;
|
||||
}
|
||||
|
||||
component = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
public TComponent GetComponent<TComponent>() where TComponent : class, IEntityComponent
|
||||
{
|
||||
if (TryGetComponent(out TComponent component) == false)
|
||||
throw new ArgumentException($"Entity not exist {typeof(TComponent)}");
|
||||
|
||||
return component;
|
||||
}
|
||||
|
||||
public Entity AddSystem(IEntitySystem system)
|
||||
{
|
||||
if (_systems.Contains(system))
|
||||
throw new ArgumentException(system.GetType().ToString());
|
||||
|
||||
_systems.Add(system);
|
||||
|
||||
if (system is IInitializableSystem initializable)
|
||||
{
|
||||
_initializables.Add(initializable);
|
||||
|
||||
if (_isInit)
|
||||
initializable.OnInit(this);
|
||||
}
|
||||
|
||||
if (system is IUpdatableSystem updatable)
|
||||
_updatables.Add(updatable);
|
||||
|
||||
if (system is IDisposableSystem disposable)
|
||||
_disposables.Add(disposable);
|
||||
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9cae313f27754e89bf5ad980839b9e41
|
||||
timeCreated: 1771425427
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 658341c858324417b69890cb508b580b
|
||||
timeCreated: 1771428293
|
||||
@@ -0,0 +1,202 @@
|
||||
namespace _Project.Develop.Runtime.Entities
|
||||
{
|
||||
public partial class Entity
|
||||
{
|
||||
public Assets._Project.Develop.Runtime.Gameplay.Common.RigidbodyComponent RigidbodyC => GetComponent<Assets._Project.Develop.Runtime.Gameplay.Common.RigidbodyComponent>();
|
||||
|
||||
public UnityEngine.Rigidbody Rigidbody => RigidbodyC.Value;
|
||||
|
||||
public bool TryGetRigidbody(out UnityEngine.Rigidbody value)
|
||||
{
|
||||
bool result = TryGetComponent(out Assets._Project.Develop.Runtime.Gameplay.Common.RigidbodyComponent component);
|
||||
if(result)
|
||||
value = component.Value;
|
||||
else
|
||||
value = default(UnityEngine.Rigidbody);
|
||||
return result;
|
||||
}
|
||||
|
||||
public _Project.Develop.Runtime.Entities.Entity AddRigidbody(UnityEngine.Rigidbody value)
|
||||
{
|
||||
return AddComponent(new Assets._Project.Develop.Runtime.Gameplay.Common.RigidbodyComponent() {Value = value});
|
||||
}
|
||||
|
||||
public Assets._Project.Develop.Runtime.Gameplay.Common.TransformComponent TransformC => GetComponent<Assets._Project.Develop.Runtime.Gameplay.Common.TransformComponent>();
|
||||
|
||||
public UnityEngine.Transform Transform => TransformC.Value;
|
||||
|
||||
public bool TryGetTransform(out UnityEngine.Transform value)
|
||||
{
|
||||
bool result = TryGetComponent(out Assets._Project.Develop.Runtime.Gameplay.Common.TransformComponent component);
|
||||
if(result)
|
||||
value = component.Value;
|
||||
else
|
||||
value = default(UnityEngine.Transform);
|
||||
return result;
|
||||
}
|
||||
|
||||
public _Project.Develop.Runtime.Entities.Entity AddTransform(UnityEngine.Transform value)
|
||||
{
|
||||
return AddComponent(new Assets._Project.Develop.Runtime.Gameplay.Common.TransformComponent() {Value = value});
|
||||
}
|
||||
|
||||
public Assets._Project.Develop.Runtime.Gameplay.Common.CharacterControllerComponent CharacterControllerC => GetComponent<Assets._Project.Develop.Runtime.Gameplay.Common.CharacterControllerComponent>();
|
||||
|
||||
public UnityEngine.CharacterController CharacterController => CharacterControllerC.Value;
|
||||
|
||||
public bool TryGetCharacterController(out UnityEngine.CharacterController value)
|
||||
{
|
||||
bool result = TryGetComponent(out Assets._Project.Develop.Runtime.Gameplay.Common.CharacterControllerComponent component);
|
||||
if(result)
|
||||
value = component.Value;
|
||||
else
|
||||
value = default(UnityEngine.CharacterController);
|
||||
return result;
|
||||
}
|
||||
|
||||
public _Project.Develop.Runtime.Entities.Entity AddCharacterController(UnityEngine.CharacterController value)
|
||||
{
|
||||
return AddComponent(new Assets._Project.Develop.Runtime.Gameplay.Common.CharacterControllerComponent() {Value = value});
|
||||
}
|
||||
|
||||
public Assets._Project.Develop.Runtime.Gameplay.Common.NavMeshAgentComponent NavMeshAgentC => GetComponent<Assets._Project.Develop.Runtime.Gameplay.Common.NavMeshAgentComponent>();
|
||||
|
||||
public UnityEngine.AI.NavMeshAgent NavMeshAgent => NavMeshAgentC.Value;
|
||||
|
||||
public bool TryGetNavMeshAgent(out UnityEngine.AI.NavMeshAgent value)
|
||||
{
|
||||
bool result = TryGetComponent(out Assets._Project.Develop.Runtime.Gameplay.Common.NavMeshAgentComponent component);
|
||||
if(result)
|
||||
value = component.Value;
|
||||
else
|
||||
value = default(UnityEngine.AI.NavMeshAgent);
|
||||
return result;
|
||||
}
|
||||
|
||||
public _Project.Develop.Runtime.Entities.Entity AddNavMeshAgent(UnityEngine.AI.NavMeshAgent value)
|
||||
{
|
||||
return AddComponent(new Assets._Project.Develop.Runtime.Gameplay.Common.NavMeshAgentComponent() {Value = value});
|
||||
}
|
||||
|
||||
public _Project.Develop.Runtime.Logic.Gameplay.Features.Movement.MoveDirection MoveDirectionC => GetComponent<_Project.Develop.Runtime.Logic.Gameplay.Features.Movement.MoveDirection>();
|
||||
|
||||
public _Project.Develop.Runtime.Utils.ReactiveManagement.ReactiveVariable<UnityEngine.Vector3> MoveDirection => MoveDirectionC.Value;
|
||||
|
||||
public bool TryGetMoveDirection(out _Project.Develop.Runtime.Utils.ReactiveManagement.ReactiveVariable<UnityEngine.Vector3> value)
|
||||
{
|
||||
bool result = TryGetComponent(out _Project.Develop.Runtime.Logic.Gameplay.Features.Movement.MoveDirection component);
|
||||
if(result)
|
||||
value = component.Value;
|
||||
else
|
||||
value = default(_Project.Develop.Runtime.Utils.ReactiveManagement.ReactiveVariable<UnityEngine.Vector3>);
|
||||
return result;
|
||||
}
|
||||
|
||||
public _Project.Develop.Runtime.Entities.Entity AddMoveDirection()
|
||||
{
|
||||
return AddComponent(new _Project.Develop.Runtime.Logic.Gameplay.Features.Movement.MoveDirection() { Value = new _Project.Develop.Runtime.Utils.ReactiveManagement.ReactiveVariable<UnityEngine.Vector3>() });
|
||||
}
|
||||
|
||||
public _Project.Develop.Runtime.Entities.Entity AddMoveDirection(_Project.Develop.Runtime.Utils.ReactiveManagement.ReactiveVariable<UnityEngine.Vector3> value)
|
||||
{
|
||||
return AddComponent(new _Project.Develop.Runtime.Logic.Gameplay.Features.Movement.MoveDirection() {Value = value});
|
||||
}
|
||||
|
||||
public _Project.Develop.Runtime.Logic.Gameplay.Features.Movement.MoveSpeed MoveSpeedC => GetComponent<_Project.Develop.Runtime.Logic.Gameplay.Features.Movement.MoveSpeed>();
|
||||
|
||||
public _Project.Develop.Runtime.Utils.ReactiveManagement.ReactiveVariable<System.Single> MoveSpeed => MoveSpeedC.Value;
|
||||
|
||||
public bool TryGetMoveSpeed(out _Project.Develop.Runtime.Utils.ReactiveManagement.ReactiveVariable<System.Single> value)
|
||||
{
|
||||
bool result = TryGetComponent(out _Project.Develop.Runtime.Logic.Gameplay.Features.Movement.MoveSpeed component);
|
||||
if(result)
|
||||
value = component.Value;
|
||||
else
|
||||
value = default(_Project.Develop.Runtime.Utils.ReactiveManagement.ReactiveVariable<System.Single>);
|
||||
return result;
|
||||
}
|
||||
|
||||
public _Project.Develop.Runtime.Entities.Entity AddMoveSpeed()
|
||||
{
|
||||
return AddComponent(new _Project.Develop.Runtime.Logic.Gameplay.Features.Movement.MoveSpeed() { Value = new _Project.Develop.Runtime.Utils.ReactiveManagement.ReactiveVariable<System.Single>() });
|
||||
}
|
||||
|
||||
public _Project.Develop.Runtime.Entities.Entity AddMoveSpeed(_Project.Develop.Runtime.Utils.ReactiveManagement.ReactiveVariable<System.Single> value)
|
||||
{
|
||||
return AddComponent(new _Project.Develop.Runtime.Logic.Gameplay.Features.Movement.MoveSpeed() {Value = value});
|
||||
}
|
||||
|
||||
public _Project.Develop.Runtime.Logic.Gameplay.Features.Movement.RotateDirection RotateDirectionC => GetComponent<_Project.Develop.Runtime.Logic.Gameplay.Features.Movement.RotateDirection>();
|
||||
|
||||
public _Project.Develop.Runtime.Utils.ReactiveManagement.ReactiveVariable<UnityEngine.Vector3> RotateDirection => RotateDirectionC.Value;
|
||||
|
||||
public bool TryGetRotateDirection(out _Project.Develop.Runtime.Utils.ReactiveManagement.ReactiveVariable<UnityEngine.Vector3> value)
|
||||
{
|
||||
bool result = TryGetComponent(out _Project.Develop.Runtime.Logic.Gameplay.Features.Movement.RotateDirection component);
|
||||
if(result)
|
||||
value = component.Value;
|
||||
else
|
||||
value = default(_Project.Develop.Runtime.Utils.ReactiveManagement.ReactiveVariable<UnityEngine.Vector3>);
|
||||
return result;
|
||||
}
|
||||
|
||||
public _Project.Develop.Runtime.Entities.Entity AddRotateDirection()
|
||||
{
|
||||
return AddComponent(new _Project.Develop.Runtime.Logic.Gameplay.Features.Movement.RotateDirection() { Value = new _Project.Develop.Runtime.Utils.ReactiveManagement.ReactiveVariable<UnityEngine.Vector3>() });
|
||||
}
|
||||
|
||||
public _Project.Develop.Runtime.Entities.Entity AddRotateDirection(_Project.Develop.Runtime.Utils.ReactiveManagement.ReactiveVariable<UnityEngine.Vector3> value)
|
||||
{
|
||||
return AddComponent(new _Project.Develop.Runtime.Logic.Gameplay.Features.Movement.RotateDirection() {Value = value});
|
||||
}
|
||||
|
||||
public _Project.Develop.Runtime.Logic.Gameplay.Features.Movement.RotationSpeed RotationSpeedC => GetComponent<_Project.Develop.Runtime.Logic.Gameplay.Features.Movement.RotationSpeed>();
|
||||
|
||||
public _Project.Develop.Runtime.Utils.ReactiveManagement.ReactiveVariable<System.Single> RotationSpeed => RotationSpeedC.Value;
|
||||
|
||||
public bool TryGetRotationSpeed(out _Project.Develop.Runtime.Utils.ReactiveManagement.ReactiveVariable<System.Single> value)
|
||||
{
|
||||
bool result = TryGetComponent(out _Project.Develop.Runtime.Logic.Gameplay.Features.Movement.RotationSpeed component);
|
||||
if(result)
|
||||
value = component.Value;
|
||||
else
|
||||
value = default(_Project.Develop.Runtime.Utils.ReactiveManagement.ReactiveVariable<System.Single>);
|
||||
return result;
|
||||
}
|
||||
|
||||
public _Project.Develop.Runtime.Entities.Entity AddRotationSpeed()
|
||||
{
|
||||
return AddComponent(new _Project.Develop.Runtime.Logic.Gameplay.Features.Movement.RotationSpeed() { Value = new _Project.Develop.Runtime.Utils.ReactiveManagement.ReactiveVariable<System.Single>() });
|
||||
}
|
||||
|
||||
public _Project.Develop.Runtime.Entities.Entity AddRotationSpeed(_Project.Develop.Runtime.Utils.ReactiveManagement.ReactiveVariable<System.Single> value)
|
||||
{
|
||||
return AddComponent(new _Project.Develop.Runtime.Logic.Gameplay.Features.Movement.RotationSpeed() {Value = value});
|
||||
}
|
||||
|
||||
public _Project.Develop.Runtime.Logic.Gameplay.Features.Movement.JumpForce JumpForceC => GetComponent<_Project.Develop.Runtime.Logic.Gameplay.Features.Movement.JumpForce>();
|
||||
|
||||
public _Project.Develop.Runtime.Utils.ReactiveManagement.ReactiveVariable<System.Single> JumpForce => JumpForceC.Value;
|
||||
|
||||
public bool TryGetJumpForce(out _Project.Develop.Runtime.Utils.ReactiveManagement.ReactiveVariable<System.Single> value)
|
||||
{
|
||||
bool result = TryGetComponent(out _Project.Develop.Runtime.Logic.Gameplay.Features.Movement.JumpForce component);
|
||||
if(result)
|
||||
value = component.Value;
|
||||
else
|
||||
value = default(_Project.Develop.Runtime.Utils.ReactiveManagement.ReactiveVariable<System.Single>);
|
||||
return result;
|
||||
}
|
||||
|
||||
public _Project.Develop.Runtime.Entities.Entity AddJumpForce()
|
||||
{
|
||||
return AddComponent(new _Project.Develop.Runtime.Logic.Gameplay.Features.Movement.JumpForce() { Value = new _Project.Develop.Runtime.Utils.ReactiveManagement.ReactiveVariable<System.Single>() });
|
||||
}
|
||||
|
||||
public _Project.Develop.Runtime.Entities.Entity AddJumpForce(_Project.Develop.Runtime.Utils.ReactiveManagement.ReactiveVariable<System.Single> value)
|
||||
{
|
||||
return AddComponent(new _Project.Develop.Runtime.Logic.Gameplay.Features.Movement.JumpForce() {Value = value});
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0e923900a2034874cafef87789bb5665
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace _Project.Develop.Runtime.Entities
|
||||
{
|
||||
public interface IEntityComponent
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d243cc104e0d40f2959cfce43b78afb3
|
||||
timeCreated: 1771425427
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4369817573164b8b8612e797f7d47fb9
|
||||
timeCreated: 1771425427
|
||||
@@ -0,0 +1,19 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace _Project.Develop.Runtime.Entities
|
||||
{
|
||||
public abstract class EntityView : MonoBehaviour
|
||||
{
|
||||
public void Link(Entity entity)
|
||||
{
|
||||
entity.Initialized += OnEntityStartedWork;
|
||||
}
|
||||
|
||||
public virtual void Cleanup(Entity entity)
|
||||
{
|
||||
entity.Initialized -= OnEntityStartedWork;
|
||||
}
|
||||
|
||||
protected abstract void OnEntityStartedWork(Entity entity);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 79a312c3c91a8cd479691cf91a72bcac
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,72 @@
|
||||
using Assets._Project.Develop.Runtime.Infrastructure.DI;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Assets._Project.Develop.Runtime.Utilities.AssetsManagement;
|
||||
using UnityEngine;
|
||||
using Object = UnityEngine.Object;
|
||||
|
||||
namespace _Project.Develop.Runtime.Entities
|
||||
{
|
||||
public class MonoEntitiesFactory : IInitializable, IDisposable
|
||||
{
|
||||
private readonly ResourcesAssetsLoader _resources;
|
||||
|
||||
private readonly EntitiesLifeContext _entitiesLifeContext;
|
||||
|
||||
private readonly CollidersRegistryService _collidersRegistryService;
|
||||
|
||||
private readonly Dictionary<Entity, MonoEntity> _entityToMono = new();
|
||||
|
||||
public MonoEntitiesFactory(
|
||||
ResourcesAssetsLoader resources,
|
||||
EntitiesLifeContext entitiesLifeContext,
|
||||
CollidersRegistryService collidersRegistryService)
|
||||
{
|
||||
_resources = resources;
|
||||
_entitiesLifeContext = entitiesLifeContext;
|
||||
_collidersRegistryService = collidersRegistryService;
|
||||
}
|
||||
|
||||
public MonoEntity Create(Entity entity, Vector3 position, string path)
|
||||
{
|
||||
MonoEntity prefab = _resources.Load<MonoEntity>(path);
|
||||
MonoEntity viewInstance = Object.Instantiate(prefab, position, Quaternion.identity, null);
|
||||
|
||||
viewInstance.Initialize(_collidersRegistryService);
|
||||
viewInstance.Link(entity);
|
||||
|
||||
_entityToMono.Add(entity, viewInstance);
|
||||
|
||||
return viewInstance;
|
||||
}
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
_entitiesLifeContext.Released += OnEntityReleased;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_entitiesLifeContext.Released -= OnEntityReleased;
|
||||
|
||||
foreach (Entity entity in _entityToMono.Keys)
|
||||
CleanupFor(entity);
|
||||
|
||||
_entityToMono.Clear();
|
||||
}
|
||||
|
||||
private void OnEntityReleased(Entity entity)
|
||||
{
|
||||
CleanupFor(entity);
|
||||
|
||||
_entityToMono.Remove(entity);
|
||||
}
|
||||
|
||||
private void CleanupFor(Entity entity)
|
||||
{
|
||||
MonoEntity monoEntity = _entityToMono[entity];
|
||||
monoEntity.Cleanup(entity);
|
||||
Object.Destroy(monoEntity.gameObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e4340f8b4e7105b4ab4cc8aa56dadfd7
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,52 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace _Project.Develop.Runtime.Entities
|
||||
{
|
||||
public class MonoEntity : MonoBehaviour
|
||||
{
|
||||
private CollidersRegistryService _collidersRegistryService;
|
||||
|
||||
private Entity _linkedEntity;
|
||||
|
||||
public Entity LinkedEntity => _linkedEntity;
|
||||
|
||||
public void Initialize(CollidersRegistryService collidersRegistryService)
|
||||
{
|
||||
_collidersRegistryService = collidersRegistryService;
|
||||
}
|
||||
|
||||
public void Link(Entity entity)
|
||||
{
|
||||
_linkedEntity = entity;
|
||||
|
||||
MonoEntityRegistrator[] registrators = GetComponentsInChildren<MonoEntityRegistrator>();
|
||||
|
||||
if (registrators != null)
|
||||
foreach (MonoEntityRegistrator registrator in registrators)
|
||||
registrator.Register(entity);
|
||||
|
||||
EntityView[] views = GetComponentsInChildren<EntityView>();
|
||||
|
||||
if (views != null)
|
||||
foreach (EntityView view in views)
|
||||
view.Link(entity);
|
||||
|
||||
foreach (Collider collider in GetComponentsInChildren<Collider>())
|
||||
_collidersRegistryService.Register(collider, entity);
|
||||
}
|
||||
|
||||
public void Cleanup(Entity entity)
|
||||
{
|
||||
EntityView[] views = GetComponentsInChildren<EntityView>();
|
||||
|
||||
if (views != null)
|
||||
foreach (EntityView view in views)
|
||||
view.Cleanup(entity);
|
||||
|
||||
foreach (Collider collider in GetComponentsInChildren<Collider>())
|
||||
_collidersRegistryService.Unregister(collider);
|
||||
|
||||
_linkedEntity = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0fa38a390e3026e4da73b8535ef0c601
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,9 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace _Project.Develop.Runtime.Entities
|
||||
{
|
||||
public abstract class MonoEntityRegistrator : MonoBehaviour
|
||||
{
|
||||
public abstract void Register(Entity entity);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 939999464fd1f194ca5503fc751733d9
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 96088c9d645e452fbcaf26ca7c50a420
|
||||
timeCreated: 1771425427
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace _Project.Develop.Runtime.Entities
|
||||
{
|
||||
public interface IDisposableSystem : IEntitySystem
|
||||
{
|
||||
void OnDispose();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b59a3191c6d2c654ba8ae385682c038d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace _Project.Develop.Runtime.Entities
|
||||
{
|
||||
public interface IEntitySystem
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d21a2dc9a95626648b96535cd779fd3c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace _Project.Develop.Runtime.Entities
|
||||
{
|
||||
public interface IInitializableSystem: IEntitySystem
|
||||
{
|
||||
void OnInit(Entity entity);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0dbd75b82765bbd48affc4dcb6075b7f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace _Project.Develop.Runtime.Entities
|
||||
{
|
||||
public interface IUpdatableSystem : IEntitySystem
|
||||
{
|
||||
void OnUpdate(float deltaTime);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9f59af20aa1d2414b9d996039654d260
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user