Exemplo n.º 1
0
        /// <summary>
        /// Prepares and sends all pending outgoing messages.
        /// </summary>
        private static void handleOutgoingMessages()
        {
            // Send updates 30 times per second.
            now = NetTime.Now;
            if (now > nextSendUpdates)
            {
                //log.Info("SENDING UPDATES!");
                // Prepare a player position packet of every player with a 'dirty' position.
                int playerCount;
                List<long> RUIList;
                List<Vector2> positionList;
                playerManager.GetPositions(out playerCount, out RUIList, out positionList);
                S_PlayerPositionMessage playerPositionMessage = new S_PlayerPositionMessage()
                {
                    PlayerCount = playerCount,
                    RUIList = RUIList,
                    PositionList = positionList
                };

                if (playerCount > 0)
                {
                    //Console.WriteLine(playerPositionMessage.ToString());
                    // There are updates to send, so send them.
                    NetOutgoingMessage om = server.CreateMessage();
                    playerPositionMessage.Write(om);

                    // Send to each connection.
                    try
                    {
                        server.SendToAll(om, NetDeliveryMethod.Unreliable);
                    }
                    catch (NetException e)
                    {

                    }
                }

                nextSendUpdates += (1.0 / updatesPerSecond);
            }
        }
Exemplo n.º 2
0
        private void handleDataMessage(NetIncomingMessage msg)
        {
            //log.Info("Got data message: {0}", msg);

            MessageType type = (MessageType)msg.ReadByte();
            switch (type)
            {
                case MessageType.PlayerPosition:
                    // Batch player position message.
                    S_PlayerPositionMessage playerPositionMessage = new S_PlayerPositionMessage()
                    {
                        RUIList = new List<long>(),
                        PositionList = new List<Vector2>()
                    };
                    playerPositionMessage.Read(msg);
                    playerManager.UpdatePositions(playerPositionMessage.PlayerCount,
                        playerPositionMessage.RUIList, playerPositionMessage.PositionList);
                    break;
                case MessageType.PlayerJoin:
                    // A new player has joined, so add it to the local dictionary.
                    S_PlayerJoinMessage playerJoinMessage = new S_PlayerJoinMessage();
                    playerJoinMessage.Read(msg);
                    PlayerManager.Add(playerJoinMessage.PlayerRUI,
                        new Player(playerJoinMessage.PlayerRUI, Resources.playerTexture, playerJoinMessage.Color));
                    break;
                case MessageType.PlayerList:
                    S_PlayerListMessage playerListMessage = new S_PlayerListMessage();
                    playerListMessage.playerTexture = Resources.playerTexture;
                    playerListMessage.Read(msg);
                    log.Info("playersCount: {0}", playerListMessage.PlayerCount);
                    playerManager.AddAll(playerListMessage.Players);
                    // Find and set the local player from the players list.
                    if (!playerManager.Players.TryGetValue(client.UniqueIdentifier, out localPlayer))
                    {
                        // This should never happen.
                        log.Error("Unable to find local player in player list");
                        client.Disconnect("Error");
                    }
                    localPlayer.Sprite = new Sprite(Resources.playerTexture);
                    break;
                default:
                    log.Error("Unknown message type: {0}", type);
                    break;
            }
        }