using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Networking; namespace Multiplayer { public class PlayersManager : MonoSingleton { [SerializeField] public GameObject LocalPlayerPrefab; [SerializeField] public GameObject RemotePlayerPrefab; public Dictionary RemotePlayers = new Dictionary(); 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; 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(msg, out vectorMsg)) return; if (!RemotePlayers.ContainsKey(vectorMsg.ID)) InstantiateRemotePlayer(vectorMsg.ID); RemotePlayers[vectorMsg.ID].transform.position = vectorMsg.vector; } } }