diff --git a/Svelto.ECS/DynamicEntityDescriptorInfo.cs b/Svelto.ECS/DynamicEntityDescriptorInfo.cs index 1aa7c83..b6f7335 100644 --- a/Svelto.ECS/DynamicEntityDescriptorInfo.cs +++ b/Svelto.ECS/DynamicEntityDescriptorInfo.cs @@ -30,12 +30,4 @@ namespace Svelto.ECS public IEntityBuilder[] entitiesToBuild { get; } } - - public struct EntityInfoView : IEntityStruct - { - public EGID ID { get; set; } - public Type type { get; set; } - - public IEntityBuilder[] entitiesToBuild; - } } \ No newline at end of file diff --git a/Svelto.ECS/ECSComponents/ECSRect.cs b/Svelto.ECS/ECSComponents/ECSRect.cs new file mode 100644 index 0000000..29f2a54 --- /dev/null +++ b/Svelto.ECS/ECSComponents/ECSRect.cs @@ -0,0 +1,17 @@ +using UnityEngine; + +namespace Svelto.ECS.Components +{ + public struct ECSRect + { + public float x, y, width, height; + + public ECSRect(Rect imageUvRect) + { + x = imageUvRect.x; + y = imageUvRect.y; + width = imageUvRect.width; + height = imageUvRect.height; + } + } +} \ No newline at end of file diff --git a/Svelto.ECS/ECSComponents/ECSVector2.cs b/Svelto.ECS/ECSComponents/ECSVector2.cs new file mode 100644 index 0000000..15c1748 --- /dev/null +++ b/Svelto.ECS/ECSComponents/ECSVector2.cs @@ -0,0 +1,14 @@ +namespace Svelto.ECS.Components +{ + public struct ECSVector2 + { + public float x; + public float y; + + public ECSVector2(float X, float Y) + { + x = X; + y = Y; + } + } +} \ No newline at end of file diff --git a/Svelto.ECS/EnginesRoot.Entities.cs b/Svelto.ECS/EnginesRoot.Entities.cs index 15903fb..feeff3e 100644 --- a/Svelto.ECS/EnginesRoot.Entities.cs +++ b/Svelto.ECS/EnginesRoot.Entities.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using Svelto.Common; using Svelto.DataStructures.Experimental; diff --git a/Svelto.ECS/EnginesRoot.GenericEntityFunctions.cs b/Svelto.ECS/EnginesRoot.GenericEntityFunctions.cs index 9de4b31..101519c 100644 --- a/Svelto.ECS/EnginesRoot.GenericEntityFunctions.cs +++ b/Svelto.ECS/EnginesRoot.GenericEntityFunctions.cs @@ -1,4 +1,4 @@ -using System; +using System; using Svelto.ECS.Internal; namespace Svelto.ECS diff --git a/Svelto.ECS/EntitiesDB.cs b/Svelto.ECS/EntitiesDB.cs index ffed7cf..f16151c 100644 --- a/Svelto.ECS/EntitiesDB.cs +++ b/Svelto.ECS/EntitiesDB.cs @@ -3,6 +3,7 @@ #endif using System; +using System.Collections; using System.Collections.Generic; using System.Diagnostics; using Svelto.DataStructures; @@ -151,6 +152,11 @@ namespace Svelto.ECS.Internal return HasAny((int) groupStruct); } + public IEnumerator IterateUntilEntityExists(ExclusiveGroup @group) where T : IEntityStruct + { + while (HasAny(group) == false) yield return null; + } + public bool TryQueryEntityView(EGID entityegid, out T entityView) where T : class, IEntityStruct { return TryQueryEntityViewInGroupInternal(entityegid, out entityView); diff --git a/Svelto.ECS/EntityBuilder.CheckFields.cs b/Svelto.ECS/EntityBuilder.CheckFields.cs index 4f98c45..51cb6e5 100644 --- a/Svelto.ECS/EntityBuilder.CheckFields.cs +++ b/Svelto.ECS/EntityBuilder.CheckFields.cs @@ -4,7 +4,6 @@ using System.Diagnostics; #endif using System; using System.Reflection; -using Svelto.ECS.Internal; namespace Svelto.ECS { @@ -15,7 +14,8 @@ namespace Svelto.ECS #endif static void CheckFields(Type type, bool needsReflection, bool isRoot) { - if (ENTITY_VIEW_TYPE == typeof(EntityInfoView) || type == EGIDType || type == ExclusiveGroupStructType) return; + if (ENTITY_VIEW_TYPE == ENTITYINFOVIEW_TYPE || type == EGIDType || type == ECLUSIVEGROUPSTRUCTTYPE) + return; { var methods = type.GetMethods(BindingFlags.Public | @@ -27,25 +27,25 @@ namespace Svelto.ECS if (isRoot) { if (properties.Length > 1) - ProcessError("Entity views cannot have public methods or properties", type); + ProcessError("Entity views cannot have public methods or properties.", type); if (methods.Length > properties.Length + 1) - ProcessError("Entity views cannot have public methods or properties", type); + ProcessError("Entity views cannot have public methods or properties.", type); } else { if (properties.Length > 0) - ProcessError("Entity components fields cannot have public methods or properties", type); + ProcessError("Entity components fields cannot have public methods or properties.", type); if (methods.Length > 0) - ProcessError("Entity components fields cannot have public methods or properties", type); + ProcessError("Entity components fields cannot have public methods or properties.", type); } } if (needsReflection == false) { if (type.IsClass) - throw new ECSException("EntityStructs must be structs - entity view: ".FastConcat(ENTITY_VIEW_TYPE.ToString())); + throw new EntityStructException("EntityStructs must be structs.", ENTITY_VIEW_TYPE, type); var fields = type.GetFields(BindingFlags.Public | BindingFlags.Instance); @@ -62,14 +62,14 @@ namespace Svelto.ECS var fields = type.GetFields(BindingFlags.Public | BindingFlags.Instance); if (fields.Length < 1) - ProcessError("Entity View Structs must hold only entity components interfaces", type); + ProcessError("Entity View Structs must hold only entity components interfaces.", type); for (int i = fields.Length - 1; i >= 0; --i) { var field = fields[i]; if (field.FieldType.IsInterfaceEx() == false) - ProcessError("Entity View Structs must hold only entity components interfaces", type); + ProcessError("Entity View Structs must hold only entity components interfaces.", type); var properties = field.FieldType.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly); @@ -79,11 +79,13 @@ namespace Svelto.ECS if (properties[j].PropertyType.IsGenericType == true) { var genericTypeDefinition = properties[j].PropertyType.GetGenericTypeDefinition(); - if (genericTypeDefinition == typeof(DispatchOnSet<>) || - genericTypeDefinition == typeof(DispatchOnChange<>)) continue; + if (genericTypeDefinition == DISPATCHONSETTYPE || + genericTypeDefinition == DISPATCHONCHANGETYPE) continue; } - - SubCheckFields(properties[j].PropertyType); + + var propertyType = properties[j].PropertyType; + if (propertyType != STRINGTYPE) + SubCheckFields(propertyType); } } } @@ -93,7 +95,7 @@ namespace Svelto.ECS { if (fieldFieldType.IsPrimitive == true || fieldFieldType.IsValueType == true) { - if (fieldFieldType.IsValueType && !fieldFieldType.IsEnum && fieldFieldType.IsPrimitive == false) + if (fieldFieldType.IsValueType == true && !fieldFieldType.IsEnum && fieldFieldType.IsPrimitive == false) { CheckFields(fieldFieldType, false, false); } @@ -101,7 +103,8 @@ namespace Svelto.ECS return; } - ProcessError("Entity Structs field and Entity View Struct components must hold value types", fieldFieldType); + ProcessError("Entity Structs field and Entity View Struct components must hold value types.", + fieldFieldType); } static void ProcessError(string message, Type type) @@ -111,8 +114,11 @@ namespace Svelto.ECS #endif } - static readonly Type EGIDType = typeof(EGID); - static readonly Type ExclusiveGroupStructType = typeof(ExclusiveGroup.ExclusiveGroupStruct); + static readonly Type EGIDType = typeof(EGID); + static readonly Type ECLUSIVEGROUPSTRUCTTYPE = typeof(ExclusiveGroup.ExclusiveGroupStruct); + static readonly Type DISPATCHONSETTYPE = typeof(DispatchOnSet<>); + static readonly Type DISPATCHONCHANGETYPE = typeof(DispatchOnChange<>); + static readonly Type STRINGTYPE = typeof(String); } public class EntityStructException : Exception diff --git a/Svelto.ECS/EntityBuilder.cs b/Svelto.ECS/EntityBuilder.cs index fbbd3bb..9bab870 100644 --- a/Svelto.ECS/EntityBuilder.cs +++ b/Svelto.ECS/EntityBuilder.cs @@ -83,6 +83,7 @@ namespace Svelto.ECS static readonly Type ENTITY_VIEW_TYPE = typeof(T); static readonly T DEFAULT_IT = default(T); + static readonly Type ENTITYINFOVIEW_TYPE = typeof(EntityInfoView); static readonly bool NEEDS_REFLECTION = typeof(IEntityViewStruct).IsAssignableFrom(typeof(T)); static readonly string ENTITY_VIEW_NAME = ENTITY_VIEW_TYPE.ToString(); diff --git a/Svelto.ECS/EntityInfoView.cs b/Svelto.ECS/EntityInfoView.cs new file mode 100644 index 0000000..a1f8276 --- /dev/null +++ b/Svelto.ECS/EntityInfoView.cs @@ -0,0 +1,12 @@ +using System; + +namespace Svelto.ECS +{ + public struct EntityInfoView : IEntityStruct + { + public EGID ID { get; set; } + public Type type { get; set; } + + public IEntityBuilder[] entitiesToBuild; + } +} \ No newline at end of file diff --git a/Svelto.ECS/EntityStructInitializer.cs b/Svelto.ECS/EntityStructInitializer.cs index 318759c..fdedb52 100644 --- a/Svelto.ECS/EntityStructInitializer.cs +++ b/Svelto.ECS/EntityStructInitializer.cs @@ -14,6 +14,9 @@ namespace Svelto.ECS public void Init(T initializer) where T: struct, IEntityStruct { + DBC.ECS.Check.Require(_current.ContainsKey(typeof(T) )== true, + "Entity to initialise not generated by the EntiyDescriptor"); + var typeSafeDictionary = (TypeSafeDictionary) _current[typeof(T)]; initializer.ID = _id; diff --git a/Svelto.ECS/ExclusiveGroups.cs b/Svelto.ECS/ExclusiveGroups.cs index 2d57ea7..322e603 100644 --- a/Svelto.ECS/ExclusiveGroups.cs +++ b/Svelto.ECS/ExclusiveGroups.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; #pragma warning disable 660,661 diff --git a/Svelto.ECS/Extensions/Unity/IImplementor.cs b/Svelto.ECS/Extensions/Unity/IImplementor.cs new file mode 100644 index 0000000..6d58780 --- /dev/null +++ b/Svelto.ECS/Extensions/Unity/IImplementor.cs @@ -0,0 +1,6 @@ +namespace Svelto.ECS.Unity +{ + public interface IImplementor + { + } +} \ No newline at end of file diff --git a/Svelto.ECS/Extensions/Unity/SveltoEntityFactoryForUnity.cs b/Svelto.ECS/Extensions/Unity/SveltoEntityFactoryForUnity.cs new file mode 100644 index 0000000..5dae91b --- /dev/null +++ b/Svelto.ECS/Extensions/Unity/SveltoEntityFactoryForUnity.cs @@ -0,0 +1,32 @@ +#if UNITY_5 || UNITY_5_3_OR_NEWER +using Svelto.Context; +using UnityEngine; + +namespace Svelto.ECS.Unity +{ + public static class SveltoEntityFactoryForUnity + { + public static void Create(EGID ID, UnityContext contextHolder, + IEntityFactory factory) where T : MonoBehaviour, IEntityDescriptorHolder + { + var holder = contextHolder.GetComponentInChildren(true); + var implementors = holder.GetComponents(); + + factory.BuildEntity(ID, holder.GetDescriptor(), implementors); + } + + public static void CreateAll(ExclusiveGroup group, UnityContext contextHolder, + IEntityFactory factory) where T : MonoBehaviour, IEntityDescriptorHolder + { + var holders = contextHolder.GetComponentsInChildren(true); + + foreach (var holder in holders) + { + var implementors = holder.GetComponents(); + + factory.BuildEntity(holder.GetInstanceID(), group, holder.GetDescriptor(), implementors); + } + } + } +} +#endif \ No newline at end of file diff --git a/Svelto.ECS/IEntitiesDB.cs b/Svelto.ECS/IEntitiesDB.cs index 13741b1..b8e9c36 100644 --- a/Svelto.ECS/IEntitiesDB.cs +++ b/Svelto.ECS/IEntitiesDB.cs @@ -1,3 +1,4 @@ +using System.Collections; using Svelto.DataStructures; namespace Svelto.ECS @@ -109,6 +110,7 @@ namespace Svelto.ECS bool HasAny(int group) where T:IEntityStruct; bool HasAny(ExclusiveGroup.ExclusiveGroupStruct groupStruct) where T:IEntityStruct; + IEnumerator IterateUntilEntityExists(ExclusiveGroup group) where T:IEntityStruct; } public delegate void EntityAction(ref T target, ref W value);