|
6 | 6 | using Improbable.Gdk.Movement; |
7 | 7 | using Improbable.Gdk.StandardTypes; |
8 | 8 | using Improbable.Gdk.Subscriptions; |
9 | | -using Unity.Entities; |
10 | 9 | using UnityEngine; |
11 | 10 | using Object = UnityEngine.Object; |
12 | 11 |
|
13 | | -public class AdvancedEntityPipeline : IEntityGameObjectCreator |
| 12 | +namespace Fps |
14 | 13 | { |
15 | | - private const string GameobjectNameFormat = "{0}(SpatialOS {1}, Worker: {2})"; |
16 | | - private const string WorkerAttributeFormat = "workerId:{0}"; |
17 | | - private const string PlayerMetadata = "Player"; |
| 14 | + public class AdvancedEntityPipeline : IEntityGameObjectCreator |
| 15 | + { |
| 16 | + private const string GameobjectNameFormat = "{0}(SpatialOS {1}, Worker: {2})"; |
| 17 | + private const string WorkerAttributeFormat = "workerId:{0}"; |
| 18 | + private const string PlayerMetadata = "Player"; |
18 | 19 |
|
19 | | - private readonly GameObject cachedAuthPlayer; |
20 | | - private readonly GameObject cachedNonAuthPlayer; |
| 20 | + private readonly GameObject cachedAuthPlayer; |
| 21 | + private readonly GameObject cachedNonAuthPlayer; |
21 | 22 |
|
22 | | - private readonly IEntityGameObjectCreator fallback; |
23 | | - private readonly string workerIdAttribute; |
24 | | - private readonly Worker worker; |
| 23 | + private readonly IEntityGameObjectCreator fallback; |
| 24 | + private readonly string workerIdAttribute; |
| 25 | + private readonly Worker worker; |
25 | 26 |
|
26 | | - private readonly Dictionary<EntityId, GameObject> gameObjectsCreated = new Dictionary<EntityId, GameObject>(); |
| 27 | + private readonly Dictionary<EntityId, GameObject> gameObjectsCreated = new Dictionary<EntityId, GameObject>(); |
27 | 28 |
|
28 | | - private readonly Type[] componentsToAdd = |
29 | | - { |
30 | | - typeof(Transform), |
31 | | - typeof(Rigidbody) |
32 | | - }; |
| 29 | + public event Action OnRemovedAuthoritativePlayer; |
33 | 30 |
|
34 | | - public AdvancedEntityPipeline(Worker worker, string authPlayer, string nonAuthPlayer, |
35 | | - IEntityGameObjectCreator fallback) |
36 | | - { |
37 | | - this.worker = worker; |
38 | | - this.fallback = fallback; |
39 | | - workerIdAttribute = string.Format(WorkerAttributeFormat, worker.WorkerId); |
40 | | - cachedAuthPlayer = Resources.Load<GameObject>(authPlayer); |
41 | | - cachedNonAuthPlayer = Resources.Load<GameObject>(nonAuthPlayer); |
42 | | - } |
| 31 | + private readonly Type[] componentsToAdd = |
| 32 | + { |
| 33 | + typeof(Transform), |
| 34 | + typeof(Rigidbody) |
| 35 | + }; |
43 | 36 |
|
44 | | - public void OnEntityCreated(SpatialOSEntity entity, EntityGameObjectLinker linker) |
45 | | - { |
46 | | - if (!entity.HasComponent<Metadata.Component>()) |
| 37 | + public AdvancedEntityPipeline(Worker worker, string authPlayer, string nonAuthPlayer, |
| 38 | + IEntityGameObjectCreator fallback) |
47 | 39 | { |
48 | | - return; |
| 40 | + this.worker = worker; |
| 41 | + this.fallback = fallback; |
| 42 | + workerIdAttribute = EntityTemplate.GetWorkerAccessAttribute(worker.WorkerId); |
| 43 | + cachedAuthPlayer = Resources.Load<GameObject>(authPlayer); |
| 44 | + cachedNonAuthPlayer = Resources.Load<GameObject>(nonAuthPlayer); |
49 | 45 | } |
50 | 46 |
|
51 | | - var prefabName = entity.GetComponent<Metadata.Component>().EntityType; |
52 | | - if (prefabName.Equals(PlayerMetadata)) |
| 47 | + public void OnEntityCreated(SpatialOSEntity entity, EntityGameObjectLinker linker) |
53 | 48 | { |
54 | | - var clientMovement = entity.GetComponent<ClientMovement.Component>(); |
55 | | - if (entity.GetComponent<EntityAcl.Component>().ComponentWriteAcl |
56 | | - .TryGetValue(clientMovement.ComponentId, out var clientMovementWrite)) |
| 49 | + if (!entity.HasComponent<Metadata.Component>()) |
57 | 50 | { |
58 | | - var authority = false; |
59 | | - foreach (var attributeSet in clientMovementWrite.AttributeSet) |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + var prefabName = entity.GetComponent<Metadata.Component>().EntityType; |
| 55 | + if (prefabName.Equals(PlayerMetadata)) |
| 56 | + { |
| 57 | + var clientMovement = entity.GetComponent<ClientMovement.Component>(); |
| 58 | + if (entity.GetComponent<EntityAcl.Component>().ComponentWriteAcl |
| 59 | + .TryGetValue(clientMovement.ComponentId, out var clientMovementWrite)) |
60 | 60 | { |
61 | | - if (attributeSet.Attribute.Contains(workerIdAttribute)) |
| 61 | + var authority = false; |
| 62 | + foreach (var attributeSet in clientMovementWrite.AttributeSet) |
62 | 63 | { |
63 | | - authority = true; |
| 64 | + if (attributeSet.Attribute.Contains(workerIdAttribute)) |
| 65 | + { |
| 66 | + authority = true; |
| 67 | + } |
64 | 68 | } |
65 | | - } |
66 | 69 |
|
67 | | - var serverPosition = entity.GetComponent<ServerMovement.Component>(); |
68 | | - var position = serverPosition.Latest.Position.ToVector3() + worker.Origin; |
| 70 | + var serverPosition = entity.GetComponent<ServerMovement.Component>(); |
| 71 | + var position = serverPosition.Latest.Position.ToVector3() + worker.Origin; |
69 | 72 |
|
70 | | - var prefab = authority ? cachedAuthPlayer : cachedNonAuthPlayer; |
71 | | - var gameObject = Object.Instantiate(prefab, position, Quaternion.identity); |
| 73 | + var prefab = authority ? cachedAuthPlayer : cachedNonAuthPlayer; |
| 74 | + var gameObject = Object.Instantiate(prefab, position, Quaternion.identity); |
72 | 75 |
|
73 | | - gameObjectsCreated.Add(entity.SpatialOSEntityId, gameObject); |
74 | | - gameObject.name = GetGameObjectName(prefab, entity, worker); |
75 | | - linker.LinkGameObjectToSpatialOSEntity(entity.SpatialOSEntityId, gameObject, componentsToAdd); |
76 | | - return; |
| 76 | + gameObjectsCreated.Add(entity.SpatialOSEntityId, gameObject); |
| 77 | + gameObject.name = GetGameObjectName(prefab, entity, worker); |
| 78 | + linker.LinkGameObjectToSpatialOSEntity(entity.SpatialOSEntityId, gameObject, componentsToAdd); |
| 79 | + return; |
| 80 | + } |
77 | 81 | } |
78 | | - } |
79 | 82 |
|
80 | | - fallback.OnEntityCreated(entity, linker); |
81 | | - } |
82 | | - |
83 | | - private static string GetGameObjectName(GameObject prefab, SpatialOSEntity entity, Worker worker) |
84 | | - { |
85 | | - return string.Format(GameobjectNameFormat, prefab.name, entity.SpatialOSEntityId, worker.WorkerType); |
86 | | - } |
| 83 | + fallback.OnEntityCreated(entity, linker); |
| 84 | + } |
87 | 85 |
|
88 | | - public void OnEntityRemoved(EntityId entityId) |
89 | | - { |
90 | | - if (!gameObjectsCreated.TryGetValue(entityId, out var go)) |
| 86 | + private static string GetGameObjectName(GameObject prefab, SpatialOSEntity entity, Worker worker) |
91 | 87 | { |
92 | | - fallback.OnEntityRemoved(entityId); |
93 | | - return; |
| 88 | + return string.Format(GameobjectNameFormat, prefab.name, entity.SpatialOSEntityId, worker.WorkerType); |
94 | 89 | } |
95 | 90 |
|
96 | | - gameObjectsCreated.Remove(entityId); |
97 | | - Object.Destroy(go); |
| 91 | + public void OnEntityRemoved(EntityId entityId) |
| 92 | + { |
| 93 | + if (!gameObjectsCreated.TryGetValue(entityId, out var go)) |
| 94 | + { |
| 95 | + fallback.OnEntityRemoved(entityId); |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + // Trigger a callback when authoritative player gets removed |
| 100 | + if (go.GetComponent<FpsDriver>() != null) |
| 101 | + { |
| 102 | + OnRemovedAuthoritativePlayer?.Invoke(); |
| 103 | + } |
| 104 | + |
| 105 | + gameObjectsCreated.Remove(entityId); |
| 106 | + Object.Destroy(go); |
| 107 | + } |
98 | 108 | } |
99 | 109 | } |
0 commit comments