using DarkRift; using DarkRift.Client; using DarkRift.Client.Unity; using System.Collections.Generic; using System.Linq; using UnityEngine; using System; /// /// Manages the world of blocks. /// internal class BlockWorld : MonoBehaviour { /// /// The client to communicate with the server via. /// [SerializeField] [Tooltip("The client to communicate with the server via.")] UnityClient client; /// /// The block prefab to spawn in the world. /// [SerializeField] [Tooltip("The block object to spawn.")] GameObject blockPrefab; /// /// The list of blocks spawned. /// List blocks = new List(); void Awake() { if (client == null) { Debug.LogError("No client assigned to BlockWorld component!"); return; } client.MessageReceived += Client_MessageReceived; } /// /// INvoked when the server receives a message. /// /// /// void Client_MessageReceived(object sender, MessageReceivedEventArgs e) { using (Message message = e.GetMessage() as Message) { //If we're placing a block we need to instantiate our prefab if (message.Tag == BlockTags.PlaceBlock) { using (DarkRiftReader reader = message.GetReader()) { Vector3 position = new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()); GameObject o = Instantiate( blockPrefab, position, Quaternion.identity ) as GameObject; o.transform.SetParent(transform); blocks.Add(o); } } //If we're destroying we need to find the block and destroy it else if (message.Tag == BlockTags.DestroyBlock) { using (DarkRiftReader reader = message.GetReader()) { Vector3 position = new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()); GameObject block = blocks.SingleOrDefault(b => b != null && b.transform.position == position); if (block == null) return; Destroy(block); blocks.Remove(block); } } } } internal void AddBlock(Vector3 position) { if (client == null) { Debug.LogError("No client assigned to BlockWorld component!"); return; } //Don't worry about snapping, we'll do that on the server using (DarkRiftWriter writer = DarkRiftWriter.Create()) { writer.Write(position.x); writer.Write(position.y); writer.Write(position.z); using (Message message = Message.Create(BlockTags.PlaceBlock, writer)) client.SendMessage(message, SendMode.Reliable); } } internal void DestroyBlock(Vector3 position) { if (client == null) { Debug.LogError("No client assigned to BlockWorld component!"); return; } //Don't worry about snapping, we'll do that on the server using (DarkRiftWriter writer = DarkRiftWriter.Create()) { writer.Write(position.x); writer.Write(position.y); writer.Write(position.z); using (Message message = Message.Create(BlockTags.DestroyBlock, writer)) client.SendMessage(message, SendMode.Reliable); } } internal void RemoveAllBlocks() { foreach (GameObject block in blocks) Destroy(block); blocks.Clear(); } }