Advertisement
Guest User

MultiplayerManager.cs

a guest
Jan 2nd, 2015
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. //credits; Klaus Joensuu
  2.  
  3. using UnityEngine;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6.  
  7.  
  8. public class MultiplayerManager : MonoBehaviour
  9. {
  10. public static MultiplayerManager instance;
  11.  
  12.  
  13. public string PlayerName;
  14.  
  15. private string MatchName = "";
  16. private string MatchPassword = "";
  17. private int MatchMaxUsers = 32;
  18.  
  19. public List<MPPlayer> PlayerList = new List<MPPlayer>();
  20. public List<MapSetting> MapList = new List<MapSetting>();
  21.  
  22. public MapSetting CurrentMap = null;
  23. public int oldprefix;
  24. public bool IsMatchStarted = false;
  25.  
  26. void Start()
  27. {
  28. instance = this;
  29. PlayerName = PlayerPrefs.GetString ("PlayerName");
  30. CurrentMap = MapList[0];
  31. }
  32.  
  33. void FixedUpdate()
  34. {
  35. instance = this;
  36. }
  37.  
  38. public void StartServer(string servername, string serverpassword, int maxusers)
  39. {
  40. MatchName = servername;
  41. MatchPassword = serverpassword;
  42. MatchMaxUsers = maxusers;
  43. Network.InitializeServer (MatchMaxUsers, 25565, false);
  44. MasterServer.RegisterHost ("RecoilCombat", MatchName, "");
  45. //Network.InitializeSecurity ();
  46. }
  47.  
  48. void OnServerInitialized()
  49. {
  50. Server_PlayerJoinRequest (PlayerName, Network.player);
  51. }
  52.  
  53. void OnConnectedToServer()
  54. {
  55. networkView.RPC ("Server_PlayerJoinRequest", RPCMode.Server, PlayerName, Network.player);
  56. }
  57.  
  58. void OnPlayerDisconnected(NetworkPlayer id)
  59. {
  60. networkView.RPC ("Client_RemovePlayer", RPCMode.All, id);
  61. }
  62.  
  63. void OnPlayerConnected(NetworkPlayer player)
  64. {
  65. foreach(MPPlayer pl in PlayerList)
  66. {
  67. networkView.RPC("Client_AddPlayerToList", player, pl.PlayerName, pl.PlayerNetwork);
  68. }
  69. networkView.RPC ("Client_GetMultiplayerMatchSettings", player, CurrentMap.MapName, "", "");
  70. }
  71.  
  72.  
  73. [RPC]
  74. void Server_PlayerJoinRequest(string playername, NetworkPlayer view)
  75. {
  76. networkView.RPC ("Client_AddPlayerToList", RPCMode.All, playername, view);
  77. }
  78.  
  79.  
  80. [RPC]
  81. void Client_AddPlayerToList(string playername, NetworkPlayer view)
  82. {
  83. MPPlayer tempplayer = new MPPlayer ();
  84. tempplayer.PlayerName = playername;
  85. tempplayer.PlayerNetwork = view;
  86. PlayerList.Add (tempplayer);
  87. }
  88.  
  89. [RPC]
  90. void Client_RemovePlayer(NetworkPlayer view)
  91. {
  92. MPPlayer temppl = null;
  93. foreach (MPPlayer pl in PlayerList)
  94. {
  95. if (pl.PlayerNetwork == view)
  96. {
  97. temppl = pl;
  98. }
  99. }
  100. if (temppl != null)
  101. {
  102. PlayerList.Remove(temppl);
  103. }
  104. }
  105.  
  106. [RPC]
  107. void Client_GetMultiplayerMatchSettings(string map, string mode, string others)
  108. {
  109. CurrentMap = GetMap(map);
  110. }
  111.  
  112. public MapSetting GetMap(string name)
  113. {
  114. MapSetting get = null;
  115.  
  116. foreach(MapSetting st in MapList)
  117. {
  118. if (st.MapName == name)
  119. {
  120. get = st;
  121. break;
  122. }
  123. }
  124. return get;
  125. }
  126.  
  127. [RPC]
  128. void Client_LoadMultiplayerMap(string map, int prefix)
  129. {
  130. Network.SetLevelPrefix(prefix);
  131. Application.LoadLevel(map);
  132. }
  133. }
  134. [System.Serializable]
  135. public class MPPlayer
  136. {
  137. public string PlayerName = "";
  138. public NetworkPlayer PlayerNetwork;
  139.  
  140. }
  141.  
  142. [System.Serializable]
  143. public class MapSetting
  144. {
  145. public string MapName;
  146. public string MapLoadName;
  147. public Texture MapLoadTexture;
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement