|
| 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 | +} |
0 commit comments