Advertisement
Statement

Hackish Multiplayer

Apr 15th, 2011
618
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.38 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerController : MonoBehaviour
  6. {
  7.     public LayerMask blockLayer = 1;
  8.     public float range = Mathf.Infinity;
  9.  
  10.     RaycastHit hit;
  11.     float mouseX = 0;
  12.     float mouseY = 0;
  13.     Action guiState;
  14.     GameObject[, ,] level = new GameObject[256, 256, 256];
  15.     string chatString = string.Empty;
  16.     List<string> chatMessages = new List<string>(10);
  17.     List<KeyValuePair<string, object>> rpcQueue = new List<KeyValuePair<string, object>>();
  18.  
  19.     float sensitivity = 1.0f;
  20.     void Start()
  21.     {
  22.         Application.runInBackground = true;
  23.  
  24.         transform.position = new Vector3(128, 128, 120);
  25.         Action game = () =>
  26.         {
  27.             GUI.color = Color.white;
  28.             foreach (var item in chatMessages)
  29.                 GUILayout.Label(item);
  30.             if (!Screen.lockCursor)
  31.             {
  32.                 chatString = GUILayout.TextField(chatString);
  33.                 if (GUILayout.Button("Send"))
  34.                 {
  35.                     RPCChat(chatString);
  36.                     chatString = string.Empty;
  37.                 }
  38.                 sensitivity = GUILayout.VerticalSlider(sensitivity, 0, 1);
  39.  
  40.             }
  41.             GUI.color = new Color(1, 1, 1, 0.25f);
  42.             GUI.Box(new Rect(Screen.width / 2 - 4, Screen.height / 2 - 4, 8, 8), GUIContent.none);
  43.         };
  44.  
  45.         string addr = "127.0.0.1";
  46.         Action menu = () =>
  47.         {
  48.             Rect wnd = new Rect(Screen.width / 2 - 75, Screen.height / 2 - 64, 150, 128);
  49.             GUILayout.BeginArea(wnd);
  50.             addr = GUILayout.TextField(addr, GUILayout.ExpandWidth(true));
  51.             if (GUILayout.Button("Join Game", GUILayout.ExpandHeight(true)))
  52.             {
  53.                 Network.Connect(addr, 48484);
  54.                 guiState = game;
  55.             }
  56.             if (GUILayout.Button("Host Game", GUILayout.ExpandHeight(true)))
  57.             {
  58.                 Network.InitializeServer(32, 48484, !Network.HavePublicAddress());
  59.                 guiState = game;
  60.                 RPCBuild(new Vector3(128, 128, 128));
  61.             }
  62.             GUILayout.EndArea();
  63.         };
  64.  
  65.         guiState = menu;
  66.     }
  67.  
  68.     void OnPlayerDisconnected(NetworkPlayer player)
  69.     {
  70.         if (Network.isServer)
  71.         {
  72.             EvtChat("@ Player Disconnected");
  73.         }
  74.     }
  75.  
  76.     void OnPlayerConnected(NetworkPlayer player)
  77.     {
  78.         if (Network.isServer)
  79.         {
  80.             foreach (var rpc in rpcQueue)
  81.             {
  82.                 networkView.RPC(rpc.Key, player, rpc.Value);
  83.             }
  84.             networkView.RPC("EvtChat", player, "Press Left Ctrl to switch play/chat.");
  85.             EvtChat("@ Player Connected");
  86.         }
  87.     }
  88.  
  89.     private void RPCBuild(Vector3 position)
  90.     {
  91.         networkView.RPC("EvtBuild", RPCMode.All, position);
  92.     }
  93.  
  94.     private void RPCErase(Vector3 position)
  95.     {
  96.         if (position != new Vector3(128, 128, 128)) // GOD BLOCK
  97.             networkView.RPC("EvtErase", RPCMode.All, position);
  98.     }
  99.  
  100.     private void RPCChat(string message)
  101.     {
  102.         networkView.RPC("EvtChat", RPCMode.All, message);
  103.     }
  104.  
  105.     void Update()
  106.     {
  107.         if (Input.GetKeyDown(KeyCode.LeftControl))
  108.             Screen.lockCursor = !Screen.lockCursor;
  109.  
  110.         if (Input.GetKeyDown(KeyCode.Return) && !string.IsNullOrEmpty(chatString))
  111.         {
  112.             RPCChat(chatString);
  113.             chatString = string.Empty;
  114.         }
  115.  
  116.         if (!Screen.lockCursor)
  117.             return;
  118.  
  119.         transform.Translate(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
  120.         mouseX += Input.GetAxis("Mouse X") * 10.0f * sensitivity;
  121.         mouseY = Mathf.Clamp(mouseY + Input.GetAxis("Mouse Y") * 10.0f * sensitivity, -90, 90);
  122.         transform.rotation = Quaternion.Euler(-mouseY, mouseX, 0);
  123.        
  124.         if (Input.GetMouseButtonDown(0) && HitBlock())
  125.             RPCBuild(hit.transform.position + hit.normal);
  126.         if (Input.GetMouseButtonDown(1) && HitBlock())
  127.             RPCErase(hit.transform.position);
  128.     }
  129.  
  130.  
  131.     [RPC]
  132.     void EvtBuild(Vector3 position)
  133.     {
  134.         if (Network.isServer)
  135.             rpcQueue.Add(new KeyValuePair<string, object>("EvtBuild", position));
  136.  
  137.         var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
  138.         cube.transform.position = position;
  139.         int x = Mathf.RoundToInt(position.x);
  140.         int y = Mathf.RoundToInt(position.y);
  141.         int z = Mathf.RoundToInt(position.z);
  142.         level[x, y, z] = cube;
  143.     }
  144.  
  145.     [RPC]
  146.     void EvtErase(Vector3 position)
  147.     {
  148.         if (Network.isServer)
  149.             rpcQueue.Add(new KeyValuePair<string, object>("EvtErase", position));
  150.  
  151.         int x = Mathf.RoundToInt(position.x);
  152.         int y = Mathf.RoundToInt(position.y);
  153.         int z = Mathf.RoundToInt(position.z);
  154.         Destroy(level[x, y, z]);
  155.     }
  156.  
  157.     [RPC]
  158.     void EvtChat(string message)
  159.     {
  160.         if (Network.isServer)
  161.             rpcQueue.Add(new KeyValuePair<string, object>("EvtChat", message));
  162.  
  163.         chatMessages.Add(message);
  164.         if (chatMessages.Count > 10)
  165.             chatMessages.RemoveAt(0);
  166.     }
  167.  
  168.     bool HitBlock()
  169.     {
  170.         return Physics.Raycast(transform.position, transform.forward, out hit, range, blockLayer);
  171.     }
  172.  
  173.     void OnGUI()
  174.     {
  175.         if (guiState != null)
  176.             guiState();
  177.     }
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement