Skip to content

Commit

Permalink
Реализация
Browse files Browse the repository at this point in the history
  • Loading branch information
Anton committed May 28, 2022
1 parent 5097ac2 commit a3de18b
Show file tree
Hide file tree
Showing 22 changed files with 377 additions and 0 deletions.
7 changes: 7 additions & 0 deletions LICENSE.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions README.md.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Runtime.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Runtime/Object.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions Runtime/Object/ComponentForObject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using UnityEngine;
using System;

namespace Leopotam.EcsLite.MonoPool.Object
{
[Serializable]
public struct ComponentForObject
{
public Type Type => Type.GetType(_type);
[SerializeField] private string _type;
public object Value => _value;
[SerializeReference] public object _value;

public ComponentForObject(Type type, object value)
{
_type = type.Name;
_value = value;
}
}
}
11 changes: 11 additions & 0 deletions Runtime/Object/ComponentForObject.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Runtime/Object/Editor.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 52 additions & 0 deletions Runtime/Object/Editor/ComponentForObjectDrawer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using UnityEditor;
using UnityEngine;
using System.Linq;
using System;

namespace Leopotam.EcsLite.MonoPool.Object
{
[CustomPropertyDrawer(typeof(ComponentForObject))]
public sealed class ComponentForObjectDrawer : PropertyDrawer
{
private readonly string[] SOLUTION_NAMES = new[] { "Assembly-CSharp" };

public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
float height = EditorGUIUtility.singleLineHeight;
height += (EditorGUIUtility.singleLineHeight + 2) * (1 + property.FindPropertyRelative("_value").CountInProperty());
return height;
}
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
EditorGUI.BeginProperty(position, label, property);
{
SerializedProperty valueFromProperty = property.FindPropertyRelative("_value");
SerializedProperty typeFromProperty = property.FindPropertyRelative("_type");

Rect rect = new(position.x, position.y, position.size.x, 18);
rect.y += 20;

Assembly assembly = AppDomain.CurrentDomain.GetAssemblies().Where(x => SOLUTION_NAMES.Contains(x.GetName().Name)).ToArray()[0];
List<Type> typeList = assembly.GetTypes().Where(x => x.IsValueType && x.IsEnum is not true && x.IsGenericType is not true).ToList();
typeList.Remove(typeof(ComponentForObject));

Type type = typeList[typeList.Select(x => x.Name).ToList().IndexOf(typeFromProperty.stringValue)];
Type oldType = type;
if(type == null)
type = typeList[0];

type = typeList[EditorGUI.Popup(rect, typeList.IndexOf(type), typeList.Select(x => x.Name).ToArray())];
if(type != oldType)
valueFromProperty.managedReferenceValue = Activator.CreateInstance(type);
typeFromProperty.stringValue = type.Name;

rect.y += 20;
EditorGUI.PropertyField(rect, valueFromProperty, true);
}
EditorGUI.EndProperty();
}
}
}
11 changes: 11 additions & 0 deletions Runtime/Object/Editor/ComponentForObjectDrawer.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions Runtime/Object/IObjectTemplate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Collections.Generic;
using UnityEngine;

namespace Leopotam.EcsLite.MonoPool.Object
{
public interface IObjectTemplate<out TObject>
where TObject : MonoBehaviour
{
public TObject ObjectInstance { get; }
public IReadOnlyList<ComponentForObject> Components { get; }
}
}
11 changes: 11 additions & 0 deletions Runtime/Object/IObjectTemplate.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions Runtime/Object/MonoPoolObject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using UnityEngine;
using System;

namespace Leopotam.EcsLite.MonoPool.Object
{
public abstract class MonoPoolObject<TTemplate> : MonoBehaviour
where TTemplate : IObjectTemplate<MonoPoolObject<TTemplate>>
{
public event Action OnReturn;

public int Entity { get; private set; }
public TTemplate Template { get; private set; }

private event Action<MonoPoolObject<TTemplate>> OnReset;

public static (TObject obj, Action<int> SetIssuingNewIdentifier) Create<TObject, TObjectTemplate>(TObjectTemplate template, Transform parent, Action<TObject> onReset)
where TObject : MonoPoolObject<TObjectTemplate>
where TObjectTemplate : IObjectTemplate<TObject>
{
TObject answer = Instantiate(template.ObjectInstance, parent);
answer.Template = template;
answer.OnReset += (MonoPoolObject<TObjectTemplate> obj) => onReset(obj as TObject);
return (answer, (int entity) => { answer.Entity = entity; });
}
public void Return()
{
OnReturn?.Invoke();
OnReset?.Invoke(this);
OnReturn = null;
}
}
}
11 changes: 11 additions & 0 deletions Runtime/Object/MonoPoolObject.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Runtime/Placemark.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Runtime/Placemark/PlacemarkJustCreateObjectComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace Leopotam.EcsLite.MonoPool.Placemark
{
public struct PlacemarkJustCreateObjectComponent { }
}
11 changes: 11 additions & 0 deletions Runtime/Placemark/PlacemarkJustCreateObjectComponent.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions Runtime/Placemark/PlacemarkObjectRequestComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace Leopotam.EcsLite.MonoPool.Placemark
{
using Object;

public struct PlacemarkObjectRequestComponent<TObject, TObjectTemplate>
where TObject : MonoPoolObject<TObjectTemplate>
where TObjectTemplate : IObjectTemplate<TObject>
{
public readonly TObjectTemplate Template;

public PlacemarkObjectRequestComponent(TObjectTemplate template)
{
Template = template;
}
}
}
11 changes: 11 additions & 0 deletions Runtime/Placemark/PlacemarkObjectRequestComponent.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions Runtime/Placemark/PlacemarkPoolObjectComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace Leopotam.EcsLite.MonoPool.Placemark
{
using Object;

public struct PlacemarkPoolObjectComponent<TObject, TObjectTemplate>
where TObject : MonoPoolObject<TObjectTemplate>
where TObjectTemplate : IObjectTemplate<TObject>
{
public readonly TObject Signature;

public PlacemarkPoolObjectComponent(TObject signature)
{
Signature = signature;
}
}
}
11 changes: 11 additions & 0 deletions Runtime/Placemark/PlacemarkPoolObjectComponent.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

91 changes: 91 additions & 0 deletions Runtime/StorageSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using System.Collections.Generic;
using UnityEngine;
using System;

namespace Leopotam.EcsLite.MonoPool
{
using Placemark;
using Object;

public sealed class StorageSystem<TObject, TObjectTemplate> : IEcsInitSystem, IEcsRunSystem
where TObject : MonoPoolObject<TObjectTemplate>
where TObjectTemplate : IObjectTemplate<TObject>
{
private EcsPool<PlacemarkObjectRequestComponent<TObject, TObjectTemplate>> _placemarkObjectRequestPool = null;
private EcsPool<PlacemarkPoolObjectComponent<TObject, TObjectTemplate>> _placemarkPoolObjectPool = null;
private EcsPool<PlacemarkJustCreateObjectComponent> _placemarkJustCreateObjectPool = null;
private EcsFilter _filter = null;
private EcsWorld _world = null;

private readonly Dictionary<TObject, Action<int>> _objectsThierInstallToEntity = new();
private readonly Dictionary<TObjectTemplate, Queue<TObject>> _freeForUse = new();
private readonly Transform _storageLocation;

public StorageSystem(Transform storageLocation)
{
_storageLocation = storageLocation;
}
public void Init(EcsSystems systems)
{
_world = systems.GetWorld();
_filter = _world.Filter<PlacemarkObjectRequestComponent<TObject, TObjectTemplate>>().End();
_placemarkPoolObjectPool = _world.GetPool<PlacemarkPoolObjectComponent<TObject, TObjectTemplate>>();
_placemarkObjectRequestPool = _world.GetPool<PlacemarkObjectRequestComponent<TObject, TObjectTemplate>>();
_placemarkJustCreateObjectPool = _world.GetPool<PlacemarkJustCreateObjectComponent>();
}
public void Run(EcsSystems systems)
{
foreach(int entity in _filter)
{
PlacemarkObjectRequestComponent<TObject, TObjectTemplate> placemarkObjectRequest = _placemarkObjectRequestPool.Get(entity);
Receive(placemarkObjectRequest.Template);
_world.DelEntity(entity);
}
}
private void Receive(TObjectTemplate template)
{
TObject obj = GetObject(template);
int entity = _world.NewEntity();

_objectsThierInstallToEntity[obj].Invoke(entity);
for(int index = 0; index < template.Components.Count; index++)
{
ComponentForObject component = template.Components[index];
AddComponentToObject(entity, component);
}
obj.transform.parent = null;
_placemarkPoolObjectPool.Add(entity) = new PlacemarkPoolObjectComponent<TObject, TObjectTemplate>(obj);
_placemarkJustCreateObjectPool.Add(entity);
}
private TObject GetObject(TObjectTemplate template)
{
if (_freeForUse.ContainsKey(template) is not true)
_freeForUse.Add(template, new Queue<TObject>());

return _freeForUse[template].Count switch
{
0 => Create(template),
_ => _freeForUse[template].Dequeue()
};
}
private TObject Create(TObjectTemplate template)
{
(TObject obj, Action<int> setIssuingNewIdentifier) = MonoPoolObject<TObjectTemplate>.Create<TObject, TObjectTemplate>(template, _storageLocation, Returning);
_objectsThierInstallToEntity.Add(obj, setIssuingNewIdentifier);

return obj;
}
private void AddComponentToObject(int entity, ComponentForObject component)
{
IEcsPool pool = typeof(EcsWorld).GetMethod("GetPool").MakeGenericMethod(component.Type).Invoke(_world, null) as IEcsPool;
pool.AddRaw(entity, component.Value);
}
private void Returning(TObject obj)
{
obj.transform.parent = _storageLocation;
_freeForUse[obj.Template].Enqueue(obj);
_world.DelEntity(obj.Entity);
_objectsThierInstallToEntity[obj].Invoke(-1);
}
}
}
11 changes: 11 additions & 0 deletions Runtime/StorageSystem.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a3de18b

Please sign in to comment.