This repository has been archived by the owner on Jan 18, 2022. It is now read-only.
/
WorkerSystem.cs
111 lines (94 loc) · 3.81 KB
/
WorkerSystem.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
using System.Collections.Generic;
using Improbable.Worker.CInterop;
using Unity.Entities;
using UnityEngine;
using Entity = Unity.Entities.Entity;
namespace Improbable.Gdk.Core
{
/// <summary>
/// A SpatialOS worker instance.
/// </summary>
[DisableAutoCreation]
public class WorkerSystem : ComponentSystem
{
/// <summary>
/// An ECS entity that represents the Worker.
/// </summary>
public Entity WorkerEntity;
public readonly Connection Connection;
public readonly ILogDispatcher LogDispatcher;
public readonly string WorkerType;
public readonly Vector3 Origin;
internal MessagesToSend MessagesToSend;
internal readonly View View = new View();
internal readonly IConnectionHandler ConnectionHandler;
internal readonly Dictionary<EntityId, Entity> EntityIdToEntity = new Dictionary<EntityId, Entity>();
internal ViewDiff Diff;
public WorkerSystem(IConnectionHandler connectionHandler, Connection connection, ILogDispatcher logDispatcher, string workerType, Vector3 origin)
{
Connection = connection;
LogDispatcher = logDispatcher;
WorkerType = workerType;
Origin = origin;
ConnectionHandler = connectionHandler;
MessagesToSend = connectionHandler.GetMessagesToSendContainer();
}
/// <summary>
/// Attempts to find an ECS entity associated with a SpatialOS entity ID.
/// </summary>
/// <param name="entityId">The SpatialOS entity ID.</param>
/// <param name="entity">
/// When this method returns, contains the ECS entity associated with the SpatialOS entity ID if one was
/// found, else the default value for <see cref="Entity" />.
/// </param>
/// <returns>
/// True, if an ECS entity associated with the SpatialOS entity ID was found, false otherwise.
/// </returns>
public bool TryGetEntity(EntityId entityId, out Entity entity)
{
return EntityIdToEntity.TryGetValue(entityId, out entity);
}
/// <summary>
/// Checks whether a SpatialOS entity is checked out on this worker.
/// </summary>
/// <param name="entityId">The SpatialOS entity ID to check for.</param>
/// <returns>True, if the SpatialOS entity is checked out on this worker, false otherwise.</returns>
public bool HasEntity(EntityId entityId)
{
return EntityIdToEntity.ContainsKey(entityId);
}
public void SendLogMessage(string message, string loggerName, LogLevel logLevel, EntityId? entityId)
{
MessagesToSend.AddLogMessage(new LogMessageToSend(message, loggerName, logLevel, entityId?.Id));
}
public void SendMetrics(Metrics metrics)
{
MessagesToSend.AddMetrics(metrics);
}
internal void GetMessages()
{
ConnectionHandler.GetMessagesReceived(ref Diff);
}
internal void SendMessages()
{
ConnectionHandler.PushMessagesToSend(MessagesToSend);
MessagesToSend = ConnectionHandler.GetMessagesToSendContainer();
}
protected override void OnCreateManager()
{
base.OnCreateManager();
var entityManager = World.GetOrCreateManager<EntityManager>();
WorkerEntity = entityManager.CreateEntity(typeof(OnConnected), typeof(WorkerEntityTag));
EntityIdToEntity.Add(new EntityId(0), WorkerEntity);
Enabled = false;
}
protected override void OnDestroyManager()
{
ConnectionHandler.Dispose();
base.OnDestroyManager();
}
protected override void OnUpdate()
{
}
}
}