Browse Source

introduce experimental ECS components and EntityFactoryForUnity

clean up code
tags/2.7
sebas77 5 years ago
parent
commit
c04eeae531
14 changed files with 119 additions and 28 deletions
  1. +0
    -8
      Svelto.ECS/DynamicEntityDescriptorInfo.cs
  2. +17
    -0
      Svelto.ECS/ECSComponents/ECSRect.cs
  3. +14
    -0
      Svelto.ECS/ECSComponents/ECSVector2.cs
  4. +1
    -1
      Svelto.ECS/EnginesRoot.Entities.cs
  5. +1
    -1
      Svelto.ECS/EnginesRoot.GenericEntityFunctions.cs
  6. +6
    -0
      Svelto.ECS/EntitiesDB.cs
  7. +23
    -17
      Svelto.ECS/EntityBuilder.CheckFields.cs
  8. +1
    -0
      Svelto.ECS/EntityBuilder.cs
  9. +12
    -0
      Svelto.ECS/EntityInfoView.cs
  10. +3
    -0
      Svelto.ECS/EntityStructInitializer.cs
  11. +1
    -1
      Svelto.ECS/ExclusiveGroups.cs
  12. +6
    -0
      Svelto.ECS/Extensions/Unity/IImplementor.cs
  13. +32
    -0
      Svelto.ECS/Extensions/Unity/SveltoEntityFactoryForUnity.cs
  14. +2
    -0
      Svelto.ECS/IEntitiesDB.cs

+ 0
- 8
Svelto.ECS/DynamicEntityDescriptorInfo.cs View File

@@ -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;
}
}

+ 17
- 0
Svelto.ECS/ECSComponents/ECSRect.cs View File

@@ -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;
}
}
}

+ 14
- 0
Svelto.ECS/ECSComponents/ECSVector2.cs View File

@@ -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;
}
}
}

+ 1
- 1
Svelto.ECS/EnginesRoot.Entities.cs View File

@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using Svelto.Common;
using Svelto.DataStructures.Experimental;


+ 1
- 1
Svelto.ECS/EnginesRoot.GenericEntityFunctions.cs View File

@@ -1,4 +1,4 @@
using System;
using System;
using Svelto.ECS.Internal;

namespace Svelto.ECS


+ 6
- 0
Svelto.ECS/EntitiesDB.cs View File

@@ -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<T>((int) groupStruct);
}

public IEnumerator IterateUntilEntityExists<T>(ExclusiveGroup @group) where T : IEntityStruct
{
while (HasAny<T>(group) == false) yield return null;
}

public bool TryQueryEntityView<T>(EGID entityegid, out T entityView) where T : class, IEntityStruct
{
return TryQueryEntityViewInGroupInternal(entityegid, out entityView);


+ 23
- 17
Svelto.ECS/EntityBuilder.CheckFields.cs View File

@@ -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


+ 1
- 0
Svelto.ECS/EntityBuilder.cs View File

@@ -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();



+ 12
- 0
Svelto.ECS/EntityInfoView.cs View File

@@ -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;
}
}

+ 3
- 0
Svelto.ECS/EntityStructInitializer.cs View File

@@ -14,6 +14,9 @@ namespace Svelto.ECS

public void Init<T>(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<T>) _current[typeof(T)];

initializer.ID = _id;


+ 1
- 1
Svelto.ECS/ExclusiveGroups.cs View File

@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
#pragma warning disable 660,661



+ 6
- 0
Svelto.ECS/Extensions/Unity/IImplementor.cs View File

@@ -0,0 +1,6 @@
namespace Svelto.ECS.Unity
{
public interface IImplementor
{
}
}

+ 32
- 0
Svelto.ECS/Extensions/Unity/SveltoEntityFactoryForUnity.cs View File

@@ -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<T>(EGID ID, UnityContext contextHolder,
IEntityFactory factory) where T : MonoBehaviour, IEntityDescriptorHolder
{
var holder = contextHolder.GetComponentInChildren<T>(true);
var implementors = holder.GetComponents<IImplementor>();

factory.BuildEntity(ID, holder.GetDescriptor(), implementors);
}
public static void CreateAll<T>(ExclusiveGroup group, UnityContext contextHolder,
IEntityFactory factory) where T : MonoBehaviour, IEntityDescriptorHolder
{
var holders = contextHolder.GetComponentsInChildren<T>(true);

foreach (var holder in holders)
{
var implementors = holder.GetComponents<IImplementor>();

factory.BuildEntity(holder.GetInstanceID(), group, holder.GetDescriptor(), implementors);
}
}
}
}
#endif

+ 2
- 0
Svelto.ECS/IEntitiesDB.cs View File

@@ -1,3 +1,4 @@
using System.Collections;
using Svelto.DataStructures;

namespace Svelto.ECS
@@ -109,6 +110,7 @@ namespace Svelto.ECS
bool HasAny<T>(int group) where T:IEntityStruct;
bool HasAny<T>(ExclusiveGroup.ExclusiveGroupStruct groupStruct) where T:IEntityStruct;
IEnumerator IterateUntilEntityExists<T>(ExclusiveGroup group) where T:IEntityStruct;
}

public delegate void EntityAction<T, W>(ref T target, ref W value);


Loading…
Cancel
Save