示例#1
0
        protected virtual void BroadCastMovement(Player[] players, Entity[] entities)
        {
            DateTime now = DateTime.UtcNow;

            if (players.Length == 0)
            {
                return;
            }

            if (players.Length <= 1 && entities.Length == 0)
            {
                return;
            }

            if (now - _lastBroadcast < TimeSpan.FromMilliseconds(50))
            {
                return;
            }

            DateTime tickTime = _lastSendTime;

            _lastSendTime = DateTime.UtcNow;

            using (MemoryStream stream = MiNetServer.MemoryStreamManager.GetStream())
            {
                int playerMoveCount = 0;
                int entiyMoveCount  = 0;

                foreach (var player in players)
                {
                    if (now - player.LastUpdatedTime <= now - tickTime)
                    {
                        PlayerLocation knownPosition = player.KnownPosition;

                        McpeMovePlayer move = McpeMovePlayer.CreateObject();
                        move.entityId = player.EntityId;
                        move.x        = knownPosition.X;
                        move.y        = knownPosition.Y + 1.62f;
                        move.z        = knownPosition.Z;
                        move.yaw      = knownPosition.Yaw;
                        move.pitch    = knownPosition.Pitch;
                        move.headYaw  = knownPosition.HeadYaw;
                        move.mode     = 0;
                        byte[] bytes = move.Encode();
                        BatchUtils.WriteLength(stream, bytes.Length);
                        stream.Write(bytes, 0, bytes.Length);
                        move.PutPool();
                        playerMoveCount++;
                    }
                }

                foreach (var entity in entities)
                {
                    if (now - entity.LastUpdatedTime <= now - tickTime)
                    {
                        {
                            McpeMoveEntity moveEntity = McpeMoveEntity.CreateObject();
                            moveEntity.entityId    = entity.EntityId;
                            moveEntity.position    = (PlayerLocation)entity.KnownPosition.Clone();
                            moveEntity.position.Y += entity.PositionOffset;
                            byte[] bytes = moveEntity.Encode();
                            BatchUtils.WriteLength(stream, bytes.Length);
                            stream.Write(bytes, 0, bytes.Length);
                            moveEntity.PutPool();
                        }
                        {
                            McpeSetEntityMotion entityMotion = McpeSetEntityMotion.CreateObject();
                            entityMotion.entityId = entity.EntityId;
                            entityMotion.velocity = entity.Velocity;
                            byte[] bytes = entityMotion.Encode();
                            BatchUtils.WriteLength(stream, bytes.Length);
                            stream.Write(bytes, 0, bytes.Length);
                            entityMotion.PutPool();
                        }
                        entiyMoveCount++;
                    }
                }

                if (playerMoveCount == 0 && entiyMoveCount == 0)
                {
                    return;
                }

                if (players.Length == 1 && entiyMoveCount == 0)
                {
                    return;
                }

                McpeBatch batch = BatchUtils.CreateBatchPacket(stream.GetBuffer(), 0, (int)stream.Length, CompressionLevel.Optimal, false);
                batch.AddReferences(players.Length - 1);
                batch.Encode();
                //batch.ValidUntil = now + TimeSpan.FromMilliseconds(50);
                foreach (var player in players)
                {
                    MiNetServer.FastThreadPool.QueueUserWorkItem(() => player.SendPackage(batch));
                }
                _lastBroadcast = DateTime.UtcNow;
            }
        }