using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Networking; namespace Multiplayer { public class PlayersManager : MonoSingleton { public GameObject LocalPlayerPrefab; public GameObject RemotePlayerPrefab; public Vector3 mapTop; public Vector3 mapBot; private Dictionary RemotePlayers = new Dictionary(); private GameObject PlayerObject; private void Start() { OnGameStart(); } public void OnGameStart() { ClientManager.Instance.Client.RegisterHandler(PlayerMsgID.Position, RecievePosition); InstantaiateLocalPlayer(); } public void InstantaiateLocalPlayer() { Vector3 startPos = new Vector3(Random.Range(mapBot.x, mapTop.x), Random.Range(mapBot.y, mapTop.y), Random.Range(mapBot.z, mapTop.z)); PlayerObject = GameObject.Instantiate(LocalPlayerPrefab); PlayerObject.transform.position = startPos; } public void InstantiateRemotePlayer(byte playerID) { RemotePlayers.Add(playerID, Instantiate(RemotePlayerPrefab)); } 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; } } }