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.

74 lines
2.1 KiB

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Networking;
  5. namespace Multiplayer
  6. {
  7. public class PlayerServerManager
  8. {
  9. private ServerManager ServerManager;
  10. public PlayerServerManager(ServerManager ServerManager)
  11. {
  12. this.ServerManager = ServerManager;
  13. RegisterHandlers();
  14. }
  15. private void RegisterHandlers()
  16. {
  17. ServerManager.Server.RegisterHandler(PlayerMsgID.Position, RecievePosition);
  18. }
  19. private void RecievePosition(NetworkMessage msg)
  20. {
  21. VectorMsg vectorMsg;
  22. if (!Utility.ReadMessage<VectorMsg>(msg, out vectorMsg))
  23. return;
  24. if (!ServerManager.AllPlayers.ContainsKey(vectorMsg.ID))
  25. {
  26. Debug.Log("Recieved position from unknown player");
  27. return;
  28. }
  29. Player player = ServerManager.AllPlayers[vectorMsg.ID];
  30. player.Position = vectorMsg.vector;
  31. foreach(Player otherPlayer in ServerManager.AllPlayers.Values)
  32. {
  33. if (otherPlayer.Equals(player))
  34. continue;
  35. otherPlayer.Connection.Send(PlayerMsgID.Position, vectorMsg);
  36. }
  37. }
  38. private void QueryPosition(NetworkMessage msg)
  39. {
  40. PlayerMsg playerMsg;
  41. if (!Utility.ReadMessage<PlayerMsg>(msg, out playerMsg))
  42. return;
  43. if (!ServerManager.AllPlayers.ContainsKey(playerMsg.ID))
  44. {
  45. Debug.Log("Recieved query from unknown player");
  46. return;
  47. }
  48. Player player = ServerManager.AllPlayers[playerMsg.ID];
  49. foreach (Player otherPlayer in ServerManager.AllPlayers.Values)
  50. {
  51. if (otherPlayer.Equals(player))
  52. continue;
  53. VectorMsg vectorMsg = new VectorMsg(otherPlayer.ID, otherPlayer.Position);
  54. player.Connection.Send(PlayerMsgID.Position, vectorMsg);
  55. }
  56. }
  57. }
  58. }