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.

50 lines
1.3 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. }
  39. }