コード例 #1
0
        //This function will manage all the movement and coordination across players (notifies other players if someone has moved, to reflect that and stuff)
        private void updateMovements()
        {
            //First find the player who made the movement (Querying through all your players is not the most efficient way, I'll fix this later when i get a good base, remind me ok?)
            Character changedplayer = ConnectedPlayers.Where(f => f.Connection.RemoteUniqueIdentifier == inc.SenderConnection.RemoteUniqueIdentifier).FirstOrDefault();

            //This is interesting for you to look at. I currently only send the updated movement to all players in the same zone HOWEVER
            //If you only want to update players in a specific range you can perform some square root math function to get the distance between each player and
            //add an if statement to only send it to players who are in x range. so.. (if (range > 20) -> abort) I can help with that later if you want, remind me.

            changedplayer.Coordinates = new Vector3(inc.ReadFloat(), inc.ReadFloat());

            foreach (Character ch in ConnectedPlayers.Where(f => f.CurrentZone == changedplayer.CurrentZone))
            {
                //Don't send the coordinates to yourself, that would be silly unless you want your own movement to be server sided as well
                if (ch.Connection.RemoteUniqueIdentifier == inc.SenderConnection.RemoteUniqueIdentifier)
                {
                    continue;
                }

                NetOutgoingMessage outmsg = Server.CreateMessage();
                outmsg.Write((byte)PacketTypes.MOVEMENTUPDATE);

                //Send player so players know whos' coordinates to update. You can just send the entire class but that's just so unnecessary..
                outmsg.Write(changedplayer.Name); //Normally you want to give each player a unique ID (int), lighter to transport and other various reasons. Remind me
                outmsg.Write(changedplayer.Coordinates.X);
                outmsg.Write(changedplayer.Coordinates.Y);
                Server.SendMessage(outmsg, ch.Connection, NetDeliveryMethod.ReliableOrdered, 0);
                break;
            }
        }
コード例 #2
0
        public bool GetRandomAvailablePlayer(string nick, out string otherNick)
        {
            var list = ConnectedPlayers.Where(pair => pair.Key != nick && pair.Value.IsPlaying == false).Select(pair => pair.Key).ToList();

            if (list.Count < 1)
            {
                otherNick = "";
                return(false);
            }

            otherNick = list[_availablePlayerRandom.Next(list.Count)];
            while (otherNick == nick)
            {
                otherNick = list[_availablePlayerRandom.Next(list.Count)];
            }

            return(true);
        }
コード例 #3
0
 //Removes players from our list of connections
 private void removeDisconnectedUser(NetConnection conn)
 {
     ConnectedPlayers.Remove(ConnectedPlayers.Where(f => f.Connection.RemoteUniqueIdentifier == conn.RemoteUniqueIdentifier).FirstOrDefault());
 }
コード例 #4
0
        private void newLogin()
        {
            //When a player logs in, we expect that he sends his character info to us.
            //Normally he would only send his username and password but because we aren't using a database model
            //We'll just create the users character in their own client. Maybe database example next time
            //FYI: The connecting player can manually specify the zone he wants to connect to, easily changed by just setting the property though

            //Create new instance of player then read all the properties the client has sent into this class
            Character newlyConnectedPlayer = new Character();

            inc.ReadAllProperties(newlyConnectedPlayer);

            //If the player that is trying to connects' name already exists in our active player list
            //We will refuse his connection. This can be handy, if you want to allow only one character (obviously) or one
            //connection per IP Address, easy to modify
            if (ConnectedPlayers.FirstOrDefault(f => f.Name == newlyConnectedPlayer.Name) != null)
            {
                inc.SenderConnection.Deny("You're already connected");
                Write("Refused player with name " + newlyConnectedPlayer.Name + " because he was already connected.."); // LET IT BE KNOWN!
                return;
            }

            //We give our player a connection property so we can keep connection info about him, like IP Address/ping, etc!
            newlyConnectedPlayer.Connection = inc.SenderConnection;

            //Let it be known that this player has connected to our wonderful game
            Write(newlyConnectedPlayer.Name + " has connected!");

            //Approve this fine gentleman into our secret society
            inc.SenderConnection.Approve();

            //Add this fine lad to the list of connected players
            ConnectedPlayers.Add(newlyConnectedPlayer);


            //Now it gets a little more interesting!
            //*~*~*~*~* (SPARKLES TO MAKE IT MORE MAGICAL!) *~*~*~*~*

            //Worldstate (Can specify a name yourself) messages send the current zone state to players

            NetOutgoingMessage outmsg = Server.CreateMessage(); //We create a new message

            outmsg.Write((byte)PacketTypes.WORLDSTATE);         // Of type WORLDSTATE

            //Get the amount of players the zone the new player is in
            outmsg.Write(ConnectedPlayers.Where(f => f.CurrentZone == newlyConnectedPlayer.CurrentZone).Count()); //notice 'Count'

            //For each player in this players' zone, send him the data of the players. (The players' client will process this info and draw them on his screen)
            foreach (Character ch in ConnectedPlayers.Where(x => x.CurrentZone == newlyConnectedPlayer.CurrentZone))
            {
                outmsg.WriteAllProperties(ch);
            }
            Server.SendMessage(outmsg, inc.SenderConnection, NetDeliveryMethod.ReliableOrdered, 0); // Send the message, reliably and ordered

            // LET IT BE KNOWN!
            Write(String.Format("{0} : {1}  - has connected to the server and is in {2}", newlyConnectedPlayer.Name,
                                newlyConnectedPlayer.Connection.RemoteEndpoint.Address,
                                newlyConnectedPlayer.CurrentZone));

            //Update our UI
            updateList();
        }