This repository has been archived by the owner on Jan 18, 2022. It is now read-only.
/
CommandSenderComponentSystem.cs
66 lines (56 loc) · 2.07 KB
/
CommandSenderComponentSystem.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
using System;
using System.Collections.Generic;
using Improbable.Gdk.Core;
using Unity.Entities;
namespace Improbable.Gdk.ReactiveComponents
{
[DisableAutoCreation]
[AlwaysUpdateSystem]
[UpdateInGroup(typeof(SpatialOSReceiveGroup.InternalSpatialOSReceiveGroup))]
[UpdateAfter(typeof(SpatialOSReceiveSystem))]
public class CommandSenderComponentSystem : ComponentSystem
{
private readonly List<ICommandSenderComponentManager> managers = new List<ICommandSenderComponentManager>();
private WorkerSystem workerSystem;
private EntitySystem entitySystem;
protected override void OnCreate()
{
base.OnCreate();
workerSystem = World.GetExistingSystem<WorkerSystem>();
entitySystem = World.GetExistingSystem<EntitySystem>();
foreach (var type in ReflectionUtility.GetNonAbstractTypes(typeof(ICommandSenderComponentManager)))
{
var instance = (ICommandSenderComponentManager) Activator.CreateInstance(type);
managers.Add(instance);
// Add stuff to the worker entity
instance.AddComponents(workerSystem.WorkerEntity, EntityManager, World);
}
}
protected override void OnDestroy()
{
foreach (var manager in managers)
{
manager.Clean(World);
}
base.OnDestroy();
}
protected override void OnUpdate()
{
foreach (var entityId in entitySystem.GetEntitiesAdded())
{
foreach (var manager in managers)
{
workerSystem.TryGetEntity(entityId, out var entity);
manager.AddComponents(entity, EntityManager, World);
}
}
foreach (var entityId in entitySystem.GetEntitiesRemoved())
{
foreach (var manager in managers)
{
manager.RemoveComponents(entityId, EntityManager, World);
}
}
}
}
}