This repository has been archived by the owner on Jan 18, 2022. It is now read-only.
/
View.cs
153 lines (126 loc) · 5.07 KB
/
View.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
using System;
using System.Collections.Generic;
using System.Linq;
using Improbable.Worker.CInterop;
using UnityEngine;
namespace Improbable.Gdk.Core
{
public class View
{
private readonly Dictionary<Type, IViewStorage> typeToViewStorage = new Dictionary<Type, IViewStorage>();
private readonly Dictionary<uint, IViewStorage> componentIdToViewStorage = new Dictionary<uint, IViewStorage>();
private readonly List<IViewStorage> viewStorages = new List<IViewStorage>();
private readonly HashSet<EntityId> entities = new HashSet<EntityId>();
private readonly Dictionary<string, string> workerFlags = new Dictionary<string, string>();
public View()
{
var types = ComponentDatabase.Metaclasses.Select(type => type.Value.ViewStorage);
foreach (var type in types)
{
var instance = (IViewStorage) Activator.CreateInstance(type);
typeToViewStorage.Add(instance.GetSnapshotType(), instance);
typeToViewStorage.Add(instance.GetUpdateType(), instance);
componentIdToViewStorage.Add(instance.GetComponentId(), instance);
viewStorages.Add(instance);
}
}
public void UpdateComponent<T>(EntityId entityId, in T update) where T : struct, ISpatialComponentUpdate
{
var storage = (IViewComponentUpdater<T>) typeToViewStorage[typeof(T)];
storage.ApplyUpdate(entityId.Id, in update);
}
public HashSet<EntityId> GetEntityIds()
{
return entities;
}
public bool HasEntity(EntityId entityId)
{
return entities.Contains(entityId);
}
// TODO: Make ref readonly when we have a dictionary type with ref indexing semantics.
public T GetComponent<T>(EntityId entityId) where T : struct, ISpatialComponentSnapshot
{
if (!HasComponent<T>(entityId))
{
throw new ArgumentException($"The view does not have entity with Entity ID: {entityId.Id} and component with ID: {ComponentDatabase.GetSnapshotComponentId<T>()}");
}
var storage = (IViewComponentStorage<T>) typeToViewStorage[typeof(T)];
return storage.GetComponent(entityId.Id);
}
public bool HasComponent<T>(EntityId entityId) where T : struct, ISpatialComponentSnapshot
{
if (!HasEntity(entityId))
{
return false;
}
var storage = typeToViewStorage[typeof(T)];
return storage.HasComponent(entityId.Id);
}
public bool HasComponent(EntityId entityId, uint componentId)
{
if (!HasEntity(entityId))
{
return false;
}
var storage = componentIdToViewStorage[componentId];
return storage.HasComponent(entityId.Id);
}
public Authority GetAuthority<T>(EntityId entityId) where T : struct, ISpatialComponentSnapshot
{
if (!HasEntity(entityId))
{
throw new ArgumentException($"The view does not have entity with Entity ID: {entityId.Id}");
}
return typeToViewStorage[typeof(T)].GetAuthority(entityId.Id);
}
public Authority GetAuthority(EntityId entityId, uint componentId)
{
if (!HasEntity(entityId))
{
throw new ArgumentException($"The view does not have entity with Entity ID: {entityId.Id}");
}
return componentIdToViewStorage[componentId].GetAuthority(entityId.Id);
}
public bool IsAuthoritative<T>(EntityId entityId) where T : struct, ISpatialComponentSnapshot
{
if (!HasComponent<T>(entityId))
{
return false;
}
return typeToViewStorage[typeof(T)].GetAuthority(entityId.Id) == Authority.Authoritative;
}
public bool IsAuthoritative(EntityId entityId, uint componentId)
{
if (!HasComponent(entityId, componentId))
{
return false;
}
return componentIdToViewStorage[componentId].GetAuthority(entityId.Id) == Authority.Authoritative;
}
public string GetWorkerFlag(string name)
{
return workerFlags.TryGetValue(name, out var value) ? value : null;
}
internal void ApplyDiff(ViewDiff diff)
{
var entitiesAdded = diff.GetEntitiesAdded();
foreach (var entity in entitiesAdded)
{
entities.Add(entity);
}
var entitiesRemoved = diff.GetEntitiesRemoved();
foreach (var entity in entitiesRemoved)
{
entities.Remove(entity);
}
foreach (var storage in viewStorages)
{
storage.ApplyDiff(diff);
}
foreach (var pair in diff.GetWorkerFlagChanges())
{
workerFlags[pair.Item1] = pair.Item2;
}
}
}
}