Esempio n. 1
0
        protected override void OnUpdate()
        {
            for (var i = 0; i < transformData.Length; i++)
            {
                var rigidbody = transformData.GameObjectRigidBody[i];
                var transform = transformData.Transform[i];

                var nativePosition = new Vector3(transform.Location.X, transform.Location.Y, transform.Location.Z);

                var positionDifference = (rigidbody.position - origin - nativePosition).sqrMagnitude;

                // Sync if sufficient difference or if sufficient time has passed
                if (positionDifference <= ToleranceMeters)
                {
                    var nativeRotation = new UnityEngine.Quaternion(transform.Rotation.X, transform.Rotation.Y,
                                                                    transform.Rotation.Z, transform.Rotation.W);
                    var rotationDifference = UnityEngine.Quaternion.Angle(rigidbody.rotation, nativeRotation);
                    if (rotationDifference <= ToleranceAngle)
                    {
                        var differencesGreaterThanZero = positionDifference == 0.0f && rotationDifference == 0.0f;
                        if (differencesGreaterThanZero || tickSystem.GlobalTick - transform.Tick < MaxTicksUntilSync)
                        {
                            continue;
                        }
                    }
                }

                var position = rigidbody.position - origin;
                var location = new Location {
                    X = position.x, Y = position.y, Z = position.z
                };
                var rotation = new Quaternion
                {
                    W = rigidbody.rotation.w,
                    X = rigidbody.rotation.x,
                    Y = rigidbody.rotation.y,
                    Z = rigidbody.rotation.z
                };

                var newTransform = new SpatialOSTransform
                {
                    Location = location,
                    Rotation = rotation,
                    Tick     = tickSystem.GlobalTick
                };

                var newPosition = new SpatialOSPosition
                {
                    Coords = new global::Generated.Improbable.Coordinates
                    {
                        X = newTransform.Location.X,
                        Y = newTransform.Location.Y,
                        Z = newTransform.Location.Z
                    }
                };

                transformData.Transform[i] = newTransform;
                transformData.Position[i]  = newPosition;
            }
        }
Esempio n. 2
0
        public static Entity CreatePlayerEntityTemplate(List <string> clientAttributeSet,
                                                        Generated.Improbable.Vector3f position)
        {
            var clientAttribute = clientAttributeSet.First(attribute => attribute != SystemConfig.UnityClient);

            if (clientAttribute == null)
            {
                throw new InvalidOperationException(
                          "Expected an attribute that is not \"UnityClient\" but none was found.");
            }

            var transform =
                SpatialOSTransform.CreateSchemaComponentData(new Location(),
                                                             new Quaternion {
                W = 1, X = 0, Y = 0, Z = 0
            }, 0);
            var playerInput     = SpatialOSPlayerInput.CreateSchemaComponentData(0, 0, false);
            var prefab          = SpatialOSPrefab.CreateSchemaComponentData(ArchetypeConfig.CharacterArchetype);
            var archetype       = SpatialOSArchetypeComponent.CreateSchemaComponentData(ArchetypeConfig.CharacterArchetype);
            var launcher        = SpatialOSLauncher.CreateSchemaComponentData(100, 0);
            var clientHeartbeat = SpatialOSPlayerHeartbeatClient.CreateSchemaComponentData();
            var serverHeartbeat = SpatialOSPlayerHeartbeatServer.CreateSchemaComponentData();
            var score           = SpatialOSScore.CreateSchemaComponentData(0);

            var entityBuilder = EntityBuilder.Begin()
                                .AddPosition(0, 0, 0, SystemConfig.UnityGameLogic)
                                .AddMetadata(ArchetypeConfig.CharacterArchetype, SystemConfig.UnityGameLogic)
                                .SetPersistence(false)
                                .SetReadAcl(AllWorkerAttributes)
                                .SetEntityAclComponentWriteAccess(SystemConfig.UnityGameLogic)
                                .AddComponent(transform, SystemConfig.UnityGameLogic)
                                .AddComponent(playerInput, clientAttribute)
                                .AddComponent(prefab, SystemConfig.UnityGameLogic)
                                .AddComponent(archetype, SystemConfig.UnityGameLogic)
                                .AddComponent(launcher, SystemConfig.UnityGameLogic)
                                .AddComponent(clientHeartbeat, clientAttribute)
                                .AddComponent(serverHeartbeat, SystemConfig.UnityGameLogic)
                                .AddComponent(score, SystemConfig.UnityGameLogic);

            return(entityBuilder.Build());
        }
Esempio n. 3
0
        private static void AddCubeGrid(Snapshot snapshot, int cubeCount)
        {
            // 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;
            const string entityType   = "Cube";

            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 transform = SpatialOSTransform.CreateSchemaComponentData(
                        new Location {
                        X = x, Y = 1, Z = z
                    },
                        new Quaternion {
                        W = 1, X = 0, Y = 0, Z = 0
                    },
                        0
                        );

                    var cubeColor          = SpatialOSCubeColor.CreateSchemaComponentData();
                    var cubeTargetVelocity = SpatialOSCubeTargetVelocity.CreateSchemaComponentData(new Vector3f {
                        X = -2.0f
                    });
                    var prefab             = SpatialOSPrefab.CreateSchemaComponentData(entityType);
                    var launchable         = SpatialOSLaunchable.CreateSchemaComponentData(new EntityId(0));
                    var archetypeComponent = SpatialOSArchetypeComponent.CreateSchemaComponentData(entityType);

                    var entity = EntityBuilder.Begin()
                                 .AddPosition(x, 0, z, SystemConfig.UnityGameLogic)
                                 .AddMetadata(entityType, SystemConfig.UnityGameLogic)
                                 .SetPersistence(true)
                                 .SetReadAcl(UnityWorkers)
                                 .AddComponent(transform, SystemConfig.UnityGameLogic)
                                 .AddComponent(cubeColor, SystemConfig.UnityGameLogic)
                                 .AddComponent(cubeTargetVelocity, SystemConfig.UnityGameLogic)
                                 .AddComponent(prefab, SystemConfig.UnityGameLogic)
                                 .AddComponent(archetypeComponent, SystemConfig.UnityGameLogic)
                                 .AddComponent(launchable, SystemConfig.UnityGameLogic)
                                 .Build();

                    snapshot.AddEntity(entity);
                }
            }
        }