This repository has been archived by the owner on Jan 18, 2022. It is now read-only.
/
PlayerLifecycleHelper.cs
74 lines (66 loc) · 3.19 KB
/
PlayerLifecycleHelper.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
using System;
using Improbable.Gdk.Core;
using Unity.Entities;
namespace Improbable.Gdk.PlayerLifecycle
{
public static class PlayerLifecycleHelper
{
/// <summary>
/// Adds the SpatialOS components used by the player lifecycle module to an entity template.
/// </summary>
/// <param name="template">The entity template to add player lifecycle components to.</param>
/// <param name="clientWorkerId">The ID of the client-worker.</param>
/// <param name="serverAccess">The server-worker write access attribute.</param>
public static void AddPlayerLifecycleComponents(EntityTemplate template,
string clientWorkerId,
string serverAccess)
{
template.AddComponent(new PlayerHeartbeatClient.Snapshot(),
EntityTemplate.GetWorkerAccessAttribute(clientWorkerId));
template.AddComponent(new PlayerHeartbeatServer.Snapshot(), serverAccess);
template.AddComponent(new OwningWorker.Snapshot(clientWorkerId), serverAccess);
}
public static bool IsOwningWorker(EntityId entityId, World workerWorld)
{
var worker = workerWorld.GetExistingSystem<WorkerSystem>();
var updateSystem = workerWorld.GetExistingSystem<ComponentUpdateSystem>();
var entitySystem = workerWorld.GetExistingSystem<EntitySystem>();
if (worker == null)
{
throw new InvalidOperationException("Provided World does not have an associated worker");
}
if (!entitySystem.GetEntitiesInView().Contains(entityId))
{
return false;
}
if (!updateSystem.HasComponent(OwningWorker.ComponentId, entityId))
{
return false;
}
var ownerId = updateSystem.GetComponent<OwningWorker.Snapshot>(entityId).WorkerId;
return worker.WorkerId == ownerId;
}
/// <summary>
/// Adds all the systems a client-worker requires for the player lifecycle module.
/// </summary>
/// <param name="world">A world that belongs to a client-worker.</param>
/// <param name="autoRequestPlayerCreation">An option to toggle automatic player creation.</param>
public static void AddClientSystems(World world, bool autoRequestPlayerCreation = true)
{
PlayerLifecycleConfig.AutoRequestPlayerCreation = autoRequestPlayerCreation;
world.GetOrCreateSystem<SendCreatePlayerRequestSystem>();
world.GetOrCreateSystem<HandlePlayerHeartbeatRequestSystem>();
}
/// <summary>
/// Adds all the systems a server-worker requires for the player lifecycle module.
/// </summary>
/// <param name="world">A world that belongs to a server-worker.</param>
public static void AddServerSystems(World world)
{
world.GetOrCreateSystem<HandleCreatePlayerRequestSystem>();
world.GetOrCreateSystem<PlayerHeartbeatInitializationSystem>();
world.GetOrCreateSystem<SendPlayerHeartbeatRequestSystem>();
world.GetOrCreateSystem<HandlePlayerHeartbeatResponseSystem>();
}
}
}