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.
 
 
 

63 lines
1.8 KiB

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