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

Commit b03b212

Browse files
author
Paul Balaji
authored
QBI query component filtering (#1338)
1 parent 15d0fcf commit b03b212

File tree

19 files changed

+254
-204
lines changed

19 files changed

+254
-204
lines changed

CHANGELOG.md

+13
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,19 @@
1111
- No need to filter your ECS query anymore, instead match for `HasAuthority`.
1212
- Built-in Unity AssetBundle module is now required.
1313

14+
## Added
15+
16+
- Added new methods to `Snapshot` utility class. [#1338](https://github.com/spatialos/gdk-for-unity/pull/1338)
17+
- `GetNextEntityId()` returns the next available entity ID.
18+
- `AddEntity(EntityId entityId, EntityTemplate entityTemplate)` adds an entity to the snapshot with a given entity ID.
19+
- Added an additional `AddComponent` method to the `EntityTemplate` class which does not require write-access to be given. [#1338](https://github.com/spatialos/gdk-for-unity/pull/1338)
20+
- This allows users to add undelegated components on entities.
21+
22+
## Internal
23+
24+
- Added component result type filters to playground QBI queries. [#1338](https://github.com/spatialos/gdk-for-unity/pull/1338)
25+
- Replaced `InitUISystem` with the `InitUIBehaviour` script on the `Character` prefab. [#1338](https://github.com/spatialos/gdk-for-unity/pull/1338)
26+
1427
## `0.3.4` - 2020-03-16
1528

1629
### Breaking Changes

snapshots/default.snapshot

-28 Bytes
Binary file not shown.

workers/unity/Assets/Playground/Config/CubeTemplate.cs

-33
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
using System.Collections.Generic;
2+
using Improbable;
3+
using Improbable.Gdk.Core;
4+
using Improbable.Gdk.PlayerLifecycle;
5+
using Improbable.Gdk.QueryBasedInterest;
6+
using Improbable.Gdk.TransformSynchronization;
7+
using UnityEngine;
8+
9+
namespace Playground
10+
{
11+
public static class EntityTemplates
12+
{
13+
private const int CheckoutRadius = 25;
14+
15+
public static EntityTemplate CreatePlayerEntityTemplate(EntityId entityId, string clientWorkerId, byte[] playerCreationArguments)
16+
{
17+
var clientAttribute = EntityTemplate.GetWorkerAccessAttribute(clientWorkerId);
18+
19+
var template = new EntityTemplate();
20+
21+
template.AddComponent(new Position.Snapshot(), clientAttribute);
22+
template.AddComponent(new Metadata.Snapshot("Character"), WorkerUtils.UnityGameLogic);
23+
template.AddComponent(new PlayerInput.Snapshot(), clientAttribute);
24+
template.AddComponent(new Launcher.Snapshot(100, 0), WorkerUtils.UnityGameLogic);
25+
template.AddComponent(new Score.Snapshot(), WorkerUtils.UnityGameLogic);
26+
template.AddComponent(new CubeSpawner.Snapshot(new List<EntityId>()), WorkerUtils.UnityGameLogic);
27+
28+
TransformSynchronizationHelper.AddTransformSynchronizationComponents(template, clientAttribute);
29+
PlayerLifecycleHelper.AddPlayerLifecycleComponents(template, clientWorkerId, WorkerUtils.UnityGameLogic);
30+
31+
var clientSelfInterest = InterestQuery.Query(Constraint.EntityId(entityId)).FilterResults(new[]
32+
{
33+
Position.ComponentId, Metadata.ComponentId, TransformInternal.ComponentId, CubeSpawner.ComponentId,
34+
Score.ComponentId, Launcher.ComponentId
35+
});
36+
37+
var clientRangeInterest = InterestQuery.Query(Constraint.RelativeCylinder(radius: CheckoutRadius))
38+
.FilterResults(new[]
39+
{
40+
Position.ComponentId, Metadata.ComponentId, TransformInternal.ComponentId, Collisions.ComponentId,
41+
SpinnerColor.ComponentId, SpinnerRotation.ComponentId, CubeColor.ComponentId, Score.ComponentId,
42+
Launchable.ComponentId
43+
});
44+
45+
var serverSelfInterest = InterestQuery.Query(Constraint.EntityId(entityId)).FilterResults(new[]
46+
{
47+
Position.ComponentId, Metadata.ComponentId, TransformInternal.ComponentId, Score.ComponentId
48+
});
49+
50+
var serverRangeInterest = InterestQuery.Query(Constraint.RelativeCylinder(radius: CheckoutRadius))
51+
.FilterResults(new[]
52+
{
53+
Position.ComponentId, Metadata.ComponentId, TransformInternal.ComponentId, Collisions.ComponentId,
54+
SpinnerColor.ComponentId, SpinnerRotation.ComponentId, Score.ComponentId
55+
});
56+
57+
var interest = InterestTemplate.Create()
58+
.AddQueries<Position.Component>(clientSelfInterest, clientRangeInterest)
59+
.AddQueries<Metadata.Component>(serverSelfInterest, serverRangeInterest);
60+
template.AddComponent(interest.ToSnapshot());
61+
62+
template.SetReadAccess(WorkerUtils.MobileClient, WorkerUtils.UnityClient, WorkerUtils.UnityGameLogic);
63+
64+
return template;
65+
}
66+
67+
public static EntityTemplate CreateCubeEntityTemplate(Vector3 location)
68+
{
69+
var template = new EntityTemplate();
70+
template.AddComponent(new Position.Snapshot(location.ToCoordinates()), WorkerUtils.UnityGameLogic);
71+
template.AddComponent(new Metadata.Snapshot("Cube"), WorkerUtils.UnityGameLogic);
72+
template.AddComponent(new Persistence.Snapshot(), WorkerUtils.UnityGameLogic);
73+
template.AddComponent(new CubeColor.Snapshot(), WorkerUtils.UnityGameLogic);
74+
template.AddComponent(new CubeTargetVelocity.Snapshot(new Vector3f(-2.0f, 0, 0)),
75+
WorkerUtils.UnityGameLogic);
76+
template.AddComponent(new Launchable.Snapshot(), WorkerUtils.UnityGameLogic);
77+
78+
TransformSynchronizationHelper.AddTransformSynchronizationComponents(template, WorkerUtils.UnityGameLogic, Quaternion.identity, location);
79+
80+
var query = InterestQuery.Query(Constraint.RelativeCylinder(radius: CheckoutRadius)).FilterResults(new[]
81+
{
82+
Position.ComponentId, Metadata.ComponentId, TransformInternal.ComponentId
83+
});
84+
85+
var interest = InterestTemplate.Create()
86+
.AddQueries<Position.Component>(query);
87+
template.AddComponent(interest.ToSnapshot());
88+
89+
template.SetReadAccess(WorkerUtils.MobileClient, WorkerUtils.UnityClient, WorkerUtils.UnityGameLogic);
90+
91+
return template;
92+
}
93+
94+
public static EntityTemplate CreateSpinnerEntityTemplate(Coordinates coords)
95+
{
96+
var transform = TransformUtils.CreateTransformSnapshot(coords.ToUnityVector(), Quaternion.identity);
97+
98+
var template = new EntityTemplate();
99+
template.AddComponent(new Position.Snapshot(coords), WorkerUtils.UnityGameLogic);
100+
template.AddComponent(new Metadata.Snapshot("Spinner"), WorkerUtils.UnityGameLogic);
101+
template.AddComponent(transform, WorkerUtils.UnityGameLogic);
102+
template.AddComponent(new Persistence.Snapshot(), WorkerUtils.UnityGameLogic);
103+
template.AddComponent(new Collisions.Snapshot(), WorkerUtils.UnityGameLogic);
104+
template.AddComponent(new SpinnerColor.Snapshot(Color.BLUE), WorkerUtils.UnityGameLogic);
105+
template.AddComponent(new SpinnerRotation.Snapshot(), WorkerUtils.UnityGameLogic);
106+
107+
var query = InterestQuery.Query(Constraint.RelativeCylinder(radius: CheckoutRadius)).FilterResults(new[]
108+
{
109+
Position.ComponentId, Metadata.ComponentId, TransformInternal.ComponentId
110+
});
111+
112+
var interest = InterestTemplate.Create()
113+
.AddQueries<Position.Component>(query);
114+
template.AddComponent(interest.ToSnapshot());
115+
116+
template.SetReadAccess(WorkerUtils.MobileClient, WorkerUtils.UnityClient, WorkerUtils.UnityGameLogic);
117+
118+
return template;
119+
}
120+
121+
public static EntityTemplate CreatePlayerSpawnerEntityTemplate(Coordinates playerSpawnerLocation)
122+
{
123+
var template = new EntityTemplate();
124+
template.AddComponent(new Position.Snapshot(playerSpawnerLocation), WorkerUtils.UnityGameLogic);
125+
template.AddComponent(new Metadata.Snapshot("PlayerCreator"), WorkerUtils.UnityGameLogic);
126+
template.AddComponent(new Persistence.Snapshot(), WorkerUtils.UnityGameLogic);
127+
template.AddComponent(new PlayerCreator.Snapshot(), WorkerUtils.UnityGameLogic);
128+
129+
return template;
130+
}
131+
}
132+
}

workers/unity/Assets/Playground/Scripts/UI/InitUISystem.cs.meta renamed to workers/unity/Assets/Playground/Config/EntityTemplates.cs.meta

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

workers/unity/Assets/Playground/Config/PlayerTemplate.cs

-41
This file was deleted.

workers/unity/Assets/Playground/Config/PlayerTemplate.cs.meta

-11
This file was deleted.

workers/unity/Assets/Playground/Config/WorkerUtils.cs

-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ public static void AddClientSystems(World world)
2323
world.GetOrCreateSystem<MoveLocalPlayerSystem>();
2424
world.GetOrCreateSystem<InitCameraSystem>();
2525
world.GetOrCreateSystem<FollowCameraSystem>();
26-
world.GetOrCreateSystem<InitUISystem>();
2726
world.GetOrCreateSystem<UpdateUISystem>();
2827
world.GetOrCreateSystem<PlayerCommandsSystem>();
2928
world.GetOrCreateSystem<MetricSendSystem>();

workers/unity/Assets/Playground/Editor/SnapshotGenerator/SnapshotGenerator.cs

+8-41
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ private static Snapshot CreateSnapshot(int cubeCount)
3535
AddPlayerSpawner(snapshot, new Coordinates(-200, 0, 200));
3636

3737
AddCubeGrid(snapshot, cubeCount);
38+
3839
CreateSpinner(snapshot, new Coordinates { X = 5.5, Y = 0.5f, Z = 0.0 });
3940
CreateSpinner(snapshot, new Coordinates { X = -5.5, Y = 0.5f, Z = 0.0 });
4041

@@ -43,22 +44,18 @@ private static Snapshot CreateSnapshot(int cubeCount)
4344

4445
private static void AddPlayerSpawner(Snapshot snapshot, Coordinates playerSpawnerLocation)
4546
{
46-
var template = new EntityTemplate();
47-
template.AddComponent(new Position.Snapshot(playerSpawnerLocation), WorkerUtils.UnityGameLogic);
48-
template.AddComponent(new Metadata.Snapshot { EntityType = "PlayerCreator" }, WorkerUtils.UnityGameLogic);
49-
template.AddComponent(new Persistence.Snapshot(), WorkerUtils.UnityGameLogic);
50-
template.AddComponent(new PlayerCreator.Snapshot(), WorkerUtils.UnityGameLogic);
51-
52-
template.SetReadAccess(WorkerUtils.UnityGameLogic, WorkerUtils.UnityClient, WorkerUtils.MobileClient);
53-
template.SetComponentWriteAccess(EntityAcl.ComponentId, WorkerUtils.UnityGameLogic);
47+
var template = EntityTemplates.CreatePlayerSpawnerEntityTemplate(playerSpawnerLocation);
48+
snapshot.AddEntity(template);
49+
}
5450

51+
private static void CreateSpinner(Snapshot snapshot, Coordinates coords)
52+
{
53+
var template = EntityTemplates.CreateSpinnerEntityTemplate(coords);
5554
snapshot.AddEntity(template);
5655
}
5756

5857
private static void AddCubeGrid(Snapshot snapshot, int cubeCount)
5958
{
60-
var cubeTemplate = CubeTemplate.CreateCubeEntityTemplate();
61-
6259
// Calculate grid size
6360
var gridLength = (int) Math.Ceiling(Math.Sqrt(cubeCount));
6461
if (gridLength % 2 == 1) // To make sure nothing is in (0, 0)
@@ -85,40 +82,10 @@ private static void AddCubeGrid(Snapshot snapshot, int cubeCount)
8582
}
8683

8784
var location = new Vector3(x, 1, z);
88-
var positionSnapshot = new Position.Snapshot(location.ToCoordinates());
89-
var transformSnapshot = TransformUtils.CreateTransformSnapshot(location, Quaternion.identity);
90-
91-
cubeTemplate.SetComponent(positionSnapshot);
92-
cubeTemplate.SetComponent(transformSnapshot);
85+
var cubeTemplate = EntityTemplates.CreateCubeEntityTemplate(location);
9386
snapshot.AddEntity(cubeTemplate);
9487
}
9588
}
9689
}
97-
98-
private static void CreateSpinner(Snapshot snapshot, Coordinates coords)
99-
{
100-
const string entityType = "Spinner";
101-
102-
var transform = TransformUtils.CreateTransformSnapshot(coords.ToUnityVector(), Quaternion.identity);
103-
104-
var template = new EntityTemplate();
105-
template.AddComponent(new Position.Snapshot(coords), WorkerUtils.UnityGameLogic);
106-
template.AddComponent(new Metadata.Snapshot(entityType), WorkerUtils.UnityGameLogic);
107-
template.AddComponent(transform, WorkerUtils.UnityGameLogic);
108-
template.AddComponent(new Persistence.Snapshot(), WorkerUtils.UnityGameLogic);
109-
template.AddComponent(new Collisions.Snapshot(), WorkerUtils.UnityGameLogic);
110-
template.AddComponent(new SpinnerColor.Snapshot(Color.BLUE), WorkerUtils.UnityGameLogic);
111-
template.AddComponent(new SpinnerRotation.Snapshot(), WorkerUtils.UnityGameLogic);
112-
113-
var query = InterestQuery.Query(Constraint.RelativeSphere(radius: 25));
114-
var interest = InterestTemplate.Create()
115-
.AddQueries<Position.Component>(query);
116-
template.AddComponent(interest.ToSnapshot(), WorkerUtils.UnityGameLogic);
117-
118-
template.SetReadAccess(WorkerUtils.UnityGameLogic, WorkerUtils.UnityClient, WorkerUtils.MobileClient);
119-
template.SetComponentWriteAccess(EntityAcl.ComponentId, WorkerUtils.UnityGameLogic);
120-
121-
snapshot.AddEntity(template);
122-
}
12390
}
12491
}

workers/unity/Assets/Playground/Resources/Prefabs/Common/Character.prefab

+14
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ GameObject:
1717
- component: {fileID: 114602415406350378}
1818
- component: {fileID: 2643701918418317171}
1919
- component: {fileID: -663445870753195477}
20+
- component: {fileID: 8124006586705730658}
2021
m_Layer: 9
2122
m_Name: Character
2223
m_TagString: Untagged
@@ -149,6 +150,19 @@ MonoBehaviour:
149150
m_Script: {fileID: 11500000, guid: 5053c3754e6a48118a61672cedc67dec, type: 3}
150151
m_Name:
151152
m_EditorClassIdentifier:
153+
--- !u!114 &8124006586705730658
154+
MonoBehaviour:
155+
m_ObjectHideFlags: 0
156+
m_CorrespondingSourceObject: {fileID: 0}
157+
m_PrefabInstance: {fileID: 0}
158+
m_PrefabAsset: {fileID: 0}
159+
m_GameObject: {fileID: 1292271339760838}
160+
m_Enabled: 0
161+
m_EditorHideFlags: 0
162+
m_Script: {fileID: 11500000, guid: 682c3f80479014542ab23d20341b0d2e, type: 3}
163+
m_Name:
164+
m_EditorClassIdentifier:
165+
uiPrefab: {fileID: 1582526976165782, guid: 0adfaf6f7e2705544a15cf9e489deb92, type: 3}
152166
--- !u!1 &1494660013534148
153167
GameObject:
154168
m_ObjectHideFlags: 0

workers/unity/Assets/Playground/Scripts/MonoBehaviours/SpawnCubeCommandReceiver.cs

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
using System.Collections.Generic;
2-
using System.Threading.Tasks;
3-
using Improbable;
42
using Improbable.Gdk.Core;
53
using Improbable.Gdk.Core.Commands;
64
using Improbable.Gdk.Subscriptions;
@@ -46,10 +44,7 @@ private void SpawnCube(EntityId entityId)
4644
var location = gameObject.transform.position - offset;
4745
location.y += 2;
4846

49-
var cubeEntityTemplate = CubeTemplate.CreateCubeEntityTemplate();
50-
51-
cubeEntityTemplate.SetComponent(new Position.Snapshot(location.ToCoordinates()));
52-
cubeEntityTemplate.SetComponent(TransformUtils.CreateTransformSnapshot(location, Quaternion.identity));
47+
var cubeEntityTemplate = EntityTemplates.CreateCubeEntityTemplate(location);
5348

5449
worldCommandRequestSender.SendCreateEntityCommand(
5550
new WorldCommands.CreateEntity.Request(cubeEntityTemplate, entityId), OnEntityCreated);

workers/unity/Assets/Playground/Scripts/OneTimeInitialisation.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ private static void Init()
1818
initialized = true;
1919

2020
// Setup template to use for player on connecting client
21-
PlayerLifecycleConfig.CreatePlayerEntityTemplate = PlayerTemplate.CreatePlayerEntityTemplate;
21+
PlayerLifecycleConfig.CreatePlayerEntityTemplate = EntityTemplates.CreatePlayerEntityTemplate;
2222
}
2323
}
2424
}

0 commit comments

Comments
 (0)