コード例 #1
0
        public void Can_create_from_schema_object()
        {
            var data = new ComponentData(0, SchemaComponentData.Create()); // Easiest way to get a valid `SchemaObject`.

            try
            {
                var schemaObject = data.SchemaData.Value.GetFields();

                var position = new Position.Snapshot(new Coordinates(10, 10, 10));
                Position.Serialization.SerializeSnapshot(position, schemaObject.AddObject(Position.ComponentId));

                var playerHeartbeatClient = new PlayerHeartbeatClient.Snapshot();
                PlayerHeartbeatClient.Serialization.SerializeSnapshot(playerHeartbeatClient,
                                                                      schemaObject.AddObject(PlayerHeartbeatClient.ComponentId));

                var snapshot = new EntitySnapshot(schemaObject);

                Assert.IsTrue(snapshot.TryGetComponentSnapshot <Position.Snapshot>(out var outPosition));
                Assert.IsTrue(
                    snapshot.TryGetComponentSnapshot <PlayerHeartbeatClient.Snapshot>(out var playerHeartbeat));
                Assert.AreEqual(outPosition.Coords.X, 10, Double.Epsilon);
            }
            finally
            {
                data.SchemaData.Value.Destroy();
            }
        }
コード例 #2
0
        public static EntityTemplate Spawner(Coordinates spawnerCoordinates)
        {
            var position = new Position.Snapshot(spawnerCoordinates);
            var metadata = new Metadata.Snapshot("PlayerCreator");

            var template = new EntityTemplate();

            template.AddComponent(position, WorkerUtils.UnityGameLogic);
            template.AddComponent(metadata, WorkerUtils.UnityGameLogic);
            template.AddComponent(new Persistence.Snapshot(), WorkerUtils.UnityGameLogic);
            template.AddComponent(new PlayerCreator.Snapshot(), WorkerUtils.UnityGameLogic);

            return(template);
        }
コード例 #3
0
        private static EntityTemplate CreateDefaultEntity(string entityType, Vector3 initialPos = default(Vector3))
        {
            var metadata = new Metadata.Snapshot()
            {
                EntityType = entityType
            };
            var pos = new Position.Snapshot(initialPos.ToSpatialCoordinates());

            var template = new EntityTemplate();

            template.AddComponent(metadata, WorkerUtils.UnityGameLogic);
            template.AddComponent(pos, WorkerUtils.UnityGameLogic);
            template.SetComponentWriteAccess(EntityAcl.ComponentId, WorkerUtils.UnityGameLogic);

            return(template);
        }
コード例 #4
0
        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)
            {
                gridLength += 1;
            }

            var cubesToSpawn = cubeCount;

            for (var x = -gridLength + 1; x <= gridLength - 1; x += 2)
            {
                for (var z = -gridLength + 1; z <= gridLength - 1; z += 2)
                {
                    // Leave the centre empty
                    if (x == 0 && z == 0)
                    {
                        continue;
                    }

                    // Exit when we've hit our cube limit
                    if (cubesToSpawn-- <= 0)
                    {
                        return;
                    }

                    var positionSnapshot = new Position.Snapshot
                    {
                        Coords = new Coordinates(x, 1, z)
                    };
                    var transformSnapshot = new TransformInternal.Snapshot
                    {
                        Location       = new Location(x, 1, z),
                        Rotation       = new Quaternion(1, 0, 0, 0),
                        TicksPerSecond = 1f / Time.fixedDeltaTime
                    };

                    cubeTemplate.SetComponent(positionSnapshot);
                    cubeTemplate.SetComponent(transformSnapshot);
                    snapshot.AddEntity(cubeTemplate);
                }
            }
        }
コード例 #5
0
        public static EntityTemplate Spawner()
        {
            var position = new Position.Snapshot {
                Coords = new Vector3().ToSpatialCoordinates()
            };
            var metadata = new Metadata.Snapshot {
                EntityType = "PlayerCreator"
            };

            var template = new EntityTemplate();

            template.AddComponent(position, WorkerUtils.UnityGameLogic);
            template.AddComponent(metadata, WorkerUtils.UnityGameLogic);
            template.AddComponent(new Persistence.Snapshot(), WorkerUtils.UnityGameLogic);
            template.AddComponent(new PlayerCreator.Snapshot(), WorkerUtils.UnityGameLogic);

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

            return(template);
        }
コード例 #6
0
        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)
            {
                gridLength += 1;
            }

            var cubesToSpawn = cubeCount;

            for (var x = -gridLength + 1; x <= gridLength - 1; x += 2)
            {
                for (var z = -gridLength + 1; z <= gridLength - 1; z += 2)
                {
                    // Leave the centre empty
                    if (x == 0 && z == 0)
                    {
                        continue;
                    }

                    // Exit when we've hit our cube limit
                    if (cubesToSpawn-- <= 0)
                    {
                        return;
                    }

                    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);
                    snapshot.AddEntity(cubeTemplate);
                }
            }
        }
コード例 #7
0
        public static EntityTemplate DeploymentState()
        {
            const uint sessionTimeSeconds = 300;

            var position = new Position.Snapshot();
            var metadata = new Metadata.Snapshot {
                EntityType = "DeploymentState"
            };

            var template = new EntityTemplate();

            template.AddComponent(position, WorkerUtils.UnityGameLogic);
            template.AddComponent(metadata, WorkerUtils.UnityGameLogic);
            template.AddComponent(new Persistence.Snapshot(), WorkerUtils.UnityGameLogic);
            template.AddComponent(new Session.Snapshot(Status.LOBBY), WorkerUtils.UnityGameLogic);
            template.AddComponent(new Deployment.Snapshot(), WorkerUtils.DeploymentManager);
            template.AddComponent(new Timer.Snapshot(0, sessionTimeSeconds), WorkerUtils.UnityGameLogic);

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

            return(template);
        }
コード例 #8
0
        public static EntityTemplate Player(string workerId, byte[] args)
        {
            var client = EntityTemplate.GetWorkerAccessAttribute(workerId);

            var(spawnPosition, spawnYaw, spawnPitch) = SpawnPoints.GetRandomSpawnPoint();

            var serverResponse = new ServerResponse
            {
                Position = spawnPosition.ToIntAbsolute()
            };

            var rotationUpdate = new RotationUpdate
            {
                Yaw   = spawnYaw.ToInt1k(),
                Pitch = spawnPitch.ToInt1k()
            };

            var pos = new Position.Snapshot {
                Coords = spawnPosition.ToSpatialCoordinates()
            };
            var serverMovement = new ServerMovement.Snapshot {
                Latest = serverResponse
            };
            var clientMovement = new ClientMovement.Snapshot {
                Latest = new ClientRequest()
            };
            var clientRotation = new ClientRotation.Snapshot {
                Latest = rotationUpdate
            };
            var shootingComponent = new ShootingComponent.Snapshot();
            var gunComponent      = new GunComponent.Snapshot {
                GunId = PlayerGunSettings.DefaultGunIndex
            };
            var gunStateComponent = new GunStateComponent.Snapshot {
                IsAiming = false
            };
            var healthComponent = new HealthComponent.Snapshot
            {
                Health    = PlayerHealthSettings.MaxHealth,
                MaxHealth = PlayerHealthSettings.MaxHealth,
            };

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

            var sessionQuery  = InterestQuery.Query(Constraint.Component <Session.Component>());
            var checkoutQuery = InterestQuery.Query(Constraint.RelativeCylinder(150));

            var interestTemplate  = InterestTemplate.Create().AddQueries <ClientMovement.Component>(sessionQuery, checkoutQuery);
            var interestComponent = interestTemplate.ToSnapshot();

            var playerName = Encoding.ASCII.GetString(args);

            var playerStateComponent = new PlayerState.Snapshot
            {
                Name   = playerName,
                Kills  = 0,
                Deaths = 0,
            };


            var playerResComponent      = new Pickups.PlayerRes.Snapshot(0);
            var soldierManagerComponent = new Soldiers.SoldierManager.Snapshot();


            var template = new EntityTemplate();

            template.AddComponent(pos, WorkerUtils.UnityGameLogic);
            template.AddComponent(new Metadata.Snapshot {
                EntityType = "Player"
            }, WorkerUtils.UnityGameLogic);
            template.AddComponent(serverMovement, WorkerUtils.UnityGameLogic);
            template.AddComponent(clientMovement, client);
            template.AddComponent(clientRotation, client);

            template.AddComponent(shootingComponent, client);
            template.AddComponent(gunComponent, WorkerUtils.UnityGameLogic);
            template.AddComponent(gunStateComponent, client);
            template.AddComponent(healthComponent, WorkerUtils.UnityGameLogic);
            template.AddComponent(healthRegenComponent, WorkerUtils.UnityGameLogic);
            template.AddComponent(playerStateComponent, WorkerUtils.UnityGameLogic);

            template.AddComponent(interestComponent, WorkerUtils.UnityGameLogic);

            template.AddComponent(playerResComponent, WorkerUtils.UnityGameLogic);
            template.AddComponent(soldierManagerComponent, WorkerUtils.UnityGameLogic);


            PlayerLifecycleHelper.AddPlayerLifecycleComponents(template, workerId, WorkerUtils.UnityGameLogic);

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

            return(template);
        }
        public static EntityTemplate Player(string workerId, byte[] args)
        {
            var client = EntityTemplate.GetWorkerAccessAttribute(workerId);

            var(spawnPosition, spawnYaw, spawnPitch) = SpawnPoints.GetRandomSpawnPoint();

            var serverResponse = new ServerResponse
            {
                Position = spawnPosition.ToIntAbsolute()
            };

            var rotationUpdate = new RotationUpdate
            {
                Yaw   = spawnYaw.ToInt1k(),
                Pitch = spawnPitch.ToInt1k()
            };

            var pos = new Position.Snapshot {
                Coords = spawnPosition.ToSpatialCoordinates()
            };
            var serverMovement = new ServerMovement.Snapshot {
                Latest = serverResponse
            };
            var clientMovement = new ClientMovement.Snapshot {
                Latest = new ClientRequest()
            };
            var clientRotation = new ClientRotation.Snapshot {
                Latest = rotationUpdate
            };
            var shootingComponent = new ShootingComponent.Snapshot();
            var gunComponent      = new GunComponent.Snapshot {
                GunId = PlayerGunSettings.DefaultGunIndex
            };
            var gunStateComponent = new GunStateComponent.Snapshot {
                IsAiming = false
            };
            var healthComponent = new HealthComponent.Snapshot
            {
                Health    = PlayerHealthSettings.MaxHealth,
                MaxHealth = PlayerHealthSettings.MaxHealth,
            };

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

            var template = new EntityTemplate();

            template.AddComponent(pos, WorkerUtils.UnityGameLogic);
            template.AddComponent(new Metadata.Snapshot {
                EntityType = "Player"
            }, WorkerUtils.UnityGameLogic);
            template.AddComponent(serverMovement, WorkerUtils.UnityGameLogic);
            template.AddComponent(clientMovement, client);
            template.AddComponent(clientRotation, client);
            template.AddComponent(shootingComponent, client);
            template.AddComponent(gunComponent, WorkerUtils.UnityGameLogic);
            template.AddComponent(gunStateComponent, client);
            template.AddComponent(healthComponent, WorkerUtils.UnityGameLogic);
            template.AddComponent(healthRegenComponent, WorkerUtils.UnityGameLogic);
            PlayerLifecycleHelper.AddPlayerLifecycleComponents(template, workerId, WorkerUtils.UnityGameLogic);

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

            return(template);
        }
コード例 #10
0
        public static EntityTemplate Player(EntityId entityId, string workerId, byte[] args)
        {
            var client = EntityTemplate.GetWorkerAccessAttribute(workerId);

            var(spawnPosition, spawnYaw, spawnPitch) = SpawnPoints.GetRandomSpawnPoint();

            var serverResponse = new ServerResponse
            {
                Position = spawnPosition.ToVector3Int()
            };

            var rotationUpdate = new RotationUpdate
            {
                Yaw   = spawnYaw.ToInt1k(),
                Pitch = spawnPitch.ToInt1k()
            };

            var pos = new Position.Snapshot {
                Coords = Coordinates.FromUnityVector(spawnPosition)
            };
            var serverMovement = new ServerMovement.Snapshot {
                Latest = serverResponse
            };
            var clientMovement = new ClientMovement.Snapshot {
                Latest = new ClientRequest()
            };
            var clientRotation = new ClientRotation.Snapshot {
                Latest = rotationUpdate
            };
            var shootingComponent = new ShootingComponent.Snapshot();
            var gunComponent      = new GunComponent.Snapshot {
                GunId = PlayerGunSettings.DefaultGunIndex
            };
            var gunStateComponent = new GunStateComponent.Snapshot {
                IsAiming = false
            };
            var healthComponent = new HealthComponent.Snapshot
            {
                Health    = PlayerHealthSettings.MaxHealth,
                MaxHealth = PlayerHealthSettings.MaxHealth,
            };

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

            var template = new EntityTemplate();

            template.AddComponent(pos, WorkerUtils.UnityGameLogic);
            template.AddComponent(new Metadata.Snapshot {
                EntityType = "Player"
            }, WorkerUtils.UnityGameLogic);
            template.AddComponent(serverMovement, WorkerUtils.UnityGameLogic);
            template.AddComponent(clientMovement, client);
            template.AddComponent(clientRotation, client);
            template.AddComponent(shootingComponent, client);
            template.AddComponent(gunComponent, WorkerUtils.UnityGameLogic);
            template.AddComponent(gunStateComponent, client);
            template.AddComponent(healthComponent, WorkerUtils.UnityGameLogic);
            template.AddComponent(healthRegenComponent, WorkerUtils.UnityGameLogic);

            PlayerLifecycleHelper.AddPlayerLifecycleComponents(template, workerId, WorkerUtils.UnityGameLogic);

            const int serverRadius = 150;
            var       clientRadius = workerId.Contains(WorkerUtils.MobileClient) ? 60 : 150;

            // Position, Metadata, OwningWorker and ServerMovement are included in all queries, since these
            // components are required by the GameObject creator.

            // HealthComponent is needed by the LookAtRagdoll script for respawn behaviour.
            // GunComponent is needed by the GunManager script.
            var clientSelfInterest = InterestQuery.Query(Constraint.EntityId(entityId)).FilterResults(new[]
            {
                Position.ComponentId, Metadata.ComponentId, OwningWorker.ComponentId,
                ServerMovement.ComponentId, HealthComponent.ComponentId, GunComponent.ComponentId
            });

            // ClientRotation is used for rendering other players.
            // GunComponent is required by the GunManager script.
            // GunStateComponent and ShootingComponent are needed for rendering other players' shots.
            var clientRangeInterest = InterestQuery.Query(Constraint.RelativeCylinder(clientRadius)).FilterResults(new[]
            {
                Position.ComponentId, Metadata.ComponentId, OwningWorker.ComponentId,
                ServerMovement.ComponentId, ClientRotation.ComponentId, HealthComponent.ComponentId,
                GunComponent.ComponentId, GunStateComponent.ComponentId, ShootingComponent.ComponentId
            });

            // ClientMovement is used by the ServerMovementDriver script.
            // ShootingComponent is used by the ServerShootingSystem.
            var serverSelfInterest = InterestQuery.Query(Constraint.EntityId(entityId)).FilterResults(new[]
            {
                ClientMovement.ComponentId, ShootingComponent.ComponentId
            });

            // ClientRotation is used for driving player proxies.
            // HealthComponent is required by the VisiblityAndCollision script.
            // ShootingComponent is used by the ServerShootingSystem.
            var serverRangeInterest = InterestQuery.Query(Constraint.RelativeCylinder(serverRadius)).FilterResults(new[]
            {
                Position.ComponentId, Metadata.ComponentId, OwningWorker.ComponentId,
                ServerMovement.ComponentId, ClientRotation.ComponentId, HealthComponent.ComponentId,
                ShootingComponent.ComponentId
            });

            var interest = InterestTemplate.Create()
                           .AddQueries <ClientMovement.Component>(clientSelfInterest, clientRangeInterest)
                           .AddQueries <ServerMovement.Component>(serverSelfInterest, serverRangeInterest);

            template.AddComponent(interest.ToSnapshot());

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

            return(template);
        }