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.
 
 
 

62 lines
1.8 KiB

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;
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;
}
}
}