SHARE
TWEET

suck suck suck that cock

a guest Dec 11th, 2015 243 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using UnityEngine;
  3. using System.Collections.Generic; its a thing obviously jeez
  4. using System.IO;
  5. using System.Linq;
  6. using System.Runtime.Serialization.Formatters.Binary;
  7.  
  8. public class MessageHandler
  9. {
  10.     List<byte> _message = new List<byte>();
  11.  
  12.     public MessageHandler()
  13.     {
  14.        
  15.     }
  16.  
  17.     public void HandleMessage(byte[] message)
  18.     {
  19.         Debug.Assert(message != null);
  20.         char messageType = (char)message[0];
  21.  
  22.         Debug.Log("Handling Message:" + messageType);
  23.         if (messageType == 'U')
  24.         {
  25.             HandleUpdateMessage(message);
  26.         }
  27.  
  28.         else if(messageType == 'C')
  29.         {
  30.             HandleCollisionMessage(message);
  31.         }
  32.         else if(messageType == 'S')
  33.         {
  34.             HandleSetupMessage(message);
  35.         }
  36.         else if (messageType == 'R')
  37.         {
  38.             HandleRequestSetupMessage();
  39.         }
  40.         else if(messageType == 'G')
  41.         {
  42.             HandleGameStartedMessage();
  43.         }
  44.  
  45.     }//end HandleMessage()
  46.  
  47.     public void HandleUpdateMessage(byte[] message)
  48.     {
  49.         if (GameObject.Find("Net").GetComponent<net_script>().otherPlayerInGame)
  50.         {
  51.             float posX = System.BitConverter.ToSingle(message, 1);//float.Parse(message[1]);
  52.             float posY = System.BitConverter.ToSingle(message, 5);
  53.  
  54.             if (GameObject.Find("Net").GetComponent<net_script>().IsHost())
  55.             {
  56.                 if (GameObject.Find("OtherPlayer"))
  57.                 {
  58.                     GameObject.Find("OtherPlayer").GetComponent<OtherPlayer>().Puppeteer(posX, posY);
  59.                 }
  60.             }
  61.             else
  62.             {
  63.                 if (GameObject.Find("Player"))
  64.                 {
  65.                     GameObject.Find("Player").GetComponent<Player>().Puppeteer(posX, posY);
  66.                 }
  67.             }
  68.         }
  69.     }//end HandleUpdateMessage()
  70.  
  71.     private void HandleCollisionMessage(byte[] message)
  72.     {
  73.         GameObject.Find("Main Camera").GetComponent<Main>().Catch();
  74.     }
  75.  
  76.     private void HandleRequestSetupMessage()
  77.     {
  78.         net_script net = GameObject.Find("Net").GetComponent<net_script>();
  79.         Main main = GameObject.Find("Main Camera").GetComponent<Main>();
  80.         Vector2 playerPos = GameObject.Find("Player").transform.position;
  81.         Vector2 otherPlayerPos = GameObject.Find("OtherPlayer").transform.position;
  82.  
  83.         _message.Clear();
  84.         _message.Add((byte)'S');
  85.         _message.AddRange(BitConverter.GetBytes(main.roleConfig));
  86.  
  87.         //serialize the player
  88.         _message.AddRange(BitConverter.GetBytes(playerPos.x));
  89.         _message.AddRange(BitConverter.GetBytes(playerPos.y));
  90.         _message.AddRange(BitConverter.GetBytes(otherPlayerPos.x));
  91.         _message.AddRange(BitConverter.GetBytes(otherPlayerPos.y));
  92.  
  93.         byte[] messageToSend = _message.ToArray();
  94.  
  95.         byte[] buffer = new byte[1024];
  96.         Stream stream = new MemoryStream(buffer);
  97.         BinaryFormatter formatter = new BinaryFormatter();
  98.         formatter.Serialize(stream, messageToSend);
  99.         net.SendSerializedMessage(buffer);
  100.     }
  101.  
  102.     private void HandleSetupMessage(byte[] message)
  103.     {
  104.         Main main = GameObject.Find("Main Camera").GetComponent<Main>();
  105.         Vector2 playerPos = new Vector2();
  106.         Vector2 otherPlayerPos = new Vector2();
  107.  
  108.         main.PickRoles((int)System.BitConverter.ToSingle(message, 1));
  109.  
  110.         playerPos.x = System.BitConverter.ToSingle(message, 5);
  111.         playerPos.y = System.BitConverter.ToSingle(message, 9);
  112.         GameObject.Find("Player").transform.position = playerPos;
  113.  
  114.         otherPlayerPos.x = System.BitConverter.ToSingle(message, 13);
  115.         otherPlayerPos.y = System.BitConverter.ToSingle(message, 17);
  116.         GameObject.Find("OtherPlayer").transform.position = otherPlayerPos;
  117.         GameObject.Find("Net").GetComponent<net_script>().otherPlayerInGame = true;
  118.         GameObject.Find("Net").GetComponent<net_script>().SendSocketMessage("GameStarted");
  119.     }
  120.  
  121.     private void HandleGameStartedMessage()
  122.     {
  123.         GameObject.Find("Net").GetComponent<net_script>().otherPlayerInGame = true;
  124.     }
  125. }//end class MessageHandler
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top