// Defines the template for the PlayerShip entity.
        public static EntityTemplate CreatePlayerShipTemplate(string clientWorkerId,
                                                              byte[] serializedArguments)
        {
            var clientAttribute = EntityTemplate.GetWorkerAccessAttribute(clientWorkerId);// CommonRequirementSets.SpecificClientOnly(_clientWorkerId);


            //set position to random for now
            var position = new Vector3(Random.Range(-1f, 1f), 0, Random.Range(-1f, 1f));

            var template = new EntityTemplate();

            template.AddComponent(new Position.Snapshot()
            {
                Coords = position.ToCoordinates()
            }, WorkerUtils.UnityGameLogic);
            template.AddComponent(new Metadata.Snapshot()
            {
                EntityType = SimulationSettings.PlayerShipPrefabName
            }, WorkerUtils.UnityGameLogic);
            template.AddComponent(new ShipControls.Snapshot(), clientAttribute);
            template.AddComponent(new ClientAuthorityCheck.Snapshot(), clientAttribute);

            PlayerLifecycleHelper.AddPlayerLifecycleComponents(template, clientWorkerId, WorkerUtils.UnityGameLogic);
            TransformSynchronizationHelper.AddTransformSynchronizationComponents(template, WorkerUtils.UnityGameLogic, location: position);


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


            return(template);
        }
Exemple #2
0
        protected override void HandleWorkerConnectionEstablished()
        {
            PlayerLifecycleHelper.AddClientSystems(Worker.World);

            Worker.World.AddSystem(new TimeRequestSystem(_ticker));
            Debug.Log("request");
        }
Exemple #3
0
        public static EntityTemplate CreatePlayerEntityTemplate(string workerId, Improbable.Vector3f position)
        {
            var clientAttribute = $"workerId:{workerId}";

            var template = new EntityTemplate();

            template.AddComponent(new Position.Snapshot(), clientAttribute);
            template.AddComponent(new Metadata.Snapshot {
                EntityType = "Character"
            }, WorkerUtils.UnityGameLogic);
            template.AddComponent(new PlayerInput.Snapshot(), clientAttribute);
            template.AddComponent(new Launcher.Snapshot {
                EnergyLeft = 100, RechargeTimeLeft = 0
            },
                                  WorkerUtils.UnityGameLogic);
            template.AddComponent(new Score.Snapshot(), WorkerUtils.UnityGameLogic);
            template.AddComponent(new CubeSpawner.Snapshot {
                SpawnedCubes = new List <EntityId>()
            },
                                  WorkerUtils.UnityGameLogic);
            TransformSynchronizationHelper.AddTransformSynchronizationComponents(template, clientAttribute);
            PlayerLifecycleHelper.AddPlayerLifecycleComponents(template, workerId, clientAttribute,
                                                               WorkerUtils.UnityGameLogic);

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

            return(template);
        }
 protected override void HandleWorkerConnectionEstablished()
 {
     PlayerLifecycleHelper.AddClientSystems(Worker.World);
     TransformSynchronizationHelper.AddClientSystems(Worker.World);
     GameObjectRepresentationHelper.AddSystems(Worker.World);
     GameObjectCreationHelper.EnableStandardGameObjectCreation(Worker.World);
 }
        protected override void HandleWorkerConnectionEstablished()
        {
            PlayerLifecycleHelper.AddClientSystems(Worker.World);
            var gameObjectCreator = new GameObjectCreatorFromTransform(WorkerType, transform.position);

            GameObjectCreationHelper.EnableStandardGameObjectCreation(Worker.World, gameObjectCreator, entityRepresentationMapping);
        }
Exemple #6
0
    public void OnEntityCreated(
        SpatialOSEntity entity,
        EntityGameObjectLinker linker)
    {
        if (!entity.HasComponent <Metadata.Component>())
        {
            return;
        }
        Metadata.Component metadata = entity.GetComponent <Metadata.Component>();
        bool isPlayer     = metadata.EntityType.Equals("Player");
        bool hasAuthority = PlayerLifecycleHelper.IsOwningWorker(entity.SpatialOSEntityId, _world);

        if (isPlayer && hasAuthority)
        {
            string     pathToPrefab     = $"Prefabs/{_workerType}/Authoratative/Player";
            Object     prefab           = Resources.Load(pathToPrefab);
            GameObject playerGameObject = (GameObject)Object.Instantiate(prefab);

            linker.LinkGameObjectToSpatialOSEntity(entity.SpatialOSEntityId, playerGameObject);
        }
        else
        {
            _fallbackCreator.OnEntityCreated(entity, linker);
        }
    }
Exemple #7
0
 public static void AddGameLogicSystems(World world)
 {
     AddLifecycleSystems(world);
     TransformSynchronizationHelper.AddServerSystems(world);
     PlayerLifecycleHelper.AddServerSystems(world);
     GameObjectCreationHelper.EnableStandardGameObjectCreation(world);
 }
Exemple #8
0
        static public EntityTemplate CreatePlayerEntityTemplate(string workerId, byte[] playerCreationArguments)
        {
            //Decide spawn position
            GameObject[] playerSpawnPoints = GameObject.FindGameObjectsWithTag("SpawnPoint");
            Vector3      spawnPos          = playerSpawnPoints[Random.Range(0, playerSpawnPoints.Length - 1)].transform.position;

            //Setup SpatialOS entity and components
            var clientAttribute = $"workerId:{workerId}";
            var serverAttribute = WorkerUtils.UnityGameLogic;

            var template = new EntityTemplate();

            template.AddComponent(new Position.Snapshot {
                Coords = new Coordinates(spawnPos.x, spawnPos.y, spawnPos.z)
            }, serverAttribute);
            template.AddComponent(new Metadata.Snapshot {
                EntityType = "Player"
            }, serverAttribute);
            template.AddComponent(new PlayerInput.Snapshot {
                TargetPosition = Vector3f.Zero
            }, clientAttribute);
            template.AddComponent(new PlayerHealth.Snapshot {
                Health = 100
            }, serverAttribute);
            template.AddComponent(new Chat.Snapshot(), WorkerUtils.ChatManager);
            TransformSynchronizationHelper.AddTransformSynchronizationComponents(template, serverAttribute);
            PlayerLifecycleHelper.AddPlayerLifecycleComponents(template, workerId, clientAttribute, serverAttribute);

            template.SetReadAccess(WorkerUtils.UnityClient, WorkerUtils.ChatManager, /*AndroidClientWorkerConnector.WorkerType, iOSClientWorkerConnector.WorkerType,*/ serverAttribute);
            template.SetComponentWriteAccess(EntityAcl.ComponentId, serverAttribute);

            return(template);
        }
Exemple #9
0
        public void IsOwningWorker_should_return_false_if_entity_doesnt_have_OwningWorker_component()
        {
            connectionHandler.CreateEntity(1, new EntityTemplate());
            receiveSystem.Update();

            Assert.IsFalse(PlayerLifecycleHelper.IsOwningWorker(new EntityId(1), worker.World));
        }
    private void CreatePlayer(SpatialOSEntity entity, EntityGameObjectLinker linker)
    {
        if (!entity.TryGetComponent <Metadata.Component>(out var metadata) ||
            !entity.TryGetComponent <Position.Component>(out var spatialOSPosition))
        {
            return;
        }

        bool   hasAuthority = PlayerLifecycleHelper.IsOwningWorker(entity.SpatialOSEntityId, world);
        string pathToPrefab;

        if (hasAuthority)
        {
            pathToPrefab = $"Prefabs/{workerType}/Authoritative/Player";
        }
        else
        {
            pathToPrefab = $"Prefabs/{workerType}/Non-Authoritative/Player";
        }

        var prefab   = Resources.Load <GameObject>(pathToPrefab);
        var position = spatialOSPosition.Coords.ToUnityVector() + workerOrigin;

        var playerGameObject = Object.Instantiate(prefab, position, Quaternion.identity);

        playerGameObject.name = $"{metadata.EntityType}(SpatialOS: {entity.SpatialOSEntityId}, Worker: {workerType})";

        linker.LinkGameObjectToSpatialOSEntity(entity.SpatialOSEntityId, (GameObject)playerGameObject,
                                               componentsToAdd);
    }
        public static EntityTemplate Player(string workerType, byte[] args)
        {
            //TODO: Add all gameplay components
            var client = $"workerId:{workerType}";

            var template = CreateDefaultEntity(EntityUtils.PlayerEntityType);

            var healthComp = new HealthComponent.Snapshot(
                health: PlayerHealthConfig.MaxHealth, maxHealth: PlayerHealthConfig.MaxHealth);

            var healthRegenComponent = new HealthRegenComponent.Snapshot
            {
                CooldownSyncInterval = PlayerHealthConfig.SpatialCooldownSyncInterval,
                DamagedRecently      = false,
                RegenAmount          = PlayerHealthConfig.RegenAmount,
                RegenCooldownTimer   = PlayerHealthConfig.RegenAfterDamageCooldown,
                RegenInterval        = PlayerHealthConfig.RegenInterval,
                RegenPauseTime       = 0,
            };

            template.AddComponent(healthComp, WorkerUtils.UnityGameLogic);
            template.AddComponent(healthRegenComponent, WorkerUtils.UnityGameLogic);

            PlayerLifecycleHelper.AddPlayerLifecycleComponents(template, workerType, client, WorkerUtils.UnityGameLogic);
            template.SetReadAccess(WorkerUtils.UnityClient, WorkerUtils.UnityGameLogic);

            return(template);
        }
    protected override void HandleWorkerConnectionEstablished()
    {
        Debug.Log("Handling client worker connection established");

        PlayerLifecycleHelper.AddClientSystems(Worker.World);
        GameObjectCreationHelper.EnableStandardGameObjectCreation(Worker.World, entityRepresentationMapping);
    }
Exemple #13
0
        public static EntityTemplate CreatePlayerEntityTemplate(EntityId entityId, string workerId, byte[] serializedArguments)
        {
            var clientAttribute = EntityTemplate.GetWorkerAccessAttribute(workerId);
            var serverAttribute = UnityGameLogicConnector.WorkerType;

            var position = new Vector3(0, 1f, 0);
            var coords   = Coordinates.FromUnityVector(position);

            var template = new EntityTemplate();

            template.AddComponent(new Position.Snapshot(coords), clientAttribute);
            template.AddComponent(new Metadata.Snapshot("Player"), serverAttribute);

            PlayerLifecycleHelper.AddPlayerLifecycleComponents(template, workerId, serverAttribute);
            TransformSynchronizationHelper.AddTransformSynchronizationComponents(template, clientAttribute, position);

            const int serverRadius = 500;
            var       clientRadius = workerId.Contains(MobileClientWorkerConnector.WorkerType) ? 100 : 500;

            var serverQuery = InterestQuery.Query(Constraint.RelativeCylinder(serverRadius));
            var clientQuery = InterestQuery.Query(Constraint.RelativeCylinder(clientRadius));

            var interest = InterestTemplate.Create()
                           .AddQueries <Metadata.Component>(serverQuery)
                           .AddQueries <Position.Component>(clientQuery);

            template.AddComponent(interest.ToSnapshot(), serverAttribute);

            template.SetReadAccess(UnityClientConnector.WorkerType, MobileClientWorkerConnector.WorkerType, serverAttribute);
            template.SetComponentWriteAccess(EntityAcl.ComponentId, serverAttribute);

            return(template);
        }
Exemple #14
0
        protected override void HandleWorkerConnectionEstablished()
        {
            PlayerLifecycleHelper.AddClientSystems(Worker.World, false);

            GameObjectCreationHelper.EnableStandardGameObjectCreation(Worker.World,
                                                                      new AdvancedEntityPipeline(Worker), entityRepresentationMapping);
        }
Exemple #15
0
        public static EntityTemplate CreatePlayerEntityTemplate(string workerId, Improbable.Vector3f position)
        {
            var clientAttribute = $"workerId:{workerId}";

            var template = new EntityTemplate();

            template.AddComponent(new Position.Snapshot(), clientAttribute);
            template.AddComponent(new Metadata.Snapshot {
                EntityType = "User"
            }, UnityGameLogicConnector.WorkerType);

            template.AddComponent(new Player.PlayerInput.Snapshot(), clientAttribute);

            /*
             * template.AddComponent(new Launcher.Snapshot { EnergyLeft = 100, RechargeTimeLeft = 0 },
             *  UnityGameLogicConnector.WorkerType);
             * template.AddComponent(new Score.Snapshot(), WorkerUtils.UnityGameLogic);
             * template.AddComponent(new CubeSpawner.Snapshot { SpawnedCubes = new List<EntityId>() },
             *  UnityGameLogicConnector.WorkerType);
             */
            template.AddComponent(new Player.PlayerAuth.Snapshot {
                IsAuthed = false, PlayerName = "unauthorized player"
            },
                                  "GRPCManager");
            TransformSynchronizationHelper.AddTransformSynchronizationComponents(template, clientAttribute);
            PlayerLifecycleHelper.AddPlayerLifecycleComponents(template, workerId, clientAttribute,
                                                               UnityGameLogicConnector.WorkerType);

            template.SetReadAccess(UnityClientConnector.WorkerType, UnityGameLogicConnector.WorkerType, "GRPCManager");
            template.SetComponentWriteAccess(EntityAcl.ComponentId, UnityGameLogicConnector.WorkerType);

            return(template);
        }
Exemple #16
0
        public void IsOwningWorker_should_return_true_if_OwningWorker_component_has_my_worker_id()
        {
            connectionHandler.CreateEntity(1, GetOwnedEntity(worker.WorkerId));
            receiveSystem.Update();

            Assert.IsTrue(PlayerLifecycleHelper.IsOwningWorker(new EntityId(1), worker.World));
        }
Exemple #17
0
        public void IsOwningWorker_should_return_false_if_entity_isnt_owned_by_this_worker()
        {
            connectionHandler.CreateEntity(1, GetOwnedEntity("other-worker"));
            receiveSystem.Update();

            Assert.IsFalse(PlayerLifecycleHelper.IsOwningWorker(new EntityId(1), worker.World));
        }
 protected override void HandleWorkerConnectionEstablished()
 {
     Worker.World.GetOrCreateManager <MetricSendSystem>();
     PlayerLifecycleHelper.AddServerSystems(Worker.World);
     GameObjectCreationHelper.EnableStandardGameObjectCreation(Worker.World);
     TransformSynchronizationHelper.AddServerSystems(Worker.World);
 }
Exemple #19
0
 protected override void HandleWorkerConnectionEstablished()
 {
     PlayerLifecycleHelper.AddClientSystems(Worker.World);
     // TODO: We'll probably have to enable these once we start building for mobile ~Jorden
     //GameObjectCreationHelper.EnableStandardGameObjectCreation(Worker.World);
     //TransformSynchronizationHelper.AddClientSystems(Worker.World);
 }
Exemple #20
0
        protected override void HandleWorkerConnectionEstablished()
        {
            Worker.World.GetOrCreateSystem <MetricSendSystem>();
            PlayerLifecycleHelper.AddServerSystems(Worker.World);

            Worker.World.AddSystem(new TimeResponseSystem());
            Debug.Log("Response");
        }
Exemple #21
0
    protected override void HandleWorkerConnectionEstablished()
    {
        Debug.Log("Server connection established");
        var world = Worker.World;

        PlayerLifecycleHelper.AddServerSystems(world);
        GameObjectCreationHelper.EnableStandardGameObjectCreation(Worker.World, entityRepresentationMapping);

        // Add server systems
    }
        protected override void HandleWorkerConnectionEstablished()
        {
            PlayerLifecycleHelper.AddClientSystems(Worker.World);
            TransformSynchronizationHelper.AddClientSystems(Worker.World);

            IEntityGameObjectCreator fallbackCreator = new GameObjectCreatorFromMetadata(Worker.WorkerType, Worker.Origin, Worker.LogDispatcher);
            IEntityGameObjectCreator customCreator   = new PlayerGameObjectCreator(fallbackCreator, Worker.World, Worker.WorkerType);

            GameObjectCreationHelper.EnableStandardGameObjectCreation(Worker.World, customCreator);
        }
Exemple #23
0
        public static void AddStrategyLogicSystems(World world, EntityRepresentationMapping entityRepresentationMapping)
        {
            TransformSynchronizationHelper.AddServerSystems(world);
            PlayerLifecycleHelper.AddServerSystems(world);
            GameObjectCreationHelper.EnableStandardGameObjectCreation(world, entityRepresentationMapping);

            world.GetOrCreateSystem <ProcessLaunchCommandSystem>();
            world.GetOrCreateSystem <MetricSendSystem>();
            //world.GetOrCreateSystem<ArmyCloudUpdateSystem>();
        }
        protected override void HandleWorkerConnectionEstablished()
        {
            PlayerLifecycleHelper.AddClientSystems(Worker.World, false);

            var fallback = new GameObjectCreatorFromMetadata(Worker.WorkerType,
                                                             Worker.Origin, Worker.LogDispatcher);

            GameObjectCreationHelper.EnableStandardGameObjectCreation(
                Worker.World,
                new AdvancedEntityPipeline(Worker, AuthPlayer, NonAuthPlayer, fallback));
        }
        protected override void HandleWorkerConnectionEstablished()
        {
            Worker.World.GetOrCreateSystem <MetricSendSystem>();
            PlayerLifecycleHelper.AddServerSystems(Worker.World);
            GameObjectCreationHelper.EnableStandardGameObjectCreation(Worker.World);

            if (level != null)
            {
                levelInstance = Instantiate(level, transform.position, transform.rotation);
            }
        }
Exemple #26
0
        protected override void HandleWorkerConnectionEstablished()
        {
            PlayerLifecycleHelper.AddClientSystems(Worker.World);
            GameObjectCreationHelper.EnableStandardGameObjectCreation(Worker.World, entityRepresentationMapping);

            if (level == null)
            {
                return;
            }

            levelInstance = Instantiate(level, transform.position, transform.rotation);
        }
        public static EntityTemplate CreateAdvancedUnitEntityTemplate(string workerId, Coordinates coords, UnitSide side)
        {
            bool   isPlayer = workerId != null;
            string controllAttribute;

            if (isPlayer)
            {
                controllAttribute = EntityTemplate.GetWorkerAccessAttribute(workerId);
            }
            else
            {
                controllAttribute = WorkerUtils.UnityGameLogic;
            }

            var template = new EntityTemplate();

            template.AddComponent(new Position.Snapshot {
                Coords = coords
            }, controllAttribute);
            template.AddComponent(new Metadata.Snapshot(isPlayer ? "Player": "AdvancedUnit"), WorkerUtils.UnityGameLogic);
            template.AddComponent(new BulletComponent.Snapshot(), controllAttribute);
            template.AddComponent(new AdvancedUnitController.Snapshot(), controllAttribute);
            template.AddComponent(new BaseUnitHealth.Snapshot(), WorkerUtils.UnityGameLogic);
            template.AddComponent(new GunComponent.Snapshot {
                GunsDic = new Dictionary <PosturePoint, GunInfo>()
            }, WorkerUtils.UnityGameLogic);
            template.AddComponent(new FuelComponent.Snapshot(), WorkerUtils.UnityGameLogic);

            if (isPlayer)
            {
                template.AddComponent(new AdvancedPlayerInput.Snapshot(), controllAttribute);
                template.AddComponent(new PlayerInfo.Snapshot {
                    ClientWorkerId = workerId
                }, controllAttribute);
            }
            else
            {
                template.AddComponent(new AdvancedUnmannedInput.Snapshot(), controllAttribute);
            }

            template.AddComponent(new BaseUnitStatus.Snapshot {
                Type = UnitType.Advanced, Side = side, State = UnitState.Alive
            }, WorkerUtils.UnityGameLogic);

            TransformSynchronizationHelper.AddTransformSynchronizationComponents(template, controllAttribute);
            PlayerLifecycleHelper.AddPlayerLifecycleComponents(template, workerId, WorkerUtils.UnityGameLogic);

            template.SetReadAccess(UnityClientConnector.WorkerType, MobileClientWorkerConnector.WorkerType, WorkerUtils.UnityGameLogic);
            template.SetComponentWriteAccess(EntityAcl.ComponentId, WorkerUtils.UnityGameLogic);

            return(template);
        }
Exemple #28
0
        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);
        }
Exemple #29
0
        private void ConfigurePlayerLifeCycle(Unity.Entities.World world)
        {
            PlayerLifecycleHelper.AddClientSystems(world);
            world.GetOrCreateManager <HandlePlayerHeartbeatRequestSystem>();

            var fallback = new GameObjectCreatorFromMetadata(Worker.WorkerId, Worker.Origin, Worker.LogDispatcher);

            GameObjectCreationHelper.EnableStandardGameObjectCreation(
                world,
                new AdvancedEntityCreationPipeline(
                    Worker, EntityUtils.PlayerEntityType, AuthPlayer, NonAuthPlayer, fallback),
                gameObject);
        }
        protected override void HandleWorkerConnectionEstablished()
        {
            var world = Worker.World;

            PlayerLifecycleHelper.AddClientSystems(world, autoRequestPlayerCreation: false);
            PlayerLifecycleConfig.MaxPlayerCreationRetries = 0;

            var entityPipeline = new AdvancedEntityPipeline(Worker, GetAuthPlayerPrefabPath(), GetNonAuthPlayerPrefabPath());

            entityPipeline.OnRemovedAuthoritativePlayer += RemovingAuthoritativePlayer;

            // Set the Worker gameObject to the ClientWorker so it can access PlayerCreater reader/writers
            GameObjectCreationHelper.EnableStandardGameObjectCreation(world, entityPipeline, gameObject);
        }