示例#1
0
        /// <summary>
        /// Synchronizes all of the DynamicEntities.
        /// </summary>
        void SynchronizeDynamicEntities()
        {
            // Don't need to synchronize a map that has no Users on it since there would be nobody to synchronize to!
            if (_users.Count == 0)
            {
                return;
            }

            var currentTime = GetTime();

            using (var pw = ServerPacket.GetWriter())
            {
                // Loop through each DynamicEntity
                foreach (var dynamicEntity in DynamicEntities)
                {
                    // Check to synchronize everything but the Position and Velocity
                    if (!dynamicEntity.IsSynchronized)
                    {
                        // Write the data into the PacketWriter, then send it to everyone on the map
                        pw.Reset();
                        ServerPacket.SynchronizeDynamicEntity(pw, dynamicEntity);
                        Send(pw, ServerMessageType.MapDynamicEntityProperty);
                    }

                    // Check to synchronize the Position and Velocity
                    if (dynamicEntity.NeedSyncPositionAndVelocity(currentTime))
                    {
                        // Make sure there are users in range since, if there isn't, we don't even need to synchronize
                        var usersToSyncTo = GetUsersToSyncPandVTo(dynamicEntity);
                        if (usersToSyncTo.IsEmpty())
                        {
                            dynamicEntity.BypassPositionAndVelocitySync(currentTime);
                        }
                        else
                        {
                            pw.Reset();
                            ServerPacket.UpdateVelocityAndPosition(pw, dynamicEntity, currentTime);
                            foreach (var user in usersToSyncTo)
                            {
                                user.Send(pw, ServerMessageType.MapDynamicEntitySpatialUpdate);
                            }
                        }
                    }
                }
            }
        }