示例#1
0
        /// <summary>
        /// Method responsible for execute the callback of on client connected handler.
        /// </summary>
        /// <param name="client">The client instance.</param>
        private void OnClientConnectedHandler(IClient client)
        {
            try
            {
                Console.WriteLine($"OnClientConnected: {client.Id}");

                using (var kingBuffer = new KingBufferBase())
                {
                    kingBuffer.WriteMessagePacket(MyPackets.PlayerPositionsArray);
                    kingBuffer.WriteInteger(_networkPlayersDictionary.Count);

                    foreach (var player in _networkPlayersDictionary)
                    {
                        kingBuffer.WriteInteger(player.Key.Id);

                        kingBuffer.WriteFloat(player.Value.X);
                        kingBuffer.WriteFloat(player.Value.Y);
                        kingBuffer.WriteFloat(player.Value.Z);
                    }

                    _server.SendMessage(client, kingBuffer);

                    if (!_networkPlayersDictionary.ContainsKey(client))
                    {
                        _networkPlayersDictionary.Add(client, new NetworkPlayer(client));
                    }

                    _networkPlayersDictionary[client].Moved = true;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error: {ex.Message}.");
            }
        }
示例#2
0
        /// <summary>
        /// This method is responsible for sychronize the players positions.
        /// </summary>
        public void SynchronizePlayersPositions()
        {
            try
            {
                var sendPosDict = new Dictionary <IClient, NetworkPlayer>(_networkPlayersDictionary);

                foreach (var sendToPlayer in sendPosDict)
                {
                    if (sendToPlayer.Value == null)
                    {
                        continue;
                    }

                    using (var kingBuffer = new KingBufferBase())
                    {
                        kingBuffer.WriteMessagePacket(MyPackets.PlayerPositionsArray);
                        kingBuffer.WriteInteger(sendPosDict.Count(c => c.Key.Id != sendToPlayer.Key.Id && c.Value.Moved));

                        int amountPlayersMoved = 0;

                        foreach (var posPlayers in sendPosDict)
                        {
                            if (sendToPlayer.Key.Id == posPlayers.Key.Id)
                            {
                                continue;
                            }

                            if (!posPlayers.Value.Moved)
                            {
                                continue;
                            }

                            kingBuffer.WriteInteger(posPlayers.Key.Id);

                            kingBuffer.WriteFloat(posPlayers.Value.X);
                            kingBuffer.WriteFloat(posPlayers.Value.Y);
                            kingBuffer.WriteFloat(posPlayers.Value.Z);

                            amountPlayersMoved++;
                        }

                        if (amountPlayersMoved > 0)
                        {
                            _server.SendMessage(sendToPlayer.Key, kingBuffer);
                        }
                    }
                }

                foreach (var player in _networkPlayersDictionary)
                {
                    player.Value.Moved = false;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error: {ex.Message}");
            }
        }