This repository has been archived by the owner on Jan 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Better gameobject initialization (#1333)
- Loading branch information
Paul Balaji
authored
Mar 24, 2020
1 parent
96e691b
commit 9c0b93f
Showing
9 changed files
with
219 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
workers/unity/Packages/io.improbable.gdk.gameobjectcreation/EntityTypeExpectations.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Unity.Entities; | ||
|
||
namespace Improbable.Gdk.GameObjectCreation | ||
{ | ||
/// <summary> | ||
/// Holds mappings from entity types to the set of components expected before creating their GameObjects. | ||
/// </summary> | ||
public class EntityTypeExpectations | ||
{ | ||
private ComponentType[] defaultExpectation = new ComponentType[] { }; | ||
|
||
private readonly Dictionary<string, ComponentType[]> entityExpectations | ||
= new Dictionary<string, ComponentType[]>(); | ||
|
||
public void RegisterDefault(IEnumerable<Type> defaultComponentTypes = null) | ||
{ | ||
if (defaultComponentTypes == null) | ||
{ | ||
defaultExpectation = new ComponentType[] { }; | ||
} | ||
else | ||
{ | ||
defaultExpectation = defaultComponentTypes | ||
.Select(type => new ComponentType(type, ComponentType.AccessMode.ReadOnly)) | ||
.ToArray(); | ||
} | ||
} | ||
|
||
public void RegisterEntityType(string entityType, IEnumerable<Type> expectedComponentTypes = null) | ||
{ | ||
ComponentType[] expectedTypes; | ||
|
||
if (expectedComponentTypes == null) | ||
{ | ||
expectedTypes = new ComponentType[] { }; | ||
} | ||
else | ||
{ | ||
expectedTypes = expectedComponentTypes | ||
.Select(type => new ComponentType(type, ComponentType.AccessMode.ReadOnly)) | ||
.ToArray(); | ||
} | ||
|
||
entityExpectations.Add(entityType, expectedTypes); | ||
} | ||
|
||
internal ComponentType[] GetExpectedTypes(string entityType) | ||
{ | ||
if (!entityExpectations.TryGetValue(entityType, out var types)) | ||
{ | ||
return defaultExpectation; | ||
} | ||
|
||
return types; | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
workers/unity/Packages/io.improbable.gdk.gameobjectcreation/EntityTypeExpectations.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.