Esempio n. 1
0
        public static void UpdateServerEntity(GameEntity gameEntity)
        {
            ServerEntity entity = ServerEntities.ContainsKey(gameEntity.ID) ? ServerEntities[gameEntity.ID] : null;

            if (entity == null)
            {
                ServerEntities.Add(gameEntity.ID, entity = new ServerEntity()
                {
                    ID = gameEntity.ID, EntityType = gameEntity.EntityType, Radius = ((Sphere)gameEntity.PhysicsEntity).Radius
                });
                if (gameEntity.EntityType == EntityType.Player)
                {
                    entity.SyncInterval = 10;
                }
                ServerEntityIDs.Add(gameEntity.ID);
            }
            entity.World             = gameEntity.World;
            entity.AngularDamping    = gameEntity.PhysicsEntity.AngularDamping;
            entity.LinearDamping     = gameEntity.PhysicsEntity.LinearDamping;
            entity.Scale.X           = gameEntity.Scale.M11;
            entity.Scale.Y           = gameEntity.Scale.M22;
            entity.Scale.Z           = gameEntity.Scale.M33;
            entity.AngularVelocity.X = gameEntity.PhysicsEntity.AngularVelocity.X;
            entity.AngularVelocity.Y = gameEntity.PhysicsEntity.AngularVelocity.Y;
            entity.AngularVelocity.Z = gameEntity.PhysicsEntity.AngularVelocity.Z;
            entity.LinearVelocity.X  = gameEntity.PhysicsEntity.LinearVelocity.X;
            entity.LinearVelocity.Y  = gameEntity.PhysicsEntity.LinearVelocity.Y;
            entity.LinearVelocity.Z  = gameEntity.PhysicsEntity.LinearVelocity.Z;
            entity.Updated           = true;
        }
Esempio n. 2
0
        private static void SyncServerEntities()
        {
            CommandWriter cw = new CommandWriter();

            while (Active)
            {
                bool sendPending = false;

                int entityCount = ServerEntityIDs.Count;
                for (int i = 0; i < entityCount; i++)
                {
                    ServerEntity entity = ServerEntities[ServerEntityIDs[i]];
                    if (entity.Updated && entity.ShouldSync)
                    {
                        if (entity.ID == 2)
                        {
                        }
                        cw.Reset();
                        cw.WriteCommand(Commands.UpdateEntity);
                        cw.WriteData(entity.ID);
                        cw.WriteData((byte)entity.EntityType);
                        cw.WriteMatrix(entity.World);
                        cw.WriteVector3(entity.Scale);
                        cw.WriteData((byte)entity.PhysicsShape);
                        cw.WriteData(entity.Radius);
                        cw.WriteData(entity.AngularDamping);
                        cw.WriteData(entity.LinearDamping);
                        cw.WriteVector3(entity.AngularVelocity);
                        cw.WriteVector3(entity.LinearVelocity);
                        if (FillBuffer(cw.GetBytes()))
                        {
                            sendPending = true;
                        }
                        else
                        {
                            FlushBuffer();
                            FillBuffer(cw.GetBytes());
                            sendPending = false;
                        }
                        entity.Updated = false;
                        entity.ResetSync();
                    }
                }
                if (sendPending)
                {
                    FlushBuffer();
                }
                Thread.Sleep(1);
            }
        }