Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using UnityEngine;
- public class PlayerController : MonoBehaviour
- {
- public LayerMask blockLayer = 1;
- public float range = Mathf.Infinity;
- RaycastHit hit;
- float mouseX = 0;
- float mouseY = 0;
- Action guiState;
- GameObject[, ,] level = new GameObject[256, 256, 256];
- string chatString = string.Empty;
- List<string> chatMessages = new List<string>(10);
- List<KeyValuePair<string, object>> rpcQueue = new List<KeyValuePair<string, object>>();
- float sensitivity = 1.0f;
- void Start()
- {
- Application.runInBackground = true;
- transform.position = new Vector3(128, 128, 120);
- Action game = () =>
- {
- GUI.color = Color.white;
- foreach (var item in chatMessages)
- GUILayout.Label(item);
- if (!Screen.lockCursor)
- {
- chatString = GUILayout.TextField(chatString);
- if (GUILayout.Button("Send"))
- {
- RPCChat(chatString);
- chatString = string.Empty;
- }
- sensitivity = GUILayout.VerticalSlider(sensitivity, 0, 1);
- }
- GUI.color = new Color(1, 1, 1, 0.25f);
- GUI.Box(new Rect(Screen.width / 2 - 4, Screen.height / 2 - 4, 8, 8), GUIContent.none);
- };
- string addr = "127.0.0.1";
- Action menu = () =>
- {
- Rect wnd = new Rect(Screen.width / 2 - 75, Screen.height / 2 - 64, 150, 128);
- GUILayout.BeginArea(wnd);
- addr = GUILayout.TextField(addr, GUILayout.ExpandWidth(true));
- if (GUILayout.Button("Join Game", GUILayout.ExpandHeight(true)))
- {
- Network.Connect(addr, 48484);
- guiState = game;
- }
- if (GUILayout.Button("Host Game", GUILayout.ExpandHeight(true)))
- {
- Network.InitializeServer(32, 48484, !Network.HavePublicAddress());
- guiState = game;
- RPCBuild(new Vector3(128, 128, 128));
- }
- GUILayout.EndArea();
- };
- guiState = menu;
- }
- void OnPlayerDisconnected(NetworkPlayer player)
- {
- if (Network.isServer)
- {
- EvtChat("@ Player Disconnected");
- }
- }
- void OnPlayerConnected(NetworkPlayer player)
- {
- if (Network.isServer)
- {
- foreach (var rpc in rpcQueue)
- {
- networkView.RPC(rpc.Key, player, rpc.Value);
- }
- networkView.RPC("EvtChat", player, "Press Left Ctrl to switch play/chat.");
- EvtChat("@ Player Connected");
- }
- }
- private void RPCBuild(Vector3 position)
- {
- networkView.RPC("EvtBuild", RPCMode.All, position);
- }
- private void RPCErase(Vector3 position)
- {
- if (position != new Vector3(128, 128, 128)) // GOD BLOCK
- networkView.RPC("EvtErase", RPCMode.All, position);
- }
- private void RPCChat(string message)
- {
- networkView.RPC("EvtChat", RPCMode.All, message);
- }
- void Update()
- {
- if (Input.GetKeyDown(KeyCode.LeftControl))
- Screen.lockCursor = !Screen.lockCursor;
- if (Input.GetKeyDown(KeyCode.Return) && !string.IsNullOrEmpty(chatString))
- {
- RPCChat(chatString);
- chatString = string.Empty;
- }
- if (!Screen.lockCursor)
- return;
- transform.Translate(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
- mouseX += Input.GetAxis("Mouse X") * 10.0f * sensitivity;
- mouseY = Mathf.Clamp(mouseY + Input.GetAxis("Mouse Y") * 10.0f * sensitivity, -90, 90);
- transform.rotation = Quaternion.Euler(-mouseY, mouseX, 0);
- if (Input.GetMouseButtonDown(0) && HitBlock())
- RPCBuild(hit.transform.position + hit.normal);
- if (Input.GetMouseButtonDown(1) && HitBlock())
- RPCErase(hit.transform.position);
- }
- [RPC]
- void EvtBuild(Vector3 position)
- {
- if (Network.isServer)
- rpcQueue.Add(new KeyValuePair<string, object>("EvtBuild", position));
- var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
- cube.transform.position = position;
- int x = Mathf.RoundToInt(position.x);
- int y = Mathf.RoundToInt(position.y);
- int z = Mathf.RoundToInt(position.z);
- level[x, y, z] = cube;
- }
- [RPC]
- void EvtErase(Vector3 position)
- {
- if (Network.isServer)
- rpcQueue.Add(new KeyValuePair<string, object>("EvtErase", position));
- int x = Mathf.RoundToInt(position.x);
- int y = Mathf.RoundToInt(position.y);
- int z = Mathf.RoundToInt(position.z);
- Destroy(level[x, y, z]);
- }
- [RPC]
- void EvtChat(string message)
- {
- if (Network.isServer)
- rpcQueue.Add(new KeyValuePair<string, object>("EvtChat", message));
- chatMessages.Add(message);
- if (chatMessages.Count > 10)
- chatMessages.RemoveAt(0);
- }
- bool HitBlock()
- {
- return Physics.Raycast(transform.position, transform.forward, out hit, range, blockLayer);
- }
- void OnGUI()
- {
- if (guiState != null)
- guiState();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement