Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections.Generic;
- using UnityEngine.UI;
- using System.Collections;
- using System;
- namespace Client
- {
- public class ClientHandle : MonoBehaviour
- {
- public static ClientHandle instance;
- private delegate void NetPackets(byte[] data);
- private static Dictionary<int, NetPackets> _packets;
- [Header("Scripts")]
- public MenuManager menuManager;
- [Header("Player Prefab")]
- public GameObject _playerPref;
- public GameObject Char;
- public Button button;
- public static GameObject PlayerList;
- public void InitializeNetworkPackages()
- {
- //Debug.Log("Initialize Network Packages");
- _packets = new Dictionary<int, NetPackets>
- {
- //LIST OF SERVER PACKAGES
- { (int)Packets.Message, HandleMessage },
- { (int)Packets.Spawn, SpawnCharacter },
- { (int)Packets.Login_Accept, HandleLoginSucces },
- { (int)Packets.Movement, HandleClientMovement },
- { (int)Packets.TEST, HandleTEST },
- };
- }
- public void Awake()
- {
- instance = this;
- InitializeNetworkPackages();
- PlayerList = new GameObject("PlayerList");
- }
- public void HandleSocketData(byte[] data)
- {
- Buffer buffer = new Buffer();
- NetPackets _packet;
- buffer.WriteBytes(data);
- int packet = buffer.ReadInteger();
- buffer.Dispose();
- if (packet == 0)
- return;
- if (_packets.TryGetValue(packet, out _packet))
- {
- _packet.Invoke(data);
- }
- }
- public void HandleMessage(byte[] data)
- {
- Buffer buffer = new Buffer();
- buffer.WriteBytes(data);
- int packet = buffer.ReadInteger();
- string AlertMsg = buffer.ReadString();
- Debug.Log(AlertMsg);
- if (packet == 0)
- return;
- }
- public void HandleLoginSucces(byte[] data)
- {
- Buffer buffer = new Buffer();
- buffer.WriteBytes(data);
- int packet = buffer.ReadInteger();
- string msg = buffer.ReadString();
- Debug.Log(msg);
- MenuManager.instance.ChangeMenu(2);
- if (packet == 0)
- return;
- }
- public void HandleClientMovement(byte[] data)
- {
- Buffer buffer = new Buffer();
- buffer.WriteBytes(data);
- int packet = buffer.ReadInteger();
- // Reciving Player Information
- int index = buffer.ReadInteger();
- // Reciving Player Position
- float x = buffer.ReadFloat();
- float y = buffer.ReadFloat();
- float z = buffer.ReadFloat();
- // Reciving Player Rotation
- float rotX = buffer.ReadFloat();
- float rotY = buffer.ReadFloat();
- float rotZ = buffer.ReadFloat();
- float rotW = buffer.ReadFloat();
- // Find Player and move it.
- GameObject _netPlayer;
- if (_netPlayer = GameObject.Find("Player (" + index + ")"))
- {
- _netPlayer.GetComponent<Transform>().position = new Vector3(x, y, z);
- _netPlayer.GetComponent<Transform>().rotation = new Quaternion(rotX, rotY, rotZ, rotW);
- }
- if (packet == 0) return;
- }
- //public void HandleTEST(byte[] data)
- //{
- // int i = 0;
- // Buffer buffer = new Buffer();
- // buffer.WriteBytes(data);
- // int packet = buffer.ReadInteger();
- // Character[] charArray = new Character[5];
- // for (int j = 0; j < 5; j++)
- // {
- // charArray[j] = new Character();
- // // charArray[j].ID = 0;
- // }
- // while (buffer.Length() > 0)
- // {
- // Debug.Log("Aye");
- // charArray[i].ID = buffer.ReadInteger();
- // charArray[i].Name = buffer.ReadString();
- // i++;
- // }
- // for (int j = 0; j < 5; j++)
- // {
- // if (charArray[j].ID > 0)
- // {
- // Debug.Log("ID: " + charArray[j].ID + " Name: " + charArray[j].Name);
- // }
- // else
- // {
- // Debug.Log("No characters");
- // }
- // }
- // if (packet == 0) return;
- // }
- public void HandleTEST(byte[] data)
- {
- int i = 0;
- Buffer buffer = new Buffer();
- buffer.WriteBytes(data);
- int packet = buffer.ReadInteger();
- Character[] charArray = new Character[5];
- for (int j = 0; j < 5; j++)
- {
- charArray[j] = new Character();
- charArray[j].ID = 0;
- }
- Debug.Log("Before While");
- while (buffer.Length() > 0)
- {
- Debug.Log("In While" + buffer.GetReadPos());
- charArray[i].ID = buffer.ReadInteger();
- charArray[i].Name = buffer.ReadString();
- i++;
- Debug.Log("End While");
- }
- Debug.Log("After While");
- for (int j = 0; j < 5; j++)
- {
- if (charArray[j].ID >= 0)
- {
- Debug.Log("ID: " + charArray[j].ID + " Name: " + charArray[j].Name);
- }
- else
- {
- Debug.Log("No characters");
- }
- }
- if (packet == 0) return;
- }
- public void SpawnCharacter(byte[] data)
- {
- Buffer buffer = new Buffer();
- buffer.WriteBytes(data);
- int packet = buffer.ReadInteger();
- int PlayerID = buffer.ReadInteger();
- int CharID = buffer.ReadInteger();
- if (Globals.Client_ID <= 1) { Globals.Client_ID = PlayerID; }
- // MENU WINDOW DOWN
- MenuManager.instance.ChangeMenu(4);
- Character.character[PlayerID].ID = CharID;
- //General.instance.SetPlayerX(PlayerID, buffer.ReadFloat());
- //General.instance.SetPlayerY(PlayerID, buffer.ReadFloat());
- //General.instance.SetPlayerZ(PlayerID, buffer.ReadFloat());
- General.instance.SetVector3(PlayerID, buffer.ReadFloat(), buffer.ReadFloat(), buffer.ReadFloat());
- Character.character[PlayerID].Name = buffer.ReadString();
- PlayerInfo.instance.Name.text = Character.character[PlayerID].Name;
- Spawn(PlayerID);
- if (packet == 0) return;
- }
- void Spawn(int PlayerID)
- {
- // Instantiate the other players on their position.
- _playerPref = Instantiate(_playerPref, new Vector3(General.instance.GetPlayerX(PlayerID), General.instance.GetPlayerY(PlayerID), General.instance.GetPlayerZ(PlayerID)), Quaternion.identity);
- _playerPref.GetComponent<NetPlayer>().Index = PlayerID;
- _playerPref.name = "Player (" + PlayerID + ")";
- _playerPref.transform.parent = GameObject.Find("PlayerList").transform;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement