/// <summary> /// Overrides the current structure (read: removes all previous blocks) with the serialized one in the buffer. /// Lazely validates the data and returns false, if it is found invalid. /// No checks are not made, #GetStructureErrors and #GetNotConnectedBlocks should be called after this method. /// </summary> public bool Deserialize(BitBuffer buffer) { foreach (RealPlacedBlock block in _blocks.Values.OfType <RealPlacedBlock>().ToList()) { RemoveBlock(block); } try { while (buffer.TotalBitsLeft >= RealPlacedBlock.SerializedBitsSize) { ushort type = (ushort)buffer.ReadBits(BlockFactory.BlockTypeSerializedBitsSize); if (type >= BlockFactory.TypeCount) { return(false); } BlockPosition position = BlockPosition.Deserialize(buffer); byte rotation = Rotation.Deserialize(buffer); BlockInfo info = BlockFactory.GetInfo(BlockFactory.GetType(type)); bool result = info is SingleBlockInfo single ? AddBlock(position, single, rotation) : AddBlock(position, (MultiBlockInfo)info, rotation); if (!result) { return(false); } } return(true); } catch (Exception e) { Debug.Log("Exception caught while deserializing into an EditableStructure: " + e); return(false); } }
private void Start() { if (_blocks.Count == 0) { Assert.IsTrue(BlockPosition.FromVector(transform.position, out BlockPosition position), "Failed to get a BlockPosition from the EditableStructure position."); Assert.IsTrue(AddBlock(position, (MultiBlockInfo)BlockFactory.GetInfo(BlockType.Mainframe), Rotation.GetByte(BlockSides.Top, 0)), "Failed to place the Mainframe."); } }
private void ShowPreview(BlockPosition position, byte rotation) { Destroy(_previewObject); _previousPreviewPosition = position; BlockInfo info = BlockFactory.GetInfo(BlockFactory.GetType(_blockType)); Color color; if (_structure.CanAddBlock(position, info, rotation)) { color = Color.white; } else { if (_structure.IsPositionOccupied(position)) { return; } else { color = Color.red; } } RealPlacedBlock block; if (info is SingleBlockInfo single) { block = BlockFactory.MakeSinglePlaced(_structure.transform, single, rotation, position); } else { // ReSharper disable once UnusedVariable block = BlockFactory.MakeMultiPlaced(_structure.transform, (MultiBlockInfo)info, rotation, position, out PlacedMultiBlockPart[] parts); if (block == null) { return; } } _previewObject = block.gameObject; _previewObject.gameObject.name = "PreviewBlock"; BlockUtilities.RemoveCollider(_previewObject, true); color.a = 0.5f; BlockUtilities.SetColor(_previewObject, color, true); }
private void Place() { // ReSharper disable once UnusedVariable if (!GetSelectedBlock(out GameObject block, out BlockPosition position, out byte rotation)) { return; } BlockInfo info = BlockFactory.GetInfo(BlockFactory.GetType(_blockType)); if (_structure.TryAddBlock(position, info, rotation)) { ColorNotConnectedBlocks(); ShowPreview(position, rotation); } }
private void Deserialize(BitBuffer buffer) { while (buffer.TotalBitsLeft >= RealPlacedBlock.SerializedBitsSize) { ushort type = (ushort)buffer.ReadBits(BlockFactory.BlockTypeSerializedBitsSize); BlockPosition position = BlockPosition.Deserialize(buffer); BlockInfo info = BlockFactory.GetInfo(BlockFactory.GetType(type)); if (info.Type == BlockType.Mainframe) { _mainframePosition = position; } byte rotation = Rotation.Deserialize(buffer); RealLiveBlock block; if (info is SingleBlockInfo single) { block = BlockFactory.MakeSingleLive(transform, single, rotation, position); } else { block = BlockFactory.MakeMultiLive(transform, (MultiBlockInfo)info, rotation, position, out LiveMultiBlockPart[] parts); foreach (LiveMultiBlockPart part in parts) { _blocks.Add(part.Position, part); } } Health += info.Health; Mass += info.Mass; _blocks.Add(position, block); if (SystemFactory.Create(this, block, out BotSystem system)) { _systems.Add(position, system); } } }