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

Commit

Permalink
QBI query component filtering (#1338)
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Balaji authored Apr 3, 2020
1 parent 15d0fcf commit b03b212
Show file tree
Hide file tree
Showing 19 changed files with 254 additions and 204 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@
- No need to filter your ECS query anymore, instead match for `HasAuthority`.
- Built-in Unity AssetBundle module is now required.

## Added

- Added new methods to `Snapshot` utility class. [#1338](https://github.com/spatialos/gdk-for-unity/pull/1338)
- `GetNextEntityId()` returns the next available entity ID.
- `AddEntity(EntityId entityId, EntityTemplate entityTemplate)` adds an entity to the snapshot with a given entity ID.
- 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)
- This allows users to add undelegated components on entities.

## Internal

- Added component result type filters to playground QBI queries. [#1338](https://github.com/spatialos/gdk-for-unity/pull/1338)
- Replaced `InitUISystem` with the `InitUIBehaviour` script on the `Character` prefab. [#1338](https://github.com/spatialos/gdk-for-unity/pull/1338)

## `0.3.4` - 2020-03-16

### Breaking Changes
Expand Down
Binary file modified snapshots/default.snapshot
Binary file not shown.
33 changes: 0 additions & 33 deletions workers/unity/Assets/Playground/Config/CubeTemplate.cs

This file was deleted.

132 changes: 132 additions & 0 deletions workers/unity/Assets/Playground/Config/EntityTemplates.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
using System.Collections.Generic;
using Improbable;
using Improbable.Gdk.Core;
using Improbable.Gdk.PlayerLifecycle;
using Improbable.Gdk.QueryBasedInterest;
using Improbable.Gdk.TransformSynchronization;
using UnityEngine;

namespace Playground
{
public static class EntityTemplates
{
private const int CheckoutRadius = 25;

public static EntityTemplate CreatePlayerEntityTemplate(EntityId entityId, string clientWorkerId, byte[] playerCreationArguments)
{
var clientAttribute = EntityTemplate.GetWorkerAccessAttribute(clientWorkerId);

var template = new EntityTemplate();

template.AddComponent(new Position.Snapshot(), clientAttribute);
template.AddComponent(new Metadata.Snapshot("Character"), WorkerUtils.UnityGameLogic);
template.AddComponent(new PlayerInput.Snapshot(), clientAttribute);
template.AddComponent(new Launcher.Snapshot(100, 0), WorkerUtils.UnityGameLogic);
template.AddComponent(new Score.Snapshot(), WorkerUtils.UnityGameLogic);
template.AddComponent(new CubeSpawner.Snapshot(new List<EntityId>()), WorkerUtils.UnityGameLogic);

TransformSynchronizationHelper.AddTransformSynchronizationComponents(template, clientAttribute);
PlayerLifecycleHelper.AddPlayerLifecycleComponents(template, clientWorkerId, WorkerUtils.UnityGameLogic);

var clientSelfInterest = InterestQuery.Query(Constraint.EntityId(entityId)).FilterResults(new[]
{
Position.ComponentId, Metadata.ComponentId, TransformInternal.ComponentId, CubeSpawner.ComponentId,
Score.ComponentId, Launcher.ComponentId
});

var clientRangeInterest = InterestQuery.Query(Constraint.RelativeCylinder(radius: CheckoutRadius))
.FilterResults(new[]
{
Position.ComponentId, Metadata.ComponentId, TransformInternal.ComponentId, Collisions.ComponentId,
SpinnerColor.ComponentId, SpinnerRotation.ComponentId, CubeColor.ComponentId, Score.ComponentId,
Launchable.ComponentId
});

var serverSelfInterest = InterestQuery.Query(Constraint.EntityId(entityId)).FilterResults(new[]
{
Position.ComponentId, Metadata.ComponentId, TransformInternal.ComponentId, Score.ComponentId
});

var serverRangeInterest = InterestQuery.Query(Constraint.RelativeCylinder(radius: CheckoutRadius))
.FilterResults(new[]
{
Position.ComponentId, Metadata.ComponentId, TransformInternal.ComponentId, Collisions.ComponentId,
SpinnerColor.ComponentId, SpinnerRotation.ComponentId, Score.ComponentId
});

var interest = InterestTemplate.Create()
.AddQueries<Position.Component>(clientSelfInterest, clientRangeInterest)
.AddQueries<Metadata.Component>(serverSelfInterest, serverRangeInterest);
template.AddComponent(interest.ToSnapshot());

template.SetReadAccess(WorkerUtils.MobileClient, WorkerUtils.UnityClient, WorkerUtils.UnityGameLogic);

return template;
}

public static EntityTemplate CreateCubeEntityTemplate(Vector3 location)
{
var template = new EntityTemplate();
template.AddComponent(new Position.Snapshot(location.ToCoordinates()), WorkerUtils.UnityGameLogic);
template.AddComponent(new Metadata.Snapshot("Cube"), WorkerUtils.UnityGameLogic);
template.AddComponent(new Persistence.Snapshot(), WorkerUtils.UnityGameLogic);
template.AddComponent(new CubeColor.Snapshot(), WorkerUtils.UnityGameLogic);
template.AddComponent(new CubeTargetVelocity.Snapshot(new Vector3f(-2.0f, 0, 0)),
WorkerUtils.UnityGameLogic);
template.AddComponent(new Launchable.Snapshot(), WorkerUtils.UnityGameLogic);

TransformSynchronizationHelper.AddTransformSynchronizationComponents(template, WorkerUtils.UnityGameLogic, Quaternion.identity, location);

var query = InterestQuery.Query(Constraint.RelativeCylinder(radius: CheckoutRadius)).FilterResults(new[]
{
Position.ComponentId, Metadata.ComponentId, TransformInternal.ComponentId
});

var interest = InterestTemplate.Create()
.AddQueries<Position.Component>(query);
template.AddComponent(interest.ToSnapshot());

template.SetReadAccess(WorkerUtils.MobileClient, WorkerUtils.UnityClient, WorkerUtils.UnityGameLogic);

return template;
}

public static EntityTemplate CreateSpinnerEntityTemplate(Coordinates coords)
{
var transform = TransformUtils.CreateTransformSnapshot(coords.ToUnityVector(), Quaternion.identity);

var template = new EntityTemplate();
template.AddComponent(new Position.Snapshot(coords), WorkerUtils.UnityGameLogic);
template.AddComponent(new Metadata.Snapshot("Spinner"), WorkerUtils.UnityGameLogic);
template.AddComponent(transform, WorkerUtils.UnityGameLogic);
template.AddComponent(new Persistence.Snapshot(), WorkerUtils.UnityGameLogic);
template.AddComponent(new Collisions.Snapshot(), WorkerUtils.UnityGameLogic);
template.AddComponent(new SpinnerColor.Snapshot(Color.BLUE), WorkerUtils.UnityGameLogic);
template.AddComponent(new SpinnerRotation.Snapshot(), WorkerUtils.UnityGameLogic);

var query = InterestQuery.Query(Constraint.RelativeCylinder(radius: CheckoutRadius)).FilterResults(new[]
{
Position.ComponentId, Metadata.ComponentId, TransformInternal.ComponentId
});

var interest = InterestTemplate.Create()
.AddQueries<Position.Component>(query);
template.AddComponent(interest.ToSnapshot());

template.SetReadAccess(WorkerUtils.MobileClient, WorkerUtils.UnityClient, WorkerUtils.UnityGameLogic);

return template;
}

public static EntityTemplate CreatePlayerSpawnerEntityTemplate(Coordinates playerSpawnerLocation)
{
var template = new EntityTemplate();
template.AddComponent(new Position.Snapshot(playerSpawnerLocation), WorkerUtils.UnityGameLogic);
template.AddComponent(new Metadata.Snapshot("PlayerCreator"), WorkerUtils.UnityGameLogic);
template.AddComponent(new Persistence.Snapshot(), WorkerUtils.UnityGameLogic);
template.AddComponent(new PlayerCreator.Snapshot(), WorkerUtils.UnityGameLogic);

return template;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 0 additions & 41 deletions workers/unity/Assets/Playground/Config/PlayerTemplate.cs

This file was deleted.

11 changes: 0 additions & 11 deletions workers/unity/Assets/Playground/Config/PlayerTemplate.cs.meta

This file was deleted.

1 change: 0 additions & 1 deletion workers/unity/Assets/Playground/Config/WorkerUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public static void AddClientSystems(World world)
world.GetOrCreateSystem<MoveLocalPlayerSystem>();
world.GetOrCreateSystem<InitCameraSystem>();
world.GetOrCreateSystem<FollowCameraSystem>();
world.GetOrCreateSystem<InitUISystem>();
world.GetOrCreateSystem<UpdateUISystem>();
world.GetOrCreateSystem<PlayerCommandsSystem>();
world.GetOrCreateSystem<MetricSendSystem>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ private static Snapshot CreateSnapshot(int cubeCount)
AddPlayerSpawner(snapshot, new Coordinates(-200, 0, 200));

AddCubeGrid(snapshot, cubeCount);

CreateSpinner(snapshot, new Coordinates { X = 5.5, Y = 0.5f, Z = 0.0 });
CreateSpinner(snapshot, new Coordinates { X = -5.5, Y = 0.5f, Z = 0.0 });

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

private static void AddPlayerSpawner(Snapshot snapshot, Coordinates playerSpawnerLocation)
{
var template = new EntityTemplate();
template.AddComponent(new Position.Snapshot(playerSpawnerLocation), WorkerUtils.UnityGameLogic);
template.AddComponent(new Metadata.Snapshot { EntityType = "PlayerCreator" }, WorkerUtils.UnityGameLogic);
template.AddComponent(new Persistence.Snapshot(), WorkerUtils.UnityGameLogic);
template.AddComponent(new PlayerCreator.Snapshot(), WorkerUtils.UnityGameLogic);

template.SetReadAccess(WorkerUtils.UnityGameLogic, WorkerUtils.UnityClient, WorkerUtils.MobileClient);
template.SetComponentWriteAccess(EntityAcl.ComponentId, WorkerUtils.UnityGameLogic);
var template = EntityTemplates.CreatePlayerSpawnerEntityTemplate(playerSpawnerLocation);
snapshot.AddEntity(template);
}

private static void CreateSpinner(Snapshot snapshot, Coordinates coords)
{
var template = EntityTemplates.CreateSpinnerEntityTemplate(coords);
snapshot.AddEntity(template);
}

private static void AddCubeGrid(Snapshot snapshot, int cubeCount)
{
var cubeTemplate = CubeTemplate.CreateCubeEntityTemplate();

// Calculate grid size
var gridLength = (int) Math.Ceiling(Math.Sqrt(cubeCount));
if (gridLength % 2 == 1) // To make sure nothing is in (0, 0)
Expand All @@ -85,40 +82,10 @@ private static void AddCubeGrid(Snapshot snapshot, int cubeCount)
}

var location = new Vector3(x, 1, z);
var positionSnapshot = new Position.Snapshot(location.ToCoordinates());
var transformSnapshot = TransformUtils.CreateTransformSnapshot(location, Quaternion.identity);

cubeTemplate.SetComponent(positionSnapshot);
cubeTemplate.SetComponent(transformSnapshot);
var cubeTemplate = EntityTemplates.CreateCubeEntityTemplate(location);
snapshot.AddEntity(cubeTemplate);
}
}
}

private static void CreateSpinner(Snapshot snapshot, Coordinates coords)
{
const string entityType = "Spinner";

var transform = TransformUtils.CreateTransformSnapshot(coords.ToUnityVector(), Quaternion.identity);

var template = new EntityTemplate();
template.AddComponent(new Position.Snapshot(coords), WorkerUtils.UnityGameLogic);
template.AddComponent(new Metadata.Snapshot(entityType), WorkerUtils.UnityGameLogic);
template.AddComponent(transform, WorkerUtils.UnityGameLogic);
template.AddComponent(new Persistence.Snapshot(), WorkerUtils.UnityGameLogic);
template.AddComponent(new Collisions.Snapshot(), WorkerUtils.UnityGameLogic);
template.AddComponent(new SpinnerColor.Snapshot(Color.BLUE), WorkerUtils.UnityGameLogic);
template.AddComponent(new SpinnerRotation.Snapshot(), WorkerUtils.UnityGameLogic);

var query = InterestQuery.Query(Constraint.RelativeSphere(radius: 25));
var interest = InterestTemplate.Create()
.AddQueries<Position.Component>(query);
template.AddComponent(interest.ToSnapshot(), WorkerUtils.UnityGameLogic);

template.SetReadAccess(WorkerUtils.UnityGameLogic, WorkerUtils.UnityClient, WorkerUtils.MobileClient);
template.SetComponentWriteAccess(EntityAcl.ComponentId, WorkerUtils.UnityGameLogic);

snapshot.AddEntity(template);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ GameObject:
- component: {fileID: 114602415406350378}
- component: {fileID: 2643701918418317171}
- component: {fileID: -663445870753195477}
- component: {fileID: 8124006586705730658}
m_Layer: 9
m_Name: Character
m_TagString: Untagged
Expand Down Expand Up @@ -149,6 +150,19 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 5053c3754e6a48118a61672cedc67dec, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!114 &8124006586705730658
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1292271339760838}
m_Enabled: 0
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 682c3f80479014542ab23d20341b0d2e, type: 3}
m_Name:
m_EditorClassIdentifier:
uiPrefab: {fileID: 1582526976165782, guid: 0adfaf6f7e2705544a15cf9e489deb92, type: 3}
--- !u!1 &1494660013534148
GameObject:
m_ObjectHideFlags: 0
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Improbable;
using Improbable.Gdk.Core;
using Improbable.Gdk.Core.Commands;
using Improbable.Gdk.Subscriptions;
Expand Down Expand Up @@ -46,10 +44,7 @@ private void SpawnCube(EntityId entityId)
var location = gameObject.transform.position - offset;
location.y += 2;

var cubeEntityTemplate = CubeTemplate.CreateCubeEntityTemplate();

cubeEntityTemplate.SetComponent(new Position.Snapshot(location.ToCoordinates()));
cubeEntityTemplate.SetComponent(TransformUtils.CreateTransformSnapshot(location, Quaternion.identity));
var cubeEntityTemplate = EntityTemplates.CreateCubeEntityTemplate(location);

worldCommandRequestSender.SendCreateEntityCommand(
new WorldCommands.CreateEntity.Request(cubeEntityTemplate, entityId), OnEntityCreated);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ private static void Init()
initialized = true;

// Setup template to use for player on connecting client
PlayerLifecycleConfig.CreatePlayerEntityTemplate = PlayerTemplate.CreatePlayerEntityTemplate;
PlayerLifecycleConfig.CreatePlayerEntityTemplate = EntityTemplates.CreatePlayerEntityTemplate;
}
}
}
Loading

0 comments on commit b03b212

Please sign in to comment.