Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Net;
- using System.Net.Sockets;
- using UnityEngine;
- using ServerData;
- using System;
- using System.Threading;
- using System.Collections.Generic;
- public class MPplayer
- {
- public string ID;
- public float x, y;
- public float gunRot;
- public int state;
- public GameObject character;
- public Vector2 lastPos;
- public MPplayer(string _ID, float _x, float _y)
- {
- ID = _ID;
- x = _x;
- y = _y;
- state = 0;
- }
- public void Update()
- {
- character.transform.position = new Vector2(x, y);
- }
- }
- public class Client : MonoBehaviour
- {
- public Socket master;
- public string name;
- public string ID;
- public UIHandler uihandler;
- public IPAddress ip;
- public bool isInit;
- private Packet lastMsg;
- private Packet currMsg;
- public List<MPplayer> players;
- public float health, ammo;
- public CharacterController cc;
- public GameObject prefab;
- public Vector2 lastPos;
- private void Awake()
- {
- uihandler = GetComponent<UIHandler>();
- players = new List<MPplayer>();
- Application.runInBackground = true;
- DontDestroyOnLoad(this);
- }
- private void Update()
- {
- if (uihandler.state == 0)
- {
- name = uihandler.clientName;
- }
- else if (uihandler.state == 2)
- {
- ip = IPAddress.Parse(uihandler.ip);
- master = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- IPEndPoint ipEnd = new IPEndPoint(ip, 1337);
- try
- {
- Debug.Log("connecting");
- master.Connect(ipEnd);
- uihandler.state = 3;
- }
- catch (SocketException ex)
- {
- uihandler.state = 0;
- Debug.LogError("Error with connection:");
- Debug.Log(ex.ToString());
- }
- }
- else if(uihandler.state == 3)
- {
- if (!isInit)
- {
- Thread data_in = new Thread(Data_IN);
- data_in.Start();
- isInit = true;
- }
- if(lastMsg != currMsg)
- {
- DataManager(currMsg);
- lastMsg = currMsg;
- }
- }
- else if(uihandler.state == 4)
- {
- float speed = 5;
- float vSpeed = Input.GetAxis("Vertical") * Time.deltaTime * speed;
- float hSpeed = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
- transform.Translate(hSpeed, vSpeed, 0);
- if(lastPos != new Vector2(transform.position.x, transform.position.y))
- {
- lastPos = new Vector2(transform.position.x, transform.position.y);
- Packet packet = new Packet(PacketType.PlayerPositionUpdate, ID, transform.position.x.ToString(), transform.position.y.ToString());
- master.Send(packet.toBytes());
- }
- }
- }
- private void Data_IN()
- {
- byte[] buffer;
- int readBytes;
- for (; ; )
- {
- try
- {
- buffer = new byte[master.SendBufferSize];
- readBytes = master.Receive(buffer);
- if (readBytes > 0)
- {
- currMsg = new Packet(buffer);
- }
- }
- catch (SocketException ex)
- {
- //TODO: jump over gracefully!
- Console.WriteLine("The server has disconnected");
- throw;
- }
- }
- }
- private void DataManager(Packet p)
- {
- Debug.Log("::>" + p.packetType);
- switch (p.packetType)
- {
- case PacketType.PlayerRegistration:
- this.ID = p.gData[0];
- Packet packet = new Packet(PacketType.PlayerRegistration, this.ID);
- master.Send(packet.toBytes());
- uihandler.state++;
- break;
- case PacketType.PlayerJoined:
- Debug.Log("Ow O");
- string ID = p.gData[0];
- float x = float.Parse(p.gData[1]);
- float y = float.Parse(p.gData[2]);
- GameObject newPlayer = Instantiate(prefab);
- newPlayer.transform.position = new Vector2(x, y);
- MPplayer mpPlayer = new MPplayer(ID, x, y);
- mpPlayer.character = newPlayer;
- players.Add(mpPlayer);
- break;
- case PacketType.PlayerPositionUpdate:
- int Index = players.FindIndex(i => i.ID == p.senderID);
- float px = float.Parse(p.gData[0]);
- float py = float.Parse(p.gData[1]);
- players[Index].x = px;
- players[Index].y = py;
- players[Index].Update();
- break;
- case PacketType.PlayerStatUpdate:
- if(p.gData[0] == this.ID)
- {
- health = float.Parse(p.gData[1]);
- ammo = float.Parse(p.gData[2]);
- }
- break;
- case PacketType.PlayerVisualUpdate:
- int index = players.FindIndex(i => i.ID == p.gData[0]);
- players[index].gunRot = float.Parse(p.gData[1]);
- break;
- default:
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement