Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using System.Text;
- using UnityEngine;
- using UnityEngine.Networking;
- using UnityEngine.UI;
- public class Player
- {
- public string playerName;
- public GameObject avatar;
- public int connectionId;
- }
- public class NetworkManagerClient : MonoBehaviour
- {
- const int MAX_CONNECTIONS = 20;
- int hostId;
- int ourClientId;
- int connectionId;
- int myReliableChannelId;
- int myUnreliableChannelId;
- int port = 5701;
- float connectionTime;
- string playerName;
- bool isConnected = false;
- bool isStarted = false;
- byte error;
- public GameObject playerPrefab;
- Dictionary<int, Player> players = new Dictionary<int, Player>();
- // Use this for initialization
- public void Connect()
- {
- string pName = GameObject.Find("ClientName").GetComponent<InputField>().text;
- if (pName == "")
- {
- Debug.Log("Player must enter a name!");
- return;
- }
- playerName = pName;
- NetworkTransport.Init();
- ConnectionConfig config = new ConnectionConfig();
- myReliableChannelId = config.AddChannel(QosType.Reliable);
- myUnreliableChannelId = config.AddChannel(QosType.Unreliable);
- HostTopology topology = new HostTopology(config, MAX_CONNECTIONS);
- hostId = NetworkTransport.AddHost(topology, 0);
- connectionId = NetworkTransport.Connect(hostId, "127.0.0.1", port, 0, out error);
- connectionTime = Time.time;
- isConnected = true;
- }
- void Update()
- {
- if (!isConnected)
- return;
- int recHostId;
- int connectionId;
- int channelId;
- byte[] recBuffer = new byte[1024];
- int bufferSize = 1024;
- int dataSize;
- byte error;
- NetworkEventType recData = NetworkTransport.Receive(out recHostId, out connectionId, out channelId, recBuffer, bufferSize, out dataSize, out error);
- switch (recData)
- {
- case NetworkEventType.DataEvent:
- string msg = Encoding.Unicode.GetString(recBuffer, 0, dataSize);
- Debug.Log("Receiving: " + msg);
- string[] splitData = msg.Split('|');
- switch (splitData[0])
- {
- case "ASKNAME":
- OnAskName(splitData);
- break;
- case "CNN":
- SpawnPlayer(splitData[1], int.Parse(splitData[2]));
- break;
- case "DC":
- PlayerDisconnected(int.Parse(splitData[1]));
- break;
- case "ASKPOSITION":
- OnAskPosition(splitData);
- break;
- default:
- Debug.Log("Invalid message: " + msg);
- break;
- }
- break;
- }
- }
- void OnAskName(string[] data)
- {
- //Set client's ID
- ourClientId = int.Parse(data[1]);
- //send our name to the server
- Send("NAMEIS|" + playerName, myReliableChannelId);
- //create all the other players
- for (int i = 2; i < data.Length - 1; i++)
- {
- string[] d = data[i].Split('%');
- SpawnPlayer(d[0], int.Parse(d[1]));
- }
- }
- void SpawnPlayer(string playerName, int cnnId)
- {
- GameObject go = Instantiate(playerPrefab) as GameObject;
- //Is this ours?
- if (cnnId == ourClientId)
- {
- //Add mobility
- go.AddComponent<PlayerMovement>();
- //Remove Canvas
- isStarted = true;
- GameObject.Find("Canvas").SetActive(false);
- }
- Player p = new Player();
- p.avatar = go;
- p.playerName = playerName;
- p.connectionId = cnnId;
- p.avatar.GetComponentInChildren<TextMesh>().text = playerName;
- players.Add(cnnId, p);
- }
- void Send(string message, int channelId)
- {
- Debug.Log("Sending: " + message);
- byte[] msg = Encoding.Unicode.GetBytes(message);
- NetworkTransport.Send(hostId, connectionId, channelId, msg, message.Length * sizeof(char), out error);
- }
- private void PlayerDisconnected(int cnnId)
- {
- Destroy(players[cnnId].avatar);
- players.Remove(cnnId);
- }
- void OnAskPosition(string[] data)
- {
- if (!isStarted)
- return;
- //Update everyone else
- for (int i = 1; i < data.Length; i++)
- {
- string[] d = data[i].Split('%');
- //Prevent the server form updating us
- if (ourClientId != int.Parse(d[0]))
- {
- Vector3 position = Vector3.zero;
- position.x = float.Parse(d[1]);
- position.y = float.Parse(d[2]);
- //position.z = float.Parse(d[3]);
- players[int.Parse(d[0])].avatar.transform.position = position;
- }
- }
- //Send our own position
- Vector3 myPosition = players[ourClientId].avatar.transform.position;
- string m = "MYPOSITION|" + myPosition.x.ToString() + "|" + myPosition.y.ToString();
- Send(m, myUnreliableChannelId);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement