using UnityEngine;
using System.Collections;
using System.IO;
using System.Net;
using TNet;
public class cTest : MonoBehaviour {
public string conso;
public string adress;
public string port;
public GameObject player;
public int games;
public int[] pCount
= new int[10];
// Use this for initialization
void Start () {
/*if(Application.isPlaying){
TNManager.StartUDP(Random.Range(100,50000));
}*/
InvokeRepeating("Refresh",2,2);
}
// Update is called once per frame
void Update () {
}
void OnGUI(){
GUILayout.Label("Connection status: " +TNManager.isConnected);
adress = GUILayout.TextField(adress);
port = GUILayout.TextField(port);
//This is where I connect
if(GUILayout.Button("Connect"+conso)){
TNManager.Connect(adress,int.Parse(port));
}
if(GUILayout.Button("Join Channel")){
TNManager.JoinChannel(1,"game");
}
if(TNManager.isConnected){
if(TNManager.isInChannel){
if(GUILayout.Button("Destroy")){
GameObject[] g;
g = GameObject.FindGameObjectsWithTag("Player");
for(int s = 0; s< g.Length; s++){
TNManager.Destroy(g[s]);
}
}
if(GUILayout.Button("Spawn")){
TNManager.Create(player);
}
}
if(GUILayout.Button("Get Channel List")){
TNManager.client.BeginSend(Packet.RequestChannelList);
TNManager.client.EndSend();
}
if(GUILayout.Button("Join Channel2")){
TNManager.JoinChannel(2,"game");
}
GUILayout
.BeginArea(new Rect
(Screen
.width/2,
0,Screen
.width/2,Screen
.height)); for(int g=0;g<games;g++){
GUILayout.BeginHorizontal();
if(GUILayout.Button("Game: "+(g+1))){
TNManager.JoinChannel(g+1,"game");
}
GUILayout.Label("Players: "+pCount[g]);
GUILayout.EndHorizontal();
}
GUILayout.EndArea();
if(TNManager.isInChannel){
if(GUILayout.Button("Leave Channel")){
TNManager.LeaveChannel();
Application.LoadLevel("menu");
}
}
}
/*if(GUILayout.Button("Get Channel List")){
}*/
}
void OnNetworkConnect (bool success, string message) {
print(success+ message);
conso=""+success;
TNManager.client.packetHandlers[(byte)Packet.ResponseChannelList] = OnChannelList;
}
void OnNetworkError (string message){
conso = message;
}
void OnNetworkJoinChannel (bool success,string message){
conso = message;
print ("Joined channel");
TNManager.Create(player);
}
//#############################
void OnChannelList (Packet response, BinaryReader reader, IPEndPoint source)
{
int count = reader.ReadInt32();
//System.Array.Resize(ref pCount, 0);
for (int i = 0; i < count; ++i)
{
int channelID = reader.ReadInt32();
int playerCount = reader.ReadInt32();
bool password = reader.ReadBoolean();
bool isPersistent = reader.ReadBoolean();
string level = reader.ReadString();
//conso = playerCount+" "+level+" " +channelID;
games = count;
System.Array.Resize(ref pCount, count);
pCount[i] = playerCount;
print("channel ID: "+channelID+" Player Count: "+playerCount+" level: "+ level);
}
}
void Refresh(){
TNManager.client.BeginSend(Packet.RequestChannelList);
TNManager.client.EndSend();
}
}