Advertisement
Guest User

Untitled

a guest
May 28th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.47 KB | None | 0 0
  1.         public Dictionary<int, Player> players = new Dictionary<int, Player>();
  2.         public Player[] playerList;
  3.  
  4.  
  5.     // EXAMPLE: editing the list
  6.         public void AcceptPlayer( Player player ) {
  7.             lock( playerListLock ) {
  8.                 lock( mapLock ) {
  9.                     if( map == null ) {
  10.                         LoadMap();
  11.                     }
  12.                 }
  13.                 players.Add( player.id, player );
  14.                 UpdatePlayerList();
  15.             }
  16.             // *snip*
  17.         }
  18.  
  19.  
  20.         // Cache the player list to an array (players -> playerList)
  21.         public void UpdatePlayerList() {
  22.             lock( playerListLock ) {
  23.                 Player[] newPlayerList = new Player[players.Count];
  24.                 int i = 0;
  25.                 foreach( Player player in players.Values ) {
  26.                     newPlayerList[i++] = player;
  27.                 }
  28.                 playerList = newPlayerList;
  29.             }
  30.         }
  31.  
  32.  
  33.     // EXAMPLE: fast, safe reading of the list
  34.         // Send a list of players to the specified new player
  35.         internal void SendPlayerList( Player player ) {
  36.             Player[] tempList = playerList;
  37.             for( int i = 0; i < tempList.Length; i++ ) {
  38.                 if( tempList[i] != null && tempList[i] != player && !tempList[i].isHidden ) {
  39.                     player.session.Send( PacketWriter.MakeAddEntity( tempList[i], tempList[i].pos ) );
  40.                 }
  41.             }
  42.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement