Skip to content
This repository has been archived by the owner on Jan 18, 2022. It is now read-only.

Better gameobject initialization #1333

Merged
merged 13 commits into from
Mar 24, 2020
Merged
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Unity.Entities;

namespace Improbable.Gdk.GameObjectCreation
{
Expand All @@ -8,29 +10,34 @@ namespace Improbable.Gdk.GameObjectCreation
/// </summary>
public class EntityTypeExpectations
{
private Type[] defaultExpectation;
private ComponentType[] defaultExpectation;

private readonly Dictionary<string, Type[]> entityExpectations
= new Dictionary<string, Type[]>();
private readonly Dictionary<string, ComponentType[]> entityExpectations
= new Dictionary<string, ComponentType[]>();

public void RegisterDefault(Type[] defaultComponentTypes)
public void RegisterDefault(Type[] defaultComponentTypes = null)
{
defaultExpectation = defaultComponentTypes;
defaultExpectation = defaultComponentTypes?
.Select(type => new ComponentType(type, ComponentType.AccessMode.ReadOnly))
.ToArray();
paulbalaji marked this conversation as resolved.
Show resolved Hide resolved
}

public void RegisterEntityType(string entityType, Type[] expectedComponentTypes)
public void RegisterEntityType(string entityType, Type[] expectedComponentTypes = null)
{
entityExpectations.Add(entityType, expectedComponentTypes ?? new Type[] { });
var expectedTypes = expectedComponentTypes?
.Select(type => new ComponentType(type, ComponentType.AccessMode.ReadOnly))
.ToArray();
entityExpectations.Add(entityType, expectedTypes);
}

internal Type[] GetExpectedTypes(string entityType)
internal ComponentType[] GetExpectedTypes(string entityType)
{
if (!entityExpectations.TryGetValue(entityType, out var types))
{
return defaultExpectation;
}

return types;
return types ?? new ComponentType[] { };
paulbalaji marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

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

Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using Improbable.Gdk.Core;
using Improbable.Gdk.Subscriptions;
using Unity.Entities;
Expand Down Expand Up @@ -31,8 +32,6 @@ public GameObjectInitializationSystem(IEntityGameObjectCreator gameObjectCreator
{
this.gameObjectCreator = gameObjectCreator;
this.workerGameObject = workerGameObject;

gameObjectCreator.PopulateEntityTypeExpectations(entityTypeExpectations);
}

protected override void OnCreate()
Expand Down Expand Up @@ -64,6 +63,8 @@ protected override void OnCreate()
All = new[] { ComponentType.ReadOnly<GameObjectInitSystemStateComponent>() },
None = minimumComponentSet
});

gameObjectCreator.PopulateEntityTypeExpectations(entityTypeExpectations);
}

protected override void OnDestroy()
Expand Down Expand Up @@ -91,17 +92,25 @@ protected override void OnUpdate()
{
ProcessRemovedEntities();
}

Linker.FlushCommandBuffer();
}

private void ProcessRemovedEntities()
{
Entities.With(removedEntitiesQuery).ForEach((ref GameObjectInitSystemStateComponent state) =>
{
Linker.UnlinkAllGameObjectsFromEntityId(state.EntityId);
gameObjectCreator.OnEntityRemoved(state.EntityId);
});

Linker.FlushCommandBuffer();
try
{
gameObjectCreator.OnEntityRemoved(state.EntityId);
}
catch (Exception e)
{
Debug.LogException(e);
}
});

EntityManager.RemoveComponent<GameObjectInitSystemStateComponent>(removedEntitiesQuery);
}
Expand All @@ -114,18 +123,23 @@ private void ProcessNewEntities()
var entityType = metadata.EntityType;
var expectedTypes = entityTypeExpectations.GetExpectedTypes(entityType);

if (expectedTypes != null)
foreach (var expectedType in expectedTypes)
{
foreach (var expectedType in expectedTypes)
if (!EntityManager.HasComponent(entity, expectedType))
{
if (!EntityManager.HasComponent(entity, expectedType))
{
return;
}
return;
}
}

gameObjectCreator.OnEntityCreated(entityType, new SpatialOSEntity(entity, EntityManager), Linker);
try
{
gameObjectCreator.OnEntityCreated(entityType, new SpatialOSEntity(entity, EntityManager), Linker);
}
catch (Exception e)
{
Debug.LogException(e);
}

PostUpdateCommands.AddComponent(entity, new GameObjectInitSystemStateComponent
{
EntityId = spatialEntityId.EntityId
Expand Down