Skip to content
This repository was archived by the owner on Jan 18, 2022. It is now read-only.

Commit 1bd423d

Browse files
author
Jamie Brynes
authored
Add LinkedGameObjectMap API (#1013)
1 parent 4178a98 commit 1bd423d

15 files changed

+247
-15
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
## Unreleased
44

5+
### Added
6+
7+
- Added the `LinkedGameObjectMap` class for finding the `GameObject`(s) linked with a specified `EntityId`. [#1013](https://github.com/spatialos/gdk-for-unity/pull/1013)
8+
- This can be used with the `[Require]` annotation to inject it into your `MonoBehaviours` provided you are using the `GameObjectCreation` feature module. For example: `[Require] private LinkedGameObjectMap gameObjectMap;`
9+
510
### Internal
611

712
- Stopped throwing a `Test Exception` in playground.

workers/unity/Packages/com.improbable.gdk.core/Subscriptions/EntityGameObjectLinker.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class EntityGameObjectLinker
1515
private readonly EntityManager entityManager;
1616
private readonly World world;
1717

18-
private readonly Dictionary<EntityId, List<GameObject>> entityIdToGameObjects =
18+
internal readonly Dictionary<EntityId, List<GameObject>> EntityIdToGameObjects =
1919
new Dictionary<EntityId, List<GameObject>>();
2020

2121
private readonly Dictionary<GameObject, List<RequiredSubscriptionsInjector>> gameObjectToInjectors =
@@ -66,10 +66,10 @@ public void LinkGameObjectToSpatialOSEntity(EntityId entityId, GameObject gameOb
6666
$"GameObject is already linked to the entity with ID {linkedComponent.EntityId}");
6767
}
6868

69-
if (!entityIdToGameObjects.TryGetValue(entityId, out var linkedGameObjects))
69+
if (!EntityIdToGameObjects.TryGetValue(entityId, out var linkedGameObjects))
7070
{
7171
linkedGameObjects = new List<GameObject>();
72-
entityIdToGameObjects.Add(entityId, linkedGameObjects);
72+
EntityIdToGameObjects.Add(entityId, linkedGameObjects);
7373
}
7474

7575
linkedGameObjects.Add(gameObject);
@@ -116,7 +116,7 @@ public void UnlinkGameObjectFromEntity(EntityId entityId, GameObject gameObject)
116116
throw new ArgumentException($"Can not unlink null GameObject from entity {entityId}");
117117
}
118118

119-
if (!entityIdToGameObjects.TryGetValue(entityId, out var gameObjectSet) ||
119+
if (!EntityIdToGameObjects.TryGetValue(entityId, out var gameObjectSet) ||
120120
!gameObjectSet.Contains(gameObject))
121121
{
122122
throw new ArgumentException(
@@ -155,13 +155,13 @@ public void UnlinkGameObjectFromEntity(EntityId entityId, GameObject gameObject)
155155
gameObjectSet.Remove(gameObject);
156156
if (gameObjectSet.Count == 0)
157157
{
158-
entityIdToGameObjects.Remove(entityId);
158+
EntityIdToGameObjects.Remove(entityId);
159159
}
160160
}
161161

162162
public void UnlinkAllGameObjects()
163163
{
164-
var ids = entityIdToGameObjects.Keys.ToArray();
164+
var ids = EntityIdToGameObjects.Keys.ToArray();
165165
foreach (var id in ids)
166166
{
167167
UnlinkAllGameObjectsFromEntityId(id);
@@ -170,7 +170,7 @@ public void UnlinkAllGameObjects()
170170

171171
public void UnlinkAllGameObjectsFromEntityId(EntityId entityId)
172172
{
173-
if (!entityIdToGameObjects.TryGetValue(entityId, out var gameObjectSet))
173+
if (!EntityIdToGameObjects.TryGetValue(entityId, out var gameObjectSet))
174174
{
175175
return;
176176
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System.Collections.Generic;
2+
using Improbable.Gdk.Core;
3+
using UnityEngine;
4+
5+
namespace Improbable.Gdk.Subscriptions
6+
{
7+
/// <summary>
8+
/// Represents the mapping between SpatialOS entity IDs and linked GameObjects.
9+
/// </summary>
10+
public class LinkedGameObjectMap
11+
{
12+
private readonly EntityGameObjectLinker linker;
13+
14+
/// <summary>
15+
/// Initializes a new instance of the <see cref="LinkedGameObjectMap"/> class backed with the data from
16+
/// the specified <see cref="EntityGameObjectLinker"/>.
17+
/// </summary>
18+
/// <param name="linker">The linker which contains the backing data for this map.</param>
19+
public LinkedGameObjectMap(EntityGameObjectLinker linker)
20+
{
21+
this.linker = linker;
22+
}
23+
24+
/// <summary>
25+
/// Gets the GameObjects that are linked to a given SpatialOS entity ID.
26+
/// </summary>
27+
/// <param name="entityId">The entity ID to get GameObjects for.</param>
28+
/// <returns>A readonly list of the linked GameObjects or null if there are none linked.</returns>
29+
public IReadOnlyList<GameObject> GetLinkedGameObjects(EntityId entityId)
30+
{
31+
return linker.EntityIdToGameObjects.TryGetValue(entityId, out var goList) ? goList.AsReadOnly() : null;
32+
}
33+
34+
/// <summary>
35+
/// Tries to get the GameObjects that are linked to a given SpatialOS entity ID.
36+
/// </summary>
37+
/// <param name="entityId">The entity ID to get GameObjects for.</param>
38+
/// <param name="linkedGameObjects">
39+
/// When this method returns, contains the GameObjects linked to the specified <see cref="EntityId"/>,
40+
/// if any are linked; otherwise, null. This parameter is passed uninitialized.
41+
/// </param>
42+
/// <returns>True, if there are any GameObjects linked to the <see cref="EntityId"/>; otherwise false</returns>
43+
public bool TryGetLinkedGameObjects(EntityId entityId, out IReadOnlyList<GameObject> linkedGameObjects)
44+
{
45+
linkedGameObjects = GetLinkedGameObjects(entityId);
46+
return linkedGameObjects != null;
47+
}
48+
}
49+
}

workers/unity/Packages/com.improbable.gdk.core/Subscriptions/LinkedGameObjectMap.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

workers/unity/Packages/com.improbable.gdk.gameobjectcreation/GameObjectInitializationSystem.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ namespace Improbable.Gdk.GameObjectCreation
1414
[UpdateInGroup(typeof(GameObjectInitializationGroup))]
1515
internal class GameObjectInitializationSystem : ComponentSystem
1616
{
17+
internal EntityGameObjectLinker Linker;
18+
1719
private readonly IEntityGameObjectCreator gameObjectCreator;
1820

1921
private readonly GameObject workerGameObject;
2022

21-
private EntityGameObjectLinker linker;
22-
2323
private EntitySystem entitySystem;
2424
private WorkerSystem workerSystem;
2525

@@ -36,17 +36,17 @@ protected override void OnCreate()
3636
entitySystem = World.GetExistingSystem<EntitySystem>();
3737
workerSystem = World.GetExistingSystem<WorkerSystem>();
3838

39-
linker = new EntityGameObjectLinker(World);
39+
Linker = new EntityGameObjectLinker(World);
4040

4141
if (workerGameObject != null)
4242
{
43-
linker.LinkGameObjectToSpatialOSEntity(new EntityId(0), workerGameObject);
43+
Linker.LinkGameObjectToSpatialOSEntity(new EntityId(0), workerGameObject);
4444
}
4545
}
4646

4747
protected override void OnDestroy()
4848
{
49-
linker.UnlinkAllGameObjects();
49+
Linker.UnlinkAllGameObjects();
5050

5151
foreach (var entityId in entitySystem.GetEntitiesInView())
5252
{
@@ -61,16 +61,16 @@ protected override void OnUpdate()
6161
foreach (var entityId in entitySystem.GetEntitiesAdded())
6262
{
6363
workerSystem.TryGetEntity(entityId, out var entity);
64-
gameObjectCreator.OnEntityCreated(new SpatialOSEntity(entity, EntityManager), linker);
64+
gameObjectCreator.OnEntityCreated(new SpatialOSEntity(entity, EntityManager), Linker);
6565
}
6666

6767
var removedEntities = entitySystem.GetEntitiesRemoved();
6868
foreach (var entityId in removedEntities)
6969
{
70-
linker.UnlinkAllGameObjectsFromEntityId(entityId);
70+
Linker.UnlinkAllGameObjectsFromEntityId(entityId);
7171
}
7272

73-
linker.FlushCommandBuffer();
73+
Linker.FlushCommandBuffer();
7474

7575
foreach (var entityId in removedEntities)
7676
{
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using Improbable.Gdk.Core;
2+
using Improbable.Gdk.Subscriptions;
3+
using Unity.Entities;
4+
5+
namespace Improbable.Gdk.GameObjectCreation
6+
{
7+
[AutoRegisterSubscriptionManager]
8+
internal class LinkedGameObjectMapSubscriptionManager : SubscriptionManager<LinkedGameObjectMap>
9+
{
10+
private readonly World world;
11+
private Subscription<LinkedGameObjectMap> linkedGameObjectMapSubscription;
12+
13+
public LinkedGameObjectMapSubscriptionManager(World world)
14+
{
15+
this.world = world;
16+
}
17+
18+
public override Subscription<LinkedGameObjectMap> Subscribe(EntityId entityId)
19+
{
20+
if (linkedGameObjectMapSubscription == null)
21+
{
22+
linkedGameObjectMapSubscription = new Subscription<LinkedGameObjectMap>(this, new EntityId(0));
23+
24+
var goSystem = world.GetExistingSystem<GameObjectInitializationSystem>();
25+
if (goSystem != null)
26+
{
27+
linkedGameObjectMapSubscription.SetAvailable(new LinkedGameObjectMap(goSystem.Linker));
28+
}
29+
}
30+
31+
return linkedGameObjectMapSubscription;
32+
}
33+
34+
public override void Cancel(ISubscription subscription)
35+
{
36+
}
37+
38+
public override void ResetValue(ISubscription subscription)
39+
{
40+
}
41+
}
42+
}

workers/unity/Packages/com.improbable.gdk.gameobjectcreation/LinkedGameObjectMapSubscriptionManager.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

workers/unity/Packages/com.improbable.gdk.gameobjectcreation/Tests.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

workers/unity/Packages/com.improbable.gdk.gameobjectcreation/Tests/Editmode.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "Improbable.Gdk.GameObjectCreation.EditmodeTests",
3+
"references": [
4+
"GUID:0603bcb6cd766ea40a053a38fff8a84b",
5+
"GUID:edb3612c44ad0d24988d387581fd5fbe",
6+
"GUID:6e87be7cacbd7264c9a5b9efc59a4030",
7+
"GUID:734d92eba21c94caba915361bd5ac177"
8+
],
9+
"optionalUnityReferences": [
10+
"TestAssemblies"
11+
],
12+
"includePlatforms": [
13+
"Editor"
14+
],
15+
"excludePlatforms": [],
16+
"allowUnsafeCode": false,
17+
"overrideReferences": false,
18+
"precompiledReferences": [],
19+
"autoReferenced": true,
20+
"defineConstraints": [],
21+
"versionDefines": []
22+
}

0 commit comments

Comments
 (0)