|
|
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.Networking;
-
-
- namespace Multiplayer
- {
-
- public class PlayersManager : MonoSingleton<PlayersManager>
- {
- [SerializeField]
- public GameObject LocalPlayerPrefab;
-
- [SerializeField]
- public GameObject RemotePlayerPrefab;
-
- public Dictionary<byte, GameObject> RemotePlayers = new Dictionary<byte, GameObject>();
- public GameObject LocalPlayer;
-
- private void Start()
- {
- OnGameStart();
- }
-
- public void OnGameStart()
- {
- ClientManager.Instance.Client.RegisterHandler(PlayerMsgID.Position, RecievePosition);
- InstantaiateLocalPlayer();
- }
-
- public void InstantaiateLocalPlayer()
- {
- LocalPlayer = GameObject.Instantiate(LocalPlayerPrefab,transform);
- LocalPlayer.transform.position = Vector3.zero + Vector3.up * 5.0f;
- ClientManager.Instance.SendMessage(PlayerMsgID.QueryPosition);
- }
-
- public void InstantiateRemotePlayer(byte playerID)
- {
- RemotePlayers.Add(playerID, Instantiate(RemotePlayerPrefab,transform));
- }
-
- public void SendPosition(Vector3 position) {
- ClientManager.Instance.SendMessage(PlayerMsgID.Position, new VectorMsg(ClientManager.Instance.ID, position));
- }
-
- public void RecievePosition(NetworkMessage msg)
- {
-
- VectorMsg vectorMsg;
- if (!Utility.ReadMessage<VectorMsg>(msg, out vectorMsg))
- return;
-
- if (!RemotePlayers.ContainsKey(vectorMsg.ID))
- InstantiateRemotePlayer(vectorMsg.ID);
-
- RemotePlayers[vectorMsg.ID].transform.position = vectorMsg.vector;
- }
-
- }
- }
|