This repository has been archived by the owner on Jan 18, 2022. It is now read-only.
/
EntityTypeExpectations.cs
60 lines (52 loc) · 1.85 KB
/
EntityTypeExpectations.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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;
}
}
}