Advertisement
Guest User

Client

a guest
Dec 16th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.57 KB | None | 0 0
  1. using System.Net;
  2. using System.Net.Sockets;
  3. using UnityEngine;
  4. using ServerData;
  5. using System;
  6. using System.Threading;
  7. using System.Collections.Generic;
  8.  
  9. public class MPplayer
  10. {
  11.     public string ID;
  12.  
  13.     public float x, y;
  14.     public float gunRot;
  15.     public int state;
  16.     public GameObject character;
  17.  
  18.     public Vector2 lastPos;
  19.  
  20.     public MPplayer(string _ID, float _x, float _y)
  21.     {
  22.         ID = _ID;
  23.         x = _x;
  24.         y = _y;
  25.         state = 0;
  26.     }
  27.  
  28.     public void Update()
  29.     {
  30.         character.transform.position = new Vector2(x, y);
  31.     }
  32. }
  33.  
  34. public class Client : MonoBehaviour
  35. {
  36.     public Socket master;
  37.     public string name;
  38.     public string ID;
  39.     public UIHandler uihandler;
  40.     public IPAddress ip;
  41.     public bool isInit;
  42.  
  43.     private Packet lastMsg;
  44.     private Packet currMsg;
  45.  
  46.     public List<MPplayer> players;
  47.  
  48.     public float health, ammo;
  49.     public CharacterController cc;
  50.     public GameObject prefab;
  51.     public Vector2 lastPos;
  52.  
  53.     private void Awake()
  54.     {
  55.         uihandler = GetComponent<UIHandler>();
  56.         players = new List<MPplayer>();
  57.         Application.runInBackground = true;
  58.         DontDestroyOnLoad(this);
  59.     }
  60.  
  61.     private void Update()
  62.     {
  63.         if (uihandler.state == 0)
  64.         {
  65.             name = uihandler.clientName;
  66.         }
  67.         else if (uihandler.state == 2)
  68.         {
  69.             ip = IPAddress.Parse(uihandler.ip);
  70.             master = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  71.            
  72.             IPEndPoint ipEnd = new IPEndPoint(ip, 1337);
  73.  
  74.             try
  75.             {
  76.                 Debug.Log("connecting");
  77.                 master.Connect(ipEnd);
  78.  
  79.                 uihandler.state = 3;
  80.             }
  81.             catch (SocketException ex)
  82.             {
  83.                 uihandler.state = 0;
  84.                 Debug.LogError("Error with connection:");
  85.                 Debug.Log(ex.ToString());
  86.             }
  87.         }
  88.         else if(uihandler.state == 3)
  89.         {
  90.             if (!isInit)
  91.             {
  92.                 Thread data_in = new Thread(Data_IN);
  93.                 data_in.Start();
  94.                 isInit = true;
  95.             }
  96.  
  97.             if(lastMsg != currMsg)
  98.             {
  99.                 DataManager(currMsg);
  100.                 lastMsg = currMsg;
  101.                
  102.             }
  103.         }
  104.         else if(uihandler.state == 4)
  105.         {
  106.            
  107.             float speed = 5;
  108.  
  109.             float vSpeed = Input.GetAxis("Vertical") * Time.deltaTime * speed;
  110.             float hSpeed = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
  111.  
  112.             transform.Translate(hSpeed, vSpeed, 0);
  113.  
  114.             if(lastPos != new Vector2(transform.position.x, transform.position.y))
  115.             {
  116.                 lastPos = new Vector2(transform.position.x, transform.position.y);
  117.                 Packet packet = new Packet(PacketType.PlayerPositionUpdate, ID, transform.position.x.ToString(), transform.position.y.ToString());
  118.                 master.Send(packet.toBytes());
  119.             }
  120.         }
  121.  
  122.  
  123.     }
  124.  
  125.     private void Data_IN()
  126.     {
  127.         byte[] buffer;
  128.         int readBytes;
  129.         for (; ; )
  130.         {
  131.             try
  132.             {
  133.                 buffer = new byte[master.SendBufferSize];
  134.                 readBytes = master.Receive(buffer);
  135.  
  136.                 if (readBytes > 0)
  137.                 {
  138.                     currMsg = new Packet(buffer);
  139.                 }
  140.             }
  141.             catch (SocketException ex)
  142.             {
  143.  
  144.                 //TODO: jump over gracefully!
  145.                 Console.WriteLine("The server has disconnected");
  146.                 throw;
  147.             }
  148.         }
  149.     }
  150.  
  151.     private void DataManager(Packet p)
  152.     {
  153.         Debug.Log("::>" + p.packetType);
  154.         switch (p.packetType)
  155.         {
  156.             case PacketType.PlayerRegistration:
  157.                 this.ID = p.gData[0];
  158.  
  159.                 Packet packet = new Packet(PacketType.PlayerRegistration, this.ID);
  160.                 master.Send(packet.toBytes());
  161.                 uihandler.state++;
  162.                 break;
  163.  
  164.             case PacketType.PlayerJoined:
  165.                 Debug.Log("Ow O");
  166.                 string ID = p.gData[0];
  167.  
  168.                 float x = float.Parse(p.gData[1]);
  169.                 float y = float.Parse(p.gData[2]);
  170.  
  171.                 GameObject newPlayer = Instantiate(prefab);
  172.                 newPlayer.transform.position = new Vector2(x, y);
  173.  
  174.                 MPplayer mpPlayer = new MPplayer(ID, x, y);
  175.                 mpPlayer.character = newPlayer;
  176.                 players.Add(mpPlayer);
  177.                
  178.                 break;
  179.            
  180.             case PacketType.PlayerPositionUpdate:
  181.                 int Index = players.FindIndex(i => i.ID == p.senderID);
  182.  
  183.                 float px = float.Parse(p.gData[0]);
  184.                 float py = float.Parse(p.gData[1]);
  185.  
  186.                 players[Index].x = px;
  187.                 players[Index].y = py;
  188.  
  189.                 players[Index].Update();
  190.                 break;
  191.             case PacketType.PlayerStatUpdate:
  192.  
  193.                 if(p.gData[0] == this.ID)
  194.                 {
  195.                     health = float.Parse(p.gData[1]);
  196.                     ammo = float.Parse(p.gData[2]);
  197.                 }
  198.  
  199.                 break;
  200.             case PacketType.PlayerVisualUpdate:
  201.                 int index = players.FindIndex(i => i.ID == p.gData[0]);
  202.                 players[index].gunRot = float.Parse(p.gData[1]);
  203.  
  204.                 break;
  205.             default:
  206.                 break;
  207.         }
  208.     }
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement