You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
3.2 KiB

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Networking;
  5. namespace Multiplayer
  6. {
  7. public class PlayersManager : MonoSingleton<PlayersManager>
  8. {
  9. [SerializeField]
  10. public GameObject LocalPlayerPrefab;
  11. [SerializeField]
  12. public GameObject RemotePlayerPrefab;
  13. public Dictionary<byte, PlayerData> Players = new Dictionary<byte, PlayerData>();
  14. public PlayerData LocalPlayer;
  15. private void Start()
  16. {
  17. RegisterHandlers();
  18. PlayerData localPlayer = new PlayerData(ClientManager.Instance.ID, ClientManager.Instance.Name, Color.white);
  19. localPlayer.isLocalPlayer = true;
  20. Players.Add(localPlayer.ID, localPlayer);
  21. LocalPlayer = localPlayer;
  22. }
  23. public void RegisterHandlers()
  24. {
  25. ClientManager.Instance.Client.RegisterHandler(PlayerMsgID.Position, RecievePosition);
  26. ClientManager.Instance.Client.RegisterHandler(LoginMsgID.RemotePlayerJoin, OnRemotePlayerJoin);
  27. ClientManager.Instance.Client.RegisterHandler(LoginMsgID.RemotePlayerExit, OnRemotePlayerLeave);
  28. }
  29. public void InstantiatePlayers()
  30. {
  31. foreach (PlayerData playerData in Players.Values)
  32. {
  33. if (playerData.isLocalPlayer)
  34. playerData.Instantiate(LocalPlayerPrefab, transform);
  35. else
  36. playerData.Instantiate(RemotePlayerPrefab, transform);
  37. }
  38. }
  39. public void DestroyPlayers()
  40. {
  41. foreach (PlayerData player in Players.Values)
  42. if (player.Player != null)
  43. Destroy(player.gameObject);
  44. }
  45. public void SendPosition(Vector3 position)
  46. {
  47. ClientManager.Instance.SendMessage(PlayerMsgID.Position, new VectorMsg(ClientManager.Instance.ID, position));
  48. }
  49. public void RecievePosition(NetworkMessage msg)
  50. {
  51. VectorMsg vectorMsg;
  52. if (!Utility.ReadMessage<VectorMsg>(msg, out vectorMsg))
  53. return;
  54. if (Players.ContainsKey(vectorMsg.ID))
  55. Players[vectorMsg.ID].SetPosition(vectorMsg.vector);
  56. }
  57. public void OnRemotePlayerJoin(NetworkMessage msg)
  58. {
  59. PlayerDataMsg playerMsg;
  60. if (!Utility.ReadMessage<PlayerDataMsg>(msg, out playerMsg))
  61. return;
  62. if (Players.ContainsKey(playerMsg.ID))
  63. Debug.Log(Players[playerMsg.ID].Name + ", trying to join twice");
  64. PlayerData newPlayer = new PlayerData(playerMsg.ID, playerMsg.Name, playerMsg.Color);
  65. Players.Add(playerMsg.ID, newPlayer);
  66. }
  67. public void OnRemotePlayerLeave(NetworkMessage msg)
  68. {
  69. PlayerMsg playerMsg;
  70. if (!Utility.ReadMessage<PlayerMsg>(msg, out playerMsg))
  71. return;
  72. if (!Players.ContainsKey(playerMsg.ID))
  73. Debug.Log("Unknown Player Left");
  74. PlayerData leftPlayer = Players[playerMsg.ID];
  75. Players.Remove(playerMsg.ID);
  76. if (leftPlayer.gameObject != null)
  77. Destroy(leftPlayer.gameObject);
  78. }
  79. }
  80. }