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,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:
|
||||
Reference in New Issue
Block a user